返回> 网站首页 

SSH - 改成“公钥认证 + 禁用密码”

yoours2026-10-19 20:46:17 阅读 111

简介一边听听音乐,一边写写文章。

1. 本地生成密钥对

    ssh-keygen -t ed25519 -C "rk3588"

    会生成:

    ~/.ssh/id_ed25519:私钥,不要泄露

    ~/.ssh/id_ed25519.pub:公钥,放到服务器上

    或

    ssh-keygen -t rsa -b 4096 -C "rk3588-dev" -f ~/.ssh/id_rsa_rk3588


2. 把公钥放到 RK3588 上

    cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak


    ssh-copy-id user@rk3588_ip

    如果没有 ssh-copy-id,就手动把 id_ed25519.pub 内容追加到服务器上的

    ~/.ssh/authorized_keys


确认权限:

    chmod 700 ~/.ssh

    chmod 600 ~/.ssh/authorized_keys

    chown -R $USER:$USER ~/.ssh


3. 测试密钥登录

    ssh -i ~/.ssh/id_ed25519 user@rk3588_ip


4. 禁用密码登录

    sudo nano /etc/ssh/sshd_config


修改或添加

    PubkeyAuthentication yes

    PasswordAuthentication no

    PermitRootLogin prohibit-password


5. 权限

    chmod 700 ~/.ssh

    chmod 600 ~/.ssh/authorized_keys

    chown -R $USER:$USER ~/.ssh


6. 推荐 sshd_config 配置

    Port 22222

    PermitRootLogin no

    PubkeyAuthentication yes

    PasswordAuthentication no

    PermitEmptyPasswords no

    MaxAuthTries 3

    LoginGraceTime 30

    ClientAliveInterval 300

    ClientAliveCountMax 2

    AllowUsers yourname

    X11Forwarding no

    PermitUserEnvironment no

    UseDNS no

    LogLevel VERBOSE

    SyslogFacility AUTH

    Banner /etc/ssh/sshd_banner


    检查SSH 服务配置有没有语法错误

    sudo sshd -t


7. 重启 SSH

    sudo systemctl restart ssh

    Ubuntu 上服务名通常是 ssh,部分系统是 sshd


8. 关键配置说明

配置项 作用 建议

Port 改默认端口,减少自动化扫描 49152-65535 任选

PermitRootLogin no 禁止 root 直接登录 必须

PasswordAuthentication no 关闭密码登录 确认密钥可用后再开

PubkeyAuthentication yes 启用公钥认证 必须

MaxAuthTries 3 限制登录尝试次数 3

ClientAliveInterval 300 空闲 5 分钟检测一次 防闲置会话

ClientAliveCountMax 2 2 次无响应断开 合计约 10 分钟

AllowUsers 只允许指定用户登录 写实际用户名

LogLevel VERBOSE 记录更多登录审计信息 方便排查


9. 防火墙

    sudo ufw default deny incoming

    sudo ufw default allow outgoing

    sudo ufw allow 22222/tcp

    sudo ufw enable


    如果只允许固定 IP 登录:

    sudo ufw allow from 192.168.1.100 to any port 22222


10. 验证与排错

    systemctl status ssh

    journalctl -u ssh -f

    sudo ufw status verbose


微信小程序扫码登陆

文章评论

111人参与,0条评论