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
相关推荐
a努力。17 小时前
国家电网Java面试被问:Spring Boot Starter 制作原理
java·spring boot·面试
一 乐17 小时前
酒店预约|基于springboot + vue酒店预约系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
老王熬夜敲代码18 小时前
解决IP不够用的问题
linux·网络·笔记
guslegend18 小时前
Tomact高级使用及原理剖析
java
Code blocks18 小时前
SpringBoot从0-1集成Minio对象存储
java·spring boot·后端
zly350018 小时前
linux查看正在运行的nginx的当前工作目录(webroot)
linux·运维·nginx
来自于狂人18 小时前
华为云Stack服务实例创建失败通用排查对照表(备考+生产故障定位必备)
服务器·数据库·华为云
故渊ZY18 小时前
MyBatis事务原理与实战指南
java·mybatis
QT 小鲜肉18 小时前
【Linux命令大全】001.文件管理之file命令(实操篇)
linux·运维·前端·网络·chrome·笔记
HTouying18 小时前
线程池【工具类】
java