InterServer Web Hosting and VPS

通过 Post-Installation Script(安装后自定义脚本)或 Cloud-Init Metadata 来提前预设ovh杜甫的 Root 密码或导入 SSH 公钥,

90次阅读
文章目录

可以通过 Post-Installation Script(安装后自定义脚本)或 Cloud-Init Metadata 来提前预设 Root 密码或导入 SSH 公钥,这样系统重装完成后您无需等待 OVH 发送邮件,就能使用您设置好的密码直接登录 。

 

最简单、直接的方法是使用 Post-Installation Script 框 。

 

方法一:使用 Post-Installation Script 设置 Root 密码(推荐)

在 Post-Installation Script 输入框中直接填写一段 Bash 脚本 。当系统初始化完成后,OVH 会在系统首次启动时自动执行该脚本 。

 

Bash

#!/bin/bash
# 1. 预设 root 密码为 your_password(请修改为您想设置的强密码)
echo "root:your_password" | chpasswd

# 2. 允许 root 账户通过密码登录 SSH
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config

# 3. 重启 SSH 服务生效 
systemctl restart ssh || service ssh restart

验证方式: 服务器重装完成后,使用终端尝试运行 ssh root@< 服务器 IP> 并输入您在脚本中填写的 your_password,能够成功登录即代表脚本生效。

方法二:使用 Post-Installation Script 写入 SSH 公钥

如果您不想使用明文密码,也可以通过该脚本直接将您的 SSH 公钥写入系统的 authorized_keys 中:

Bash

#!/bin/bash
mkdir -p /root/.ssh
chmod 700 /root/.ssh
# 将下方的 ssh-rsa ... 替换为您的公钥内容 
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ..." > /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys

关于 Cloud-init Config Drive Metadata 选项

如果在安装支持 cloud-init 的通用镜像时,可以通过下方 Cloud-init Config Drive Metadata 填入 Key/Value 对 :

 

  • Key: password

  • Value: your_password

但对于第三方 raw/img 自定义镜像,Post-Installation Script 的执行成功率更高、兼容性更好 。

 

正文完
 0
InterServer Web Hosting and VPS