在Debian上配置SFTP密钥认证步骤如下:
生成SSH密钥对(客户端)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # 生成密钥对,默认保存~/.ssh/
(可按回车使用默认路径和空密码短语)
将公钥复制到服务器
ssh-copy-id
命令(推荐)ssh-copy-id user@server_ip # 输入服务器密码即可
cat ~/.ssh/id_rsa.pub
复制公钥内容。mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo "公钥内容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
配置SSH服务器
编辑/etc/ssh/sshd_config
:
sudo nano /etc/ssh/sshd_config
确保以下配置:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no # 可选:仅允许密钥认证
保存后重启SSH服务:
sudo systemctl restart sshd
测试SFTP连接
sftp -i ~/.ssh/id_rsa user@server_ip # 指定私钥路径
若配置正确,可直接登录无需密码。
安全加固(可选):
chroot jail
)。