例如:ubuntu
$ ssh -i ~/ec2.pem ubuntu@12.34.56.78
$ sudo ssh root@12.34.56.78
$ ssh-keygen -t rsa -b 2048 -v
执行上述命令首先会让你输入生成密钥的文件名:我这里输入的 myPemKey ,以后一路回车。ruby
Generating public/private rsa key pair. Enter file in which to save the key (/home/anonymouse/.ssh/id_rsa): myPemKey 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:
在执行命令的当前目录下会生成一个myPemKey.pub、myPemKey 两个文件。bash
$ ssh-copy-id -i ~/myPemKey.pub root@12.34.56.78
输入你的 root 用户密码服务器
$ sudo ssh -i ~/myPemKey root@12.34.56.78
或者把 myPemKey 重命名为 myPemKey.pemdom
$ sudo ssh -i ~/myPemKey.pem root@12.34.56.78
sudo vi /etc/ssh/sshd_config
找到这一行 #PasswordAuthentication yesssh
# Change to no to disable tunnelled clear text passwords # PasswordAuthentication yes
取消前边的 # 注释,改成ide
PasswordAuthentication no
重启 ssh 服务测试
sudo service ssh restart