vagrant file 设置固定IP并允许密码登录

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.12

Option 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"
end
Step 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
相关推荐
learning-striving13 小时前
华为云欧拉操作系统的服务器实例中手工部署 Docker
linux·运维·服务器·docker·容器·华为云
戴西软件13 小时前
戴西软件入选2026年安徽省制造业数智化转型服务商名单
java·大数据·服务器·前端·人工智能
爱棋笑谦13 小时前
springboot—数据源相关配置
java·spring boot·spring
小此方13 小时前
Re:Linux系统篇(十五)工具篇 ·六:GDB 调试从底层逻辑到高阶实战
linux·运维·服务器·chrome
sulikey20 小时前
Linux ext2文件系统结构
linux·操作系统·文件系统·linux文件系统·ext2·ext2文件系统
白菜欣21 小时前
Linux — 进程控制
android·linux·运维
budingxiaomoli1 天前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫1 天前
Springboot SSO系统设计文档
java·spring boot·后端
皮卡狮1 天前
Linux开发专属工具
linux
未若君雅裁1 天前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql