为新开的vps配置ssh+密码登录,适合甲骨文云Oracle cloud VM实例等
很多云厂商默认关闭了 ssh 登录功能,像甲骨文 Oracle cloud 和谷歌 gcp,不过 gcp 还好,在元数据可设置全局 ssh 密钥,但是甲骨文云就很离谱,不但没有用户预设 ssh 密钥功能,而且配置 ssh 登录过程还麻烦的一比…,不过好在甲骨文提供了启动脚本 (cloud-init),即在创建 mv 实例第一次开机时执行启动脚本。
有了这个功能,我们就可以在首次开机配置 ssh 相关设置。
注意,甲骨文云管这叫 cloud-init,gcp 管这叫自动化,vultr 管这叫 Startup Script。各家的命名不一样,但是功能都是相同的,即开机启动脚本。
基本原理
原理就是在新实例开机的时候执行脚本,自动修改目录下 /etc/ssh/sshd_config 的这个 ssh 配置文件,修改 PermitRootLogin 和 PasswordAuthentication 参数,并且顺便修改 root 密码。
cloud-init 脚本内容
cloud-init 开机脚本在高级选项这一栏中。
debian / ubuntu
精准查找,适合 debian 与 ubuntu 系统。
其中 123456789
是你需要设置的 root 密码。
#!/bin/bash
echo root:123456789 |sudo chpasswd root
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config;
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config;
sudo service sshd restart
centos
精准查找,适合 centos 系统。
其中 123456789
是你需要设置的 root 密码。
#!/bin/bash
echo root:123456789 |sudo chpasswd root
sudo sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config;
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config;
sudo service sshd restart
通用脚本 (模糊查找)
模糊查找,理论上适合大部分 Linux 操作系统。
其中 123456789
是你需要设置的 root 密码。
#!/bin/bash
echo root:123456789 |sudo chpasswd root
sudo sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config;
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config;
sudo service sshd restart
作者:i_木木木木木
链接:https://www.jianshu.com/p/bf985a7458c0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。