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
相关推荐
咩咩啃树皮10 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
爱写代码的森10 小时前
鸿蒙三方库 | harmony-utils之ImageUtil图片保存到本地详解
服务器·华为·harmonyos·鸿蒙·huawei
鱟鲥鳚10 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
大模型码小白12 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司12 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地13 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun31415913 小时前
TCP超时重传机制是为了解决什么问题?
java
HLC++14 小时前
Linux的进程间通信
android·linux·服务器
华清远见IT开放实验室15 小时前
实验室建设案例 | 石家庄科技信息职业学院嵌入式实验室——从底层硬件到系统应用,一所应用型高校的嵌入式人才培养这样落地
linux·arm开发·stm32·嵌入式硬件·高校·实验室建设
白露与泡影15 小时前
Arthas 实战指南:从方法耗时定位到 JVM 变量热修改
服务器·jvm·c#