要实现从服务器 192.168.103.123
到服务器 192.168.104.3
的免密登录,以便使用 rsync
或其他工具传输文件时无需输入密码,你需要将 192.168.103.123
的公钥添加到 192.168.104.3
的 root
用户的 authorized_keys
文件中。
以下是详细步骤:
在 192.168.103.123
服务器上操作
检查是否已经生成了 SSH 密钥对:
bash
复制
ls -l ~/.ssh/id_ed25519.pub
如果文件存在,说明已经生成了密钥对。
如果文件不存在,生成一个新的密钥对:
bash
复制
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519
复制公钥到
192.168.104.3
服务器: 使用ssh-copy-id
命令将公钥复制到目标服务器:bash
复制
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@192.168.104.3
如果提示输入密码,输入
192.168.104.3
服务器的root
用户密码。如果成功,你会看到类似以下的输出:
复制
/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 root@192.168.104.3's password: Number of keys added: 1 Now try logging into the machine, with: "ssh 'root@192.168.104.3'" and check to make sure that only the key(s) you wanted were added.
验证免密登录是否成功:
bash
复制
ssh root@192.168.104.3
如果无需输入密码即可登录,说明免密登录配置成功。
使用
rsync
传输文件: 现在你可以使用rsync
命令传输文件而无需输入密码:bash
复制
rsync -avzP --progress /elk root@192.168.104.3:/elk
在 192.168.104.3
服务器上操作(可选)
如果你想确保 authorized_keys
文件中正确添加了公钥,可以手动检查:
检查
authorized_keys
文件:bash
复制
cat ~/.ssh/authorized_keys
确保文件中包含
192.168.103.123
服务器的公钥内容。
确保
~/.ssh
目录和authorized_keys
文件的权限正确:bash
复制
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
总结
通过上述步骤,你可以实现从 192.168.103.123
到 192.168.104.3
的免密登录。这样在使用 rsync
或其他工具传输文件时,就不需要手动输入密码了。