ssh无密码登录的设置步骤
首先我们在自己的linux系统上生成一对ssh key:ssh密钥和ssh公钥.密钥保存在自己的linux系统上。
然后公钥上传到linux服务器.之后我们就能无密码ssh登录了.ssh密钥就好比是你的身份证明.
1在自己的linux系统上生成ssh密钥和公钥
打开终端,使用下面的ssh-keygen来生成rsa密钥和公钥.-t表示type,就是说要生成rsa加密的钥匙.
ssh-keygen -t rsa
rsa也是默认的加密类型.所以你也可以只输入ssh-keygen.默认的rsa长度是2048位.如果你非常注重安全,那么可以指定4096位的长度.
ssh-keygen -b 4096 -t rsa
生成ssh key的过程中会要求你指定一个文件来保存密钥,按enter键使用默认的文件就行了.然后需要输入一个密码来加密你的ssh key.密码至少要20位长度.ssh密钥会保存在home目录下的.ssh/id_rsa文件中.ssh公钥保存在.ssh/id_rsa.pub文件中.
generating public/private rsa key pair.
enter file in which to save the key (/home/matrix/.ssh/id_rsa): 按enter键
enter passphrase (empty for no passphrase): 输入一个密码
enter same passphrase again: 再次输入密码
your identification has been saved in /home/matrix/.ssh/id_rsa.
your public key has been saved in /home/matrix/.ssh/id_rsa.pub.
the key fingerprint is:
e1:dc:ab:ae:b6:19:b0:19:74:d5:fe:57:3f:32:b4:d0 matrix@vivid
the key's randomart image is:
+---[rsa 4096]----+
| .. |
| . . |
| . . .. . |
| . . o o.. e .|
| o s ..o ...|
| = ..+...|
| o . . .o .|
| .o . |
查看.ssh/id_rsa文件就会看到,这个文件是经过加密的(encrypted).也就是用你输入的密码来加密.
less .ssh/id_rsa
ssh无密码登录
2将ssh公钥上传到linux服务器
可以使用ssh-copy-id命令来完成.
ssh-copy-id username@remote-server
输入远程用户的密码后,ssh公钥就会自动上传了.ssh公钥保存在远程linux服务器的.ssh/authorized_keys文件中.
上传完成后,ssh登录就不需要再次输入密码了.但是首次使用ssh key登录时需要输入一次ssh密钥的加密密码.(只需要输入一次,将来会自动登录,不再需要输入密钥的密码.)
使用scp命令来传送文件时也不需要输入密码.