shell
#!/bin/bash
# 设置静态IP地址的脚本
# 网卡名称
interface="ens33"
# 静态IP地址
ip_address="192.168.225.2"
# 子网掩码长度
subnet_mask_len="24"
# 默认网关
default_gateway="192.168.225.1"
# DNS服务器
dns_servers="[114.114.114.114, 8.8.8.8]"
# 原始网络配置文件名
netplan_filename="00-installer-config.yaml"
# 备份原始网络配置文件
sudo cp /etc/netplan/$netplan_filename /etc/netplan/$netplan_filename.bak
# 创建新的网络配置文件
sudo tee /etc/netplan/$$netplan_filename.yaml > /dev/null <<EOL
network:
renderer: networkd
ethernets:
ens33:
addresses:
- $ip_address/$subnet_mask_len
nameservers:
addresses: $dns_servers
routes:
- to: default
via: $default_gateway
version: 2
EOL
# 应用配置
sudo netplan apply
echo "静态IP地址已成功设置为 $ip_address"
具体设置过程可参考:
- Linux中国:https://linux.cn/article-15181-1.html
- VMware固定虚拟机IP:https://m.jb51.net/article/230738.htm