Ubuntu server批量添加IP的方法

Netplan replaced ifupdown as the default configuration utility starting with Ubuntu 17.10 Artful.

https://ubuntu.com/blog/ubuntu-bionic-netplan

ubuntu 从 17.10 开始,改成 netplan 方式网络,配置写在 /etc/netplan/01-netcfg.yaml 或者类似名称的 yaml 文件里。

ubuntu 17.10以后的版本配置方法:

#写入到 /etc/netplan/01-netcfg.yaml ,或者vim打开文件手动调整
echo "      addresses: " >> /etc/netplan/01-netcfg.yaml
for ((i=1;i<=64;i++))
do
echo "        - 1.2.3.$i/29" >> /etc/netplan/01-netcfg.yaml
done
echo "      gateway4: 1.2.3.4" >> /etc/netplan/01-netcfg.yaml

#在vim中使用s/1/a/进行新旧ip替换,也可使用sed
#应用
netplan apply

ubuntu 16.04 14.04 脚本

# !/bin/bash
for ((i=1;i<=64;i=i+1))
do
echo "auto eth0:$i">>/etc/network/interfaces
echo "iface eth0:$i inet static">>/etc/network/interfaces
let j=$i+5
echo "address 1.2.3.$j">>/etc/network/interfaces
echo "netmask 255.255.255.248">>/etc/network/interfaces
echo "gateway 1.2.3.4">>/etc/network/interfaces
echo " ">>/etc/network/interfaces
done
# end

部分内容来源:

https://www.myfreax.com/how-to-configure-static-ip-address-on-ubuntu-20-04/
https://www.raksmart.com/45.html