Ubuntu 18.04 ssh key的配置以及免密登录
-
https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-1804
1.在客户端创建私钥和公钥$ ssh-keygen生成的私钥key默认保存在~/.ssh/id_rsa文件中。重复执行这个命令会覆盖这个文件,会导致之前的key无法使用。
生成过程中会提示加密,建议输入一个密码进行安全保护。Output Your identification has been saved in /your_home/.ssh/id_rsa. Your public key has been saved in /your_home/.ssh/id_rsa.pub(公钥). The key fingerprint is: a9:49:2e:2a:5e:33:3e:a9:de:4e:77:11:58:b6:90:26 username@remote_host The key's randomart image is: +--[ RSA 2048]----+ | ..o | | E o= . | | o. o | | .. | | ..S | | o o. | | =o.+. | |. =++.. | |o=++. | +-----------------+2.将公钥上传到服务器
方法一 使用ssh-copy-id 最简单$ ssh-copy-id username@remote_host Output /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys username@203.0.113.1's password:方法二 拷贝
cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"方法三 手动将id_rsq.pub的内容粘贴到服务器的~/.ssh/authorized_keys
id_rsa.pub file to the ~/.ssh/authorized_keys3.登录远端服务器
$ ssh username@remote_host4.禁用服务器的密码登录(可选)
sudo nano /etc/ssh/sshd_config
PasswordAuthentication no
sudo systemctl restart ssh