1.切换root用户
(1)由于腾讯云服务器登录默认是ubuntu用户,所以使用sudo passwd root
创建root用户和密码
注意:su和sudo的区别:
su的密码是root的密码,而sudo的密码是用户的密码;
su直接将身份变成root用户。
(2)sudo vi /etc/ssh/sshd_config
修改ssh配置
找到 #PermitRootLogin prohibit-password
修改为 PermitRootLogin yes
*重装系统选择密钥登录的还需要修改为 PasswordAuthentication yes
保存退出
(3)重启ssh服务:
sudo service ssh restart
(4)可以重新连接改为root用户登录
2.创建一个密钥对
cd /root/.ssh
ssh-keygen -t rsa -b 4096
一路回车即可,保存文件地址(~/.ssh)和密码(无)都默认即可
3.将密钥对中的公钥放进服务器的authorized_keys文件中
- 使用ssh-copy-id命令来自动添加
ssh-copy-id -i /root/.ssh/id_rsa.pub root@服务器IP地址
- 查看authorized_keys中是否已有公钥
cat authorized_keys
4.将私钥下载到本地
5.使用密钥登录服务器
6.设置服务器只允许密钥登录
(1)打开ssh配置
sudo vi /etc/ssh/sshd_config
(2)修改为
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
ChallengeResponseAuthentication no
RSAAuthentication yes
(3)重启sshd服务
sudo systemctl restart sshd