linux访问linux
在本地服务器上生成SSH密钥对(如果已有可跳过):
ssh-keygen -t rsa -b 4096
按提示操作,默认会将密钥保存在 ~/.ssh/id_rsa 和 ~/.ssh/id_rsa.pub。
有root密码
复制公钥到目标服务器
ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_host
# 或指定端口号
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22000 root@remote_host
输入密码后,即可更新目标服务器。
没有root密码
手动复制公钥到目标服务器
本机服务器执行
# 查看生成的公钥
cat ~/.ssh/id_rsa.pub
目标服务器执行
创建 .ssh 目录并设置权限
mkdir -p ~/.ssh
chmod 700 ~/.ssh
追加公钥到 authorized_keys
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ..." >> ~/.ssh/authorized_keys
设置 authorized_keys 文件权限
chmod 600 ~/.ssh/authorized_keys
测试免密登录
尝试SSH连接目标服务器:
ssh user@remote_host
# 或指定端口
ssh -p 22000 root@remote_host
windows访问linux
生成密钥
ssh-keygen -t ed25519 -C "你的邮箱或备注"
查看密钥内容
cat ~/.ssh/id_ed25519.pub
登录服务器,把公钥内容放入
echo "在此处粘贴你的公钥内容" >> ~/.ssh/authorized_keys
测试连接。
ssh root@remote_host