1、设置root密码
如果root未设置过密码,先以普通账号登录,然后输入以下命令来修改root密码:
sudo passwd root
2、开启root远程登录
切换root用户
修改sshd_config文件,将PermitRootLogin的值改成yes,并保存
![](https://img-blog.csdnimg.cn/img_convert/fafaceb67b27b020592135f1b98d5ba9.png)
修改sshd_config文件,将PasswordAuthentication的值改成yes,并保存
![](https://img-blog.csdnimg.cn/img_convert/fef9199e09b494c90bea428c2719358a.png)
修改sshd_config文件,将PubkeyAuthentication的值改成yes,并保存
![](https://img-blog.csdnimg.cn/img_convert/04ab16eac2a993d323a49e816f2e4940.png)
修改认证文件/root/.ssh/authorized_keys
vi /root/.ssh/authorized_keys
删除如下内容
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"centos\" rather than the user \"root\".';echo;sleep 10"
![](https://img-blog.csdnimg.cn/img_convert/97036717dbb6f3ab30510f712e2017b0.png)
重启ssh服务
# service sshd restart
systemctl restart sshd.service
3、测试root远程登录成功
快捷脚本
sed -i 's/^#\?PermitRootLogin yes./PermitRootLogin yes/g' /etc/ssh/sshd_config;
sed -i 's/^#\?PasswordAuthentication yes./PasswordAuthentication yes/g' /etc/ssh/sshd_config;
sed -i 's/^#\?PubkeyAuthentication yes./PubkeyAuthentication yes/g' /etc/ssh/sshd_config;
echo "" > /root/.ssh/authorized_keys;
service sshd restart;