前言
实现批量创建多网络,更改主机名称,hosts解析
初始化网卡,主机名称,hosts解析,重启网卡
- 我的主机六个网卡,使用的有四个网卡,以下一键创建和初始化
- 主机名称我是以硬件的SN号最为主机的名称,实际环境看主机的需求进行更改
- 变量host_name_sn获取主机SN号
- 变量vmbr_IP获取vmbr0 IP地址
- 变量vmbr_gateway获取vmbr0 网关地址
- 变量vmbr_bridge桥接的实际网卡接口名称
- 桥接网卡名称定义必须要以vmbr0.100这样,参考这里,不要自定名称,不支持哦
- 其它参考官网
- Proxmox ve 中文参考文档
vim /proxmox_network_hostname.sh
#!/bin/bash
# -*- coding: utf-8 -*-
# Author: make.han
# Email: CIASM@CIASM
# Date: 2024/04/16
# pve The NIC was initially configured, the host name changed, and host issues changed
echo "backup network"
mv /etc/network/interfaces /etc/network/interfaces.bak
host_name_sn=`dmidecode -s system-serial-number`
vmbr_IP=`ip addr show vmbr0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
vmbr_gateway=`ip route show | grep default | awk '{print $3}'`
vmbr_bridge=`brctl show vmbr0 | awk 'NR==2{print $4}'`
echo "Example Initialize the default NIC "
cat <<EOF>>/etc/network/interfaces
# network interface settings; autogenerated
# Please do NOT modify this file directly, unless you know what
# you're doing.
#
# If you want to manage parts of the network configuration manually,
# please utilize the 'source' or 'source-directory' directives to do
# so.
# PVE will preserve these directives, but will NOT read its network
# configuration from sourced files, so do not attempt to move any of
# the PVE managed interfaces into external files!
# restrat netwrok service networking restart or systemctl restart networking.service or ifreload -a
auto lo
iface lo inet loopback
auto vmbr0
auto $vmbr_bridge
iface $vmbr_bridge inet manual
iface vmbr0 inet static
address $vmbr_IP/24
gateway $vmbr_gateway
bridge-ports $vmbr_bridge
bridge-stp off
bridge-fd 0
auto vmbr1
auto eno8403
iface eno8403 inet dhcp
iface vmbr1 inet dhcp
bridge-ports eno8403
bridge-stp off
bridge-fd 0
auto vmbr2
auto ens1f0
iface ens1f0 inet manual
iface vmbr2 inet static
address 192.168.3.101/24
bridge-ports ens1f0
bridge-stp off
bridge-fd 0
auto vmbr3
auto ens1f1
iface ens1f1 inet manual
iface vmbr3 inet static
address 192.168.15.2/24
bridge-ports ens1f1
bridge-stp off
bridge-fd 0
iface ens1f2 inet manual
iface ens1f3 inet manual
source /etc/network/interfaces.d/*
EOF
echo "hostname change"
echo > /etc/hostname
echo "$host_name_sn" > /etc/hostname
echo "hosts change"
sed -i '2d' /etc/hosts
sed -i "2i\\$vmbr_IP $host_name_sn.8.8.8.8 $host_name_sn" /etc/hosts
echo "issues change"
sed -i '/https:\/\//d' /etc/issue
sed -i "7i\ https://$vmbr_IP:8006/" /etc/issue
echo "change postfix"
sed -i "s/myhostname=.*/myhostname=$host_name_sn/" /etc/postfix/main.cf
echo "restart network"
#service networking restart
systemctl restart networking.service
执行硬件初始化
bash /proxmox_network_hostname.sh