How to use this box with Vagrant
Step 1
Option 1: Create a Vagrantfile and initiate the box
vagrant init generic/centos9s --box-version 4.3.12Option 2: Open the Vagrantfile and replace the contents with the following
Vagrant.configure("2") do |config| config.vm.box = "generic/centos9s" config.vm.box_version = "4.3.12" endStep 2
Bring up your virtual machine
vagrant up
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "generic/centos9s"
config.vm.box_version = "4.3.12"
# 设置固定 IP
config.vm.network "private_network", ip: "192.168.56.101"
# 启用密码 SSH 登录(关键!)
config.vm.provision "enable_password_auth", type: "shell", run: "always", inline: <<-SHELL
if [ ! -f /etc/ssh/sshd_config.d/99-vagrant-password.conf ]; then
echo "PasswordAuthentication yes" | sudo tee /etc/ssh/sshd_config.d/99-vagrant-password.conf
sudo systemctl reload sshd
echo "[INFO] Password authentication enabled for SSH."
fi
SHELL
# 可选:VMware 或 VirtualBox 资源配置
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = 2
end
config.vm.provider "vmware_desktop" do |vmware|
vmware.memory = "2048"
vmware.cpus = 2
end
end
114.114.114.114和192.168.56.1
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "generic/centos9s"
config.vm.box_version = "4.3.12"
config.vm.network "private_network", ip: "192.168.56.100"
# 启用密码登录
config.vm.provision "enable_password_auth", type: "shell", run: "always", inline: <<-SHELL
if [ ! -f /etc/ssh/sshd_config.d/99-vagrant-password.conf ]; then
echo "PasswordAuthentication yes" | sudo tee /etc/ssh/sshd_config.d/99-vagrant-password.conf
sudo systemctl reload sshd
fi
SHELL
# 设置纯净 DNS(仅保留指定的两个)
config.vm.provision "set_static_dns", type: "shell", inline: <<-SHELL
CON_NAME=$(nmcli -t -f NAME,DEVICE con show --active | grep -E 'eth0|enp' | head -n1 | cut -d: -f1)
[ -z "$CON_NAME" ] && CON_NAME=$(nmcli -t -f NAME con show --active | head -n1)
if [ -n "$CON_NAME" ]; then
sudo nmcli con mod "$CON_NAME" ipv4.dns "114.114.114.114 192.168.56.1"
sudo nmcli con mod "$CON_NAME" ipv4.ignore-auto-dns yes
sudo nmcli con up "$CON_NAME" 2>/dev/null || true
fi
cat /etc/resolv.conf
SHELL
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = 2
end
config.vm.provider "vmware_desktop" do |vmware|
vmware.memory = "2048"
vmware.cpus = 2
end
end