基于ssh密钥访问远程Linux
1、在本地机器上生成密钥对(默认保存在 ~/.ssh/)
ssh-keygen -t ed25519
或使用 RSA(兼容性更好):
ssh-keygen -t rsa -b 4096
2、 将公钥上传到远程主机
方法一:使用 ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@remote_ip
公钥内容会追加到远程主机的 ~/.ssh/authorized_keys
方法二:手动复制公钥
cat ~/.ssh/id_ed25519.pub | ssh username@remote_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
3、访问远程主机
scp -i ~/.ssh/id_ed25519 /path/to/local_file username@remote_ip:/remote/path
如果私钥是 默认名称+路径(如 ~/.ssh/id_ed25519),则无需 -i
scp /local/file username@remote_ip:/remote/path