在 Debian 上通过 SFTP 使用密钥登录,一般流程如下(以 OpenSSH 为例):
在你自己的电脑(客户端)执行:
ssh-keygen -t rsa -b 4096
一路回车即可,默认生成:
~/.ssh/id_rsa~/.ssh/id_rsa.pub建议设置私钥密码(passphrase),更安全
ssh-copy-id user@server_ip
输入一次密码后,公钥会自动写入服务器:
~/.ssh/authorized_keys
在客户端查看公钥:
cat ~/.ssh/id_rsa.pub
在 Debian 服务器上:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
粘贴公钥,保存后:
chmod 600 ~/.ssh/authorized_keys
编辑配置文件:
sudo nano /etc/ssh/sshd_config
确保以下内容正确:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no # 可选,禁止密码登录更安全
重启 SSH:
sudo systemctl restart ssh
sftp -i ~/.ssh/id_rsa user@server_ip
id_rsa 私钥如果私钥有密码,连接时会提示输入
服务器上必须严格:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
家目录不能对组/其他可写
Debian 默认无 SELinux,可忽略
在 /etc/ssh/sshd_config 加:
Match User sftpuser
PasswordAuthentication no
ForceCommand internal-sftp
ChrootDirectory /home/sftpuser
如果你是想 只允许 SFTP、禁止 SSH、或 用非 root 用户 + chroot,可以告诉我,我可以给你完整配置示例。