作者选择了 开放的互联网 / 自由言论基金作为 写给捐赠计划的一部分获得捐款。
介绍
所有基于 [Linux] (https://andsky.com/tech/tutorials/an-introduction-to-the-linux-terminal) 的机器都带有默认的[root user] (https://andsky.com/tech/tutorials/initial-server-setup-with-ubuntu-20-04# about-root),在机器上拥有所有权限;默认情况下,你总是扮演一个root用户(一个_超级用户_). 良好的安全做法建议您在 [SSH] (https://andsky.com/tech/tutorials/ssh-essentials-working-with-ssh-servers-clients-and-keys] 上禁用根登录,以防止其他用户擅自访问基于 Linux 的机器。 禁用root登录会阻止root通过基于 Linux 的 SSH 访问您的机器,这意味着没有人会拥有无限权限. 遵循推荐的安全惯例,您应该创建一个额外的用户,拥有几乎所有的超级用户权限来访问账户.
在本教程中,您将禁用Ubuntu上的根登录,防止未经授权的根访问通过SSH,并提高基于Linux的系统的安全性。
前提条件
要完成本教程,您将需要:
- 一个 Ubuntu 20.04 服务器,具有非根用户的
sudo
特权,防火墙和至少 1 GB 的 RAM,您可以通过遵循 Ubuntu 20.04 初始服务器设置指南来设置。
步骤 1 — 登录并检查auth.log
在此步骤中,您将通过sudo
启用的非根用户访问您的服务器,以检查您的服务器的身份验证尝试. 通过检查身份验证日志,您可能会看到授权和未授权的登录尝试。
在前提条件期间,您创建了一个新用户,并将该用户添加到sudo
组中,以授予管理权限.您将使用这个sudo
用户访问您的机器,因为在禁用 root 登录后,您将无法作为 root 用户 SSH。
根据您选择的登录方法,使用 SSH 登录您的服务器. 如果您在初始服务器设置期间使用 SSH 密钥登录了根帐户,则必须使用基于密钥的机制,因为使用基于密钥的登录为您的服务器时禁用了密码身份验证。
登录您的服务器作为您的sudo
启用用户(在本教程中,它将是 sammy )使用以下命令以密码为基础的登录:
1ssh sammy@your_server_ip
如果使用基于密钥的登录方法,请以以下命令登录您的服务器作为sudo
功能的用户:
1ssh -i your_private_key sammy@your_server_ip
-i
旗帜代表了用于身份验证的your_private_key
的身份文件。
接下来,检查auth.log
文件,转到/var/log
目录:
1cd /var/log/
使用cat auth.log
来显示文件的内容:
1sudo cat auth.log
请在提示时输入您的密码。
您将获得类似于此的输出:
1[secondary_label Output]
2May 29 18:46:32 ubuntu sshd[3886]: Disconnected from invalid user cally 43.155.90.144 port 47454 [preauth]
3May 29 18:51:56 ubuntu sshd[3890]: Received disconnect from 195.38.129.16 port 10017:11: Bye Bye [preauth]
4May 29 18:51:56 ubuntu sshd[3890]: Disconnected from authenticating user root 195.38.129.16 port 10017 [preauth]
5May 29 18:52:24 ubuntu sshd[3892]: Received disconnect from 178.128.234.248 port 58660:11: Bye Bye [preauth]
6May 29 18:52:24 ubuntu sshd[3892]: Disconnected from authenticating user root 178.128.234.248 port 58660 [preauth]
7May 29 18:52:34 ubuntu sshd[3894]: Received disconnect from 43.134.106.128 port 33854:11: Bye Bye [preauth]
8May 29 18:52:34 ubuntu sshd[3894]: Disconnected from authenticating user root 43.134.106.128 port 33854 [preauth]
9May 29 18:53:07 ubuntu sshd[3896]: Invalid user projects from 176.183.60.72 port 42070
10May 29 18:53:07 ubuntu sshd[3896]: Received disconnect from 176.183.60.72 port 42070:11: Bye Bye [preauth]
11May 29 18:53:07 ubuntu sshd[3896]: Disconnected from invalid user projects 176.183.60.72 port 42070 [preauth]
12May 29 18:57:27 ubuntu sshd[3900]: Received disconnect from 92.255.85.135 port 20436:11: Bye Bye [preauth]
13May 29 18:57:27 ubuntu sshd[3900]: Disconnected from authenticating user root 92.255.85.135 port 20436 [preauth]
14May 29 19:06:40 ubuntu sshd[3903]: Invalid user default from 27.71.207.190 port 57513
15May 29 19:06:41 ubuntu sshd[3903]: Connection closed by invalid user default 27.71.207.190 port 57513 [preauth]
16...
auth.log
文件记录了向服务器进行的所有身份验证尝试. 您可能会看到许多未知和未经授权的请求被您的服务器接收. 出于这个原因,您可能希望在您的系统上禁用根登录并定期旋转您的密钥和密码。
您现在已经审查了验证日志,这些日志表明您的服务器已接收的不仅仅是您的验证请求,接下来,您将更新您的服务器的SSH配置,以便在您的服务器上完全禁用根访问。
步骤 2 — 禁用根登录
在此步骤中,您将编辑 sshd_config
文件以禁用 root 登录,然后重新启动 sshd
对象,以便在更改后阅读配置。
「sshd_config」文件存储了包含「sshd」使用的参数的 SSH DAEMON 配置。 该 DAEMON 负责处理 SSH 连接。 您需要重新启动「sshd」 DAEMON 来应用配置更改。
使用 nano 或您最喜欢的文本编辑器打开位于 /etc/ssh
目录中的 sshd_config
文件:
1sudo nano /etc/ssh/sshd_config
檢查檔案,尋找 'PermitRootLogin' 行:
1[secondary_label Output]
2...
3
4#LoginGraceTime 2m
5PermitRootLogin yes
6#StrictModes yes
7#MaxAuthTries 6
8#MaxSessions 10
9
10...
将允许RootLogin
的值从是
更改为不
:
1[secondary_label Output]
2...
3
4#LoginGraceTime 2m
5PermitRootLogin no
6#StrictModes yes
7#MaxAuthTries 6
8#MaxSessions 10
9
10...
保存并关闭文件。
接下来,您将重新启动sshd
大门,在您刚刚做的更改后阅读配置。
使用以下命令重新启动 DAEMON:
1sudo systemctl restart sshd
此命令将使用systemctl
重新启动sshd
服务。
在此步骤中,您更改了配置文件以拒绝 root 登录请求,并重新启动了sshd
以读取最新的配置。
步骤 3 — 测试 root 登录
禁用 root 登录后,尝试使用 SSH 作为 root 登录到新的终端会话。
使用基于密码的登录:
1ssh root@your_server_ip
如果使用基于密钥的登录:
1ssh -i your_private_key root@your_server_ip
尝试将 SSH 作为 root 以此类型的错误消息失败:
1[secondary_label Output]
2root@your_server_ip: Permission denied (publickey).
若要再次访问服务器,请使用sudo
启用的非根用户凭证登录您的服务器,以确认您仍然可以访问服务器。
使用基于密码的登录:
1ssh sammy@your_server_ip
如果使用基于密钥的登录:
1ssh -i your_private_key sammy@your_server_ip
您现在可以根据需要继续使用服务器。
结论
在本文中,您配置了sshd
配置以禁用Ubuntu上的根登录,现在您知道如何防止根登录到基于Linux的机器,从而为您的机器添加额外的安全层。
要继续设置您的机器,请阅读更多关于如何更新Ubuntu 20.04服务器(https://andsky.com/tech/tutorials/how-to-keep-ubuntu-20-04-servers-updated)。