CentOS
官網文件:14.1. Network Configuration Files
Path:
/etc/sysconfig/network-scripts/依照每張網卡擁有各自設定檔:
ifcfg-eth0
ifcfg-eth1新增流程:
如上格式新增完成一張網卡設定檔後,啟用該編號
ifup eth1設定範例:
DEVICE=eth1
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.0.100
NETMASK=255.255.255.0Centos 預設
ONBOOT是關閉的,開啟才能開機自動啟用網卡
Debian
一般來說/etc/resolv.conf會被其他來源覆寫,改法可以參考往下 Ubuntu 的說明。
Vanilla Debian 直接使用resolv.conf,在 Proxmox Debian 上有遇過此狀況。
Ubuntu
File Path:
/etc/network/interfaces不同於CentOS,所有網卡設定皆於上述設定檔
新增流程:
編輯上述設定檔後,重新啟動全部網路卡:
sudo /etc/init.d/networking restart或針對單一網卡進行操作:
sudo ifdown eth0 #關閉 eth0 這個裝置
sudo ifup eth0 #啟用 eth0 並從 interfaces 讀取設定檔設定規則:
inet Type
iface eth0 inet inet type
Description
loopback
回送,預設為127.0.0.1
dhcp
DHCP模式
static
固定IP模式
DNS
Ubuntu的DNS若要永久生效則設定在/etc/networking/interfaces的網卡中:
iface ...
    dns-nameservers 8.8.8.8 168.95.1.1設定範例:
# The primary network interface
auto eth0
iface eth0 inet static
    address 123.123.123.123/24
    gateway 123.123.123.1
    dns-nameservers 8.8.8.8 168.95.1.1Up & Down (Optional):
    up   ip addr add 192.168.0.100/17 dev eth0 label eth0:1
    down ip addr del 192.168.0.100/17 dev eth0 label eth0:1Ubuntu 18 - netplan
設定檔位於/etc/netplan/{config}.yaml:
network:
    ethernets:
        ens192:
            addresses: [192.168.1.100/24]
            gateway4: 192.168.1.1
            nameservers:
                addresses: [8.8.8.8,8.8.4.4]
            dhcp4: no
        ens224:
            dhcp4: true
    version: 2設定完後使用try檢查:
sudo netplan try確認沒問題即可Apply:
sudo netplan apply