SSH密钥对是Linux系统管理中安全远程访问的基础工具。作为IT工程师,我将详细介绍SSH密钥的配置与管理方法。
# 生成Ed25519密钥对(推荐)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 生成RSA密钥对(4096位)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
生成过程中会提示:
1. 密钥保存路径(默认为~/.ssh/id_算法
)
2. 设置密钥密码(可选但推荐)
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@remote_host
~/.ssh/authorized_keys
文件中bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
编辑~/.ssh/config
文件:
Host *
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
ServerAliveInterval 60
TCPKeepAlive yes
Host myserver
HostName server.example.com
User myusername
Port 2222
IdentityFile ~/.ssh/myserver_key
IdentityFile
指令指定特定密钥在authorized_keys
中添加选项:
from="192.168.1.*",command="/bin/backup-script",no-agent-forwarding,no-port-forwarding,no-pty ssh-ed25519 AAAAC3Nz... user@host
# 启动ssh-agent
eval "$(ssh-agent -s)"
# 添加密钥到agent
ssh-add ~/.ssh/id_ed25519
# 列出已加载密钥
ssh-add -l
权限问题:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
SELinux阻止:
restorecon -Rv ~/.ssh
调试连接:
ssh -vvv user@host
PasswordAuthentication no
)通过以上方法,您可以有效管理Linux系统中的SSH密钥对,确保安全高效的远程访问。