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
相关推荐
Chase_______2 小时前
【Java杂项】Arrays.asList、List.of 和 new ArrayList:集合可变性避坑
java·windows·list
发际线向北2 小时前
0x07 深入了解JVM虚拟机(JVM异常处理)
java
Seven972 小时前
每个线程只管自己的变量,性能却不如单线程?问题出在缓存行
java
Java开发追求者2 小时前
oracle解决服务器正常使用但是互联网无法使用问题
运维·服务器·ora-12154·windows监听问题·oracle互联网无法访问
2601_961845152 小时前
2026四级作文预测题|英语四级写作押题+提纲PDF
java·c语言·数据库·c++·python·pdf·php
日取其半万世不竭2 小时前
Palworld《幻兽帕鲁》 服务器搜不到怎么办?端口和防火墙排查清单
运维·服务器
大树882 小时前
一滴冷却液,烧掉2000万算力
大数据·运维·服务器·人工智能
xhtdj2 小时前
DuckDB Quack基于 HTTP的客户端 / 服务器协议面向多用户分析
服务器·网络协议·http
日取其半万世不竭2 小时前
Rust《腐蚀》 服务器低成本怎么开?配置、端口和存档避坑
服务器·开发语言·rust
用户531397318172 小时前
「踩坑实录」原来的SQL索引自动优化失败了,线上数据库差点被打挂
java·后端