使用SSH公钥配置Linux免密登录
我们平时都会使用ssh进行远程登录 ssh username@ip,然后输入密码既可以登录成功。但是每次ssh登录,scp远程复制等,都输入密码却相当麻烦。为了解决这个问题,我们可以使用ssh的公钥,配置免密登录。配置过程主要包括以下几步:
- step 1 使用ssh-keygen 创建公钥
ssh-keygen -t rsa
按回车以后可以得到类似以下输出:
Output
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:
+--[ RSA 2048]----+
| .oo. |
| . o.E |
| + . o |
| . = = . |
| = S = . |
| o + = + |
| . o + o . |
| . o |
| |
+-----------------+
- step 2 将上述产生的公钥复制到目标服务器上
ssh-copy-id username@ip
或者使用scp 复制到远程服务器.ssh/authorized_keys 下
scp id_rsa.pub username@ip:/home/username
cat id_rsa.pub >>.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub | ssh demo@198.51.100.0 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"
以上步骤就已经完成配置。