每个人都清楚,Linux 系统带有 root 用户权限,并且默认情况下,root 权限对外界是启用的。
出于安全考虑,不为未经授权的用户启用 ssh root 访问 并不是一个好主意。因为任何黑客都可以尝试对你的密码进行暴力破解,从而获得对你的系统的访问权限。

所以,最好创建另一个你经常使用的账户,然后在必要时使用 ‘su –’ 命令切换到 root 用户。在我们开始之前,请确保你有一个普通用户账户,通过该账户,你可以使用 su 或者 sudo 命令来获取 root 访问权限。
[ 你可能还喜欢: 如何保护和加强 OpenSSH 服务器的安全 ]
在 Linux 中,创建一个单独的账户非常简单,以 root 用户登录,然后只需运行 adduser 命令 就可以创建一个单独的用户。创建用户后,只需按照以下步骤禁用通过 SSH 的 root 登录。
我们使用sshd主配置文件来禁用root登录,这将可能减少并防止黑客获取root访问您的Linux主机。我们还会看到如何重新启用root访问,以及如何基于用户列表限制ssh访问。
禁用SSH Root登录
要禁用root登录,请使用您选择的编辑器打开主ssh配置文件/etc/ssh/sshd_config。
# vi /etc/ssh/sshd_config
在文件中搜索以下行。
#PermitRootLogin no
从行首删除“#”。使该行看起来类似于这样。
PermitRootLogin no

接下来,我们需要重新启动SSH守护程序服务。
# systemctl restart sshd OR # /etc/init.d/sshd restart
现在尝试使用root用户登录,您将收到“权限被拒绝”错误。
$ ssh [email protected] [email protected]'s password: Permission denied, please try again.

因此,从现在开始,请使用普通用户登录,然后使用‘su’命令切换到root用户。
$ ssh [email protected] [email protected]'s password: Last login: Mon Dec 27 15:04:58 2021 from 192.168.0.161 $ su - Password: Last login: Mon Dec 27 15:05:07 IST 2021 on pts/1

启用SSH Root登录
要启用ssh root登录,请打开文件/etc/ssh/sshd_config。
# vi /etc/ssh/sshd_config
搜索以下行并删除行首的“#”并保存文件。
PermitRootLogin yes

重新启动sshd服务。
# systemctl restart sshd OR # /etc/init.d/sshd restart
现在尝试使用root用户登录。
$ ssh [email protected] [email protected]'s password: Last login: Mon Dec 27 15:14:54 2021 from 192.168.0.161

限制SSH用户登录
如果您在系统上拥有大量用户帐户,那么限制远程SSH访问只给那些真正需要的用户是有意义的。打开/etc/ssh/sshd_config文件。
# vi /etc/ssh/sshd_config
在文件底部添加一个AllowUsers行,并用空格分隔用户名列表。例如,用户tecmint和sheena都可以访问远程SSH。
AllowUsers tecmint sheena

现在重新启动ssh服务。
Source:
https://www.tecmint.com/disable-or-enable-ssh-root-login-and-limit-ssh-access-in-linux/