设置服务器秘钥授权登录
方式一
本地生成密钥对
ssh-keygen -t rsa -b 2048 -v
执行上述命令首先会让你输入生成密钥的文件名:我这里输入的 myPemKey ,之后一路回车。
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xxx/.ssh/id_rsa): Test
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in hetzner.
Your public key has been saved in hetzner.pub.
The key fingerprint is:
bb:c6:9c:ee:6b:c0:67:58:b2:bb:4b:44:72:d3:cc:a5 localhost@localhost
The key's randomart image is:
在执行命令的当前目录下会生成一个Test.pub、Test 两个文件。
推送Test.pub到服务器
ssh-copy-id -i ./Test.pub root@192.168.0.6
重命名Test到Test.pem
mv Test Test.pem
远程登录
ssh -i ./Test.pem root@192.168.0.6
方式二
服务器端生成密钥对
生成密钥对,同方式一。
修改sshd配置文件
sudo vim /etc/ssh/sshd_config
'''
PubkeyAuthentication yes #启用公告密钥配对认证方式
AuthorizedKeysFile %h/.ssh/authorized_keys #设定PublicKey文件路径
RSAAuthentication yes #允许RSA密钥
PasswordAuthentication no #禁止密码验证登录,如果启用的话,RSA认证登录就没有意义了
#禁用root账户登录,非必要,但为了安全性,请配置
PermitRootLogin no
'''
创建authorized_keys文件
cat Test.pub >> authorized_keys
对authorized_keys设置600权限
chmod 600 authorized_keys
重启ssh服务
sudo /etc/init.d/ssh restart
重命名Test到Test.pem
mv Test Test.pem
远程登录
ssh ./Test.pem root@192.168.0.6
删除账户密码
sudo passwd -d xxx(用户名)
添加账户密码
passwd xxx
设置账户允许使用sudo
sudo vi /etc/sudoers
在
# User privilege specification
root ALL=(ALL:ALL) ALL
下面添加
xxx ALL=(ALL:ALL) ALL
强制保存退出:wq!
设置sudo免密码使用
sudo vi /etc/sudoers
设置所有用户免密码使用
修改
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
到
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) NOPASSWD: ALL
设置单个用户免密码使用
在
# User privilege specification
root ALL=(ALL:ALL) ALL
添加
# User privilege specification
root ALL=(ALL:ALL) ALL
xxx ALL=(ALL:ALL) NOPASSWD: ALL
强制保存退出:wq!