ssh切换root登陆
像aws、oracle、gcp等这样的大厂,开了实例后默认不是以root身份来登陆ssh,为了操作方便一般会直接修改为root方式来登陆,方法如下:
一、命令操作
1、切换到root用户:sudo -i
2、修改SSH配置文件:vi /etc/ssh/sshd_config
3、找到PermitRootLogin和PasswordAuthentication项,默认为no,修改为yes
4、重启SSH服务:
1)【Debian & Ubuntu】系统:/etc/init.d/ssh restart
1)【CentOS】系统:/bin/systemctl restart sshd.service
5、设置root账户密码:passwd
6、重启:reboot
二、一键脚本
一键脚本的方式虽然方便,但有时会失效,因为各平台的差异不能通用,下面贴出gcp的脚本:
echo "root:yourpassword" | sudo chpasswd root && sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config && sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config && sudo service ssh restart && sudo reboot && echo "Password changed successfully and SSH configuration updated. Restarting Server..." || echo "Failed to change password or update SSH configuration"