1.SSH端口

nano /etc/ssh/sshd_config

使用 ctrl+w 进入搜索模式,然后输入 Port 22 并回车
删除 22 并改成 9753

#Port 22
sed -i "s/Port .*/Port 9753/g" /etc/ssh/sshd_config

#Port 22
sed -i "s/#Port .*/Port 9753/g" /etc/ssh/sshd_config

#重启ssh
sudo service sshd restart

2.设置复杂密码

passwd
```
该部分仅登录用户可见
## 3.新建普通用户 ``` adduser cokan ```
该部分仅登录用户可见
[创建强大、安全、随机的密码 | Password 密码生成器 | 1Password](https://1password.com/zh-cn/password-generator/) ## 4.安装sudo ``` apt update && apt install sudo visudo ``` `在 User Privilege Specification 下加入一行` ``` cokan ALL=(ALL) NOPASSWD: ALL

5.禁止root登录

nano /etc/ssh/sshd_config

使用 ctrl+w 进入搜索模式,然后输入 PermitRootLogin 并回车

PermitRootLogin Yes这一项,然后把它后面的设定值改为no

sed -i "s/PermitRootLogin .*/PermitRootLogin no/g" /etc/ssh/sshd_config ; /etc/init.d/ssh restart

6.启用RSA密钥验证登录

  • 本地生成密钥
    需要登录的账号登录,非root账号!!!

    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    ssh-keygen -t rsa -b 4096 -C "cokan-vps"
    
    cat id_dsa.pub >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    
  • 远程复制密钥到当前服务器
    需要登录的账号登录,非root账号!!!

    mkdir -p /home/cokan/.ssh
    chmod 700 /home/cokan/.ssh
    scp -P 9953 [email protected]:/home/cokan/.ssh/id_rsa.pub /home/cokan/.ssh/authorized_keys
    chmod 600 /home/cokan/.ssh/authorized_keys
  • 启用密钥登录,关闭服务器密码登录
    nano /etc/ssh/sshd_config
    PubkeyAuthentication Yes 这一项,然后把它后面的设定值改为Yes
    PasswordAuthentication no 这一项,然后把它后面的设定值改为no
    sudo service sshd restart

启用密钥登录

sed -i "s/#PubkeyAuthentication .*/PubkeyAuthentication yes/g" /etc/ssh/sshd_config ; /etc/init.d/ssh restart

关闭密码登录

sed -i "s/#PasswordAuthentication .*/PasswordAuthentication no/g" /etc/ssh/sshd_config ; /etc/init.d/ssh restart

Ubuntu SSH密钥远程登陆VPS(CentOS)以及多SSH-KEY管理 - 云途主机 (vpsvt.com)

最后修改:2022 年 12 月 26 日
如果觉得我的文章对你有用,请随意赞赏