ssh-keygen -t rsa
输入你想创建的公钥的名字,也可以输入打开私钥的密码。一般不输入。
在当前目录能看到keipair,后缀为pub的是公钥。另外一个是私钥,在本地保存好。
把公钥上传到服务器的家目录,打开或者创建.ssh目录
cd ~/.ssh
mv ~/公钥.pub.pem authorized_keys
chmod 400 authorized_keys
cd ..
chmod 400 ./.ssh
然后修改sshd配置文件:
nano /etc/ssh/sshd_config
把其中的
PubkeyAuthentication no 改成
PubkeyAuthentication yes
PasswordAuthentication yes 改成PasswordAuthentication no
注意前面的#号要去掉
重启sshd:
sudo service sshd restart
然后就可以使用private key远程登录了,不用输入密码
ssh -i yourkey.pem username@hostname