为一个 NIC 分配多个静态 IP 地址

为一个 NIC 分配多个静态 IP 地址

我需要帮助,在一个 NIC 上为 2 个不同的 IP(例如 192.168.xx.xx/24 和 172.16.xx.xx/24)分配多个不同的静态 IP。有没有人知道如何在 Ubuntu Server 22.04 上进行配置。

我尝试在 etc/netplan/00-install 中创建静态 IP

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses: 
      - 192.168.1.212/24 
      nameservers: 
        addresses: [8.8.8.8, 8.8.4.4]
      routes:
      - to: default 
        via: 192.168.1.2

答案1

您可以使用nmcli它来分配 IP 地址。

对于多个接口

nmcli con add con-name static1 type ethernet ipv4.addresses <IP_ADDRESS>/<SUBNET_CIDR> ipv4.method manual ipv4.gateway <GATEWAY_IP> connection.autoconnect yes ifname <INTERFACE> ipv4.dns "8.8.8.8 8.8.4.4" 

route add -net <Network> netmask <subnet> dev <interface>
route add default gw <gateway>

已填充数据的示例...

nmcli con add con-name static1 type ethernet ipv4.addresses 192.168.0.101/24 ipv4.method manual ifname eth0 ipv4.gateway 192.168.0.1 ipv4.dns "8.8.8.8 8.8.4.4" connection.autoconnect yes

nmcli con add con-name static2 type ethernet ipv4.addresses 172.16.0.101/24 ipv4.method manual ifname eth1 ipv4.gateway 172.16.0.1 ipv4.dns "8.8.8.8 8.8.4.4" connection.autoconnect yes

route add -net 192.168.0.0 netmask 255.255.255.0 dev eth1
route add default gw 192.168.0.1
route add -net 172.16.0.0 netmask 255.255.255.0 dev eth0

对于单个接口

# For routes nmcli has a ipv4.routes option that is "<IP_NETWORK>/<CIDR> <GATEWAY>, <IP_NETWORK_2> <GATEWAY_2>,..."
nmcli con add con-name static2 type ethernet ipv4.addresses 172.16.0.101/24,192.168.0.101/24 ipv4.method manual ifname eth0 ipv4.gateway 172.16.0.1 ipv4.dns "8.8.8.8 8.8.4.4" connection.autoconnect yes

route add -net 192.168.0.0 netmask 255.255.255.0 dev eth0
route add default gw 192.168.0.1
route add -net 172.16.0.0 netmask 255.255.255.0 dev eth0

所示部分物品的文档:

使用路由设置路线 https://www.techrepublic.com/article/understand-the-basics-of-linux-routing/

基本 nmcli 文档 https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-configuring_ip_networking_with_nmcli

与使用 nmcli 的 ipv4.routes 相关

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-configuring_static_routes_using_nmcli

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/configuring-static-routes_configuring-and-managing-networking

相关内容