一、 使用具有root权限的用户登录,禁止root登录
1. 创建一个用户
#adduser myroot //添加一个名为myroot的用户 #passwd myroot//修改密码 Changing password for user myroot. New UNIX password: //在这里输入新密码 Retype new UNIX password: //再次输入新密码 passwd: all authentication tokens updated successfully.
2. 将创建的用户赋予Root权限
修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示:
## Allow root to run any commands anywhere root ALL=(ALL) ALL myroot ALL=(ALL) ALL
现在可以sudo su 进入root用户, 默认5分钟后刚才输入的sodo密码过期,下次sudo需要重新输入密码,如果觉得在sudo的时候输入密码麻烦,把刚才的输入换成如下内容即可:
myroot ALL=(ALL) NOPASSWD:ALL
注意 这个代码放在最后一名,以为下面的Group权限覆盖NOPASSWD,而导致无效,可以 vi /etc/sudoers.d/nopwd文件 输入上面的代码
二. 使用Key授权登录。禁止密码的登录
1. 使用puttygen 创建共有Key和私有Key,
将公有Key保存到服务器 /home/myroot/.ssh/authorized_keys文件中,文件需要手动创建
私有Key 在自己的putty客户端使用,注意保留好自己的私有Key
2. 修改./etc/ssh/sshd_config文件,将下面这几行的注释删掉
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
3. 重启ssh 服务,
service ssh restart
在客户端尝试使用putty免密码登录成功
如果key登录的时候出现Server refused our key错误,请检查服务用户目录权限,/home/myroot根目录文件644权限
SSH doesn’t like it if your home or ~/.ssh directories have group write permissions. Your home directory should be writable only by you, ~/.ssh should be 700, and authorized_keys should be 600
You can also get around this by adding StrictModes off to your ssh_config file, but I’d advise against it - fixing permissions is the way to go.
4. 修改SSH的配置文件/etc/ssh/sshd_config的PasswordAuthentication变量为no
PasswordAuthentication no
这样就只有myroot用户可以使用Key授权登录。root用户因为么有key而不能登录。