很多云廠商默認(rèn)關(guān)閉了 ssh 登錄功能迈螟,像甲骨文 Oracle cloud 和谷歌 gcp,不過 gcp 還好尔崔,在元數(shù)據(jù)可設(shè)置全局 ssh 密鑰答毫,但是甲骨文云就很離譜,不但沒有用戶預(yù)設(shè) ssh 密鑰功能季春,而且配置 ssh 登錄過程還麻煩的一比…洗搂,不過好在甲骨文提供了啟動(dòng)腳本 (cloud-init),即在創(chuàng)建 mv 實(shí)例第一次開機(jī)時(shí)執(zhí)行啟動(dòng)腳本。
有了這個(gè)功能蚕脏,我們就可以在首次開機(jī)配置 ssh 相關(guān)設(shè)置侦副。
注意,甲骨文云管這叫 cloud-init驼鞭,gcp 管這叫自動(dòng)化秦驯,vultr 管這叫 Startup Script。各家的命名不一樣挣棕,但是功能都是相同的译隘,即開機(jī)啟動(dòng)腳本。
基本原理
原理就是在新實(shí)例開機(jī)的時(shí)候執(zhí)行腳本洛心,自動(dòng)修改目錄下 /etc/ssh/sshd_config 的這個(gè) ssh 配置文件固耘,修改 PermitRootLogin 和 PasswordAuthentication 參數(shù),并且順便修改 root 密碼词身。
cloud-init 腳本內(nèi)容
cloud-init 開機(jī)腳本在高級(jí)選項(xiàng)這一欄中厅目。
debian / ubuntu
精準(zhǔn)查找,適合 debian 與 ubuntu 系統(tǒng)法严。
其中 123456789
是你需要設(shè)置的 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
精準(zhǔn)查找,適合 centos 系統(tǒng)深啤。
其中 123456789
是你需要設(shè)置的 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 操作系統(tǒng)溯街。
其中 123456789
是你需要設(shè)置的 root 密碼诱桂。
#!/bin/bash
echo root:123456789 |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 sshd restart
轉(zhuǎn)自:為vps配置ssh密碼登錄,適合甲骨文云Oracle cloud等 | 優(yōu)速VPS測評(píng) (vpsur.com)