参数
注:这些参数值不区分大小写,不区分单引号和双引号,甚至可以不用引号。
- TYPE:配置文件接口类型。在/etc/sysconfig/network-scripts/目录有多种网络配置文件,有 Ethernet 、IPsec 等类型,网络接口类型为 Ethernet。
- DEVICE:网络接口名称
- BOOTPROTO:系统启动地址协议
- none:不使用启动地址协议
- bootp:BOOTP协议
- dhcp:DHCP动态地址协议
- static:静态地址协议
- ONBOOT:系统启动时是否激活
- yes:系统启动时激活该网络接口
- no:系统启动时不激活该网络接口
- IPADDR:IP地址
- NETMASK:子网掩码
- GATEWAY:网关地址
- BROADCAST:广播地址
- HWADDR/MACADDR:MAC地址。只需设置其中一个,同时设置时不能相互冲突。
- PEERDNS:是否指定DNS。如果使用DHCP协议,默认为yes。
- yes:如果DNS设置,修改/etc/resolv.conf中的DNS
- no:不修改/etc/resolv.conf中的DNS
- DNS{1, 2}:DNS地址。当PEERDNS为yes时会被写入/etc/resolv.conf中。
- NM_CONTROLLED:是否由Network Manager控制该网络接口。修改保存后立即生效,无需重启。被其坑过几次,建议一般设为no。
- yes:由Network Manager控制
- no:不由Network Manager控制
- USERCTL:用户权限控制
- yes:非root用户允许控制该网络接口
- no:非root用户不允许控制该网络接口
- IPV6INIT:是否执行IPv6
- IPV6ADDR:IPv6地址/前缀长度
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| [root@localhost network-scripts] 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:3d:bc:2d brd ff:ff:ff:ff:ff:ff inet 192.168.0.130/24 brd 192.168.0.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe3d:bc2d/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:3f:8b:8f brd ff:ff:ff:ff:ff:ff inet 192.168.66.130/24 brd 192.168.10.255 scope global noprefixroute ens37 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe3f:8b8f/64 scope link valid_lft forever preferred_lft forever 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:77:ee:04 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000 link/ether 52:54:00:77:ee:04 brd ff:ff:ff:ff:ff:ff
|
两块网卡,都使用静态 IP
- ens33:内网(DEFROUTE=no)
- ens37:外网(DEFROUTE=yes,DNS1=192.168.66.2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| [root@localhost ~] [root@localhost network-scripts] total 256 -rw-r--r--. 1 root root 392 Apr 29 01:05 ifcfg-ens33 -rw-r--r--. 1 root root 413 Apr 29 01:08 ifcfg-ens37 -rw-r--r--. 1 root root 254 May 22 2020 ifcfg-lo lrwxrwxrwx. 1 root root 24 Apr 28 23:54 ifdown -> ../../../usr/sbin/ifdown -rwxr-xr-x. 1 root root 654 May 22 2020 ifdown-bnep -rwxr-xr-x. 1 root root 6532 May 22 2020 ifdown-eth -rwxr-xr-x. 1 root root 6190 Oct 14 2020 ifdown-ib -rwxr-xr-x. 1 root root 781 May 22 2020 ifdown-ippp -rwxr-xr-x. 1 root root 4540 May 22 2020 ifdown-ipv6 lrwxrwxrwx. 1 root root 11 Apr 28 23:54 ifdown-isdn -> ifdown-ippp -rwxr-xr-x. 1 root root 2130 May 22 2020 ifdown-post -rwxr-xr-x. 1 root root 1068 May 22 2020 ifdown-ppp -rwxr-xr-x. 1 root root 870 May 22 2020 ifdown-routes -rwxr-xr-x. 1 root root 1456 May 22 2020 ifdown-sit -rwxr-xr-x. 1 root root 1621 Dec 9 2018 ifdown-Team -rwxr-xr-x. 1 root root 1556 Dec 9 2018 ifdown-TeamPort -rwxr-xr-x. 1 root root 1462 May 22 2020 ifdown-tunnel lrwxrwxrwx. 1 root root 22 Apr 28 23:54 ifup -> ../../../usr/sbin/ifup -rwxr-xr-x. 1 root root 12415 May 22 2020 ifup-aliases -rwxr-xr-x. 1 root root 910 May 22 2020 ifup-bnep -rwxr-xr-x. 1 root root 13758 May 22 2020 ifup-eth -rwxr-xr-x. 1 root root 10357 Oct 14 2020 ifup-ib -rwxr-xr-x. 1 root root 12075 May 22 2020 ifup-ippp -rwxr-xr-x. 1 root root 11893 May 22 2020 ifup-ipv6 lrwxrwxrwx. 1 root root 9 Apr 28 23:54 ifup-isdn -> ifup-ippp -rwxr-xr-x. 1 root root 650 May 22 2020 ifup-plip -rwxr-xr-x. 1 root root 1064 May 22 2020 ifup-plusb -rwxr-xr-x. 1 root root 4997 May 22 2020 ifup-post -rwxr-xr-x. 1 root root 4154 May 22 2020 ifup-ppp -rwxr-xr-x. 1 root root 2001 May 22 2020 ifup-routes -rwxr-xr-x. 1 root root 3303 May 22 2020 ifup-sit -rwxr-xr-x. 1 root root 1755 Dec 9 2018 ifup-Team -rwxr-xr-x. 1 root root 1876 Dec 9 2018 ifup-TeamPort -rwxr-xr-x. 1 root root 2780 May 22 2020 ifup-tunnel -rwxr-xr-x. 1 root root 1836 May 22 2020 ifup-wireless -rwxr-xr-x. 1 root root 5419 May 22 2020 init.ipv6-global -rw-r--r--. 1 root root 20678 May 22 2020 network-functions -rw-r--r--. 1 root root 30988 May 22 2020 network-functions-ipv6
|
ifcfg-ens33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| HWADDR=00:50:56:3D:BC:2D TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=192.168.0.130 PREFIX=24 GATEWAY=192.168.0.1 DEFROUTE=no PEERDNS=no PEERROUTES=no IPV4_FAILURE_FATAL=no IPV6INIT=no IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=a6580d4c-11b0-310b-946b-31ef503f326e ONBOOT=yes AUTOCONNECT_PRIORITY=-999
|
ifcfg-ens37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| HWADDR=00:50:56:3F:8B:8F TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=no IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens37 UUID=45bffbcf-e3ce-3bcf-9a30-73f3ceec61e7 ONBOOT=yes AUTOCONNECT_PRIORITY=-999 IPADDR=192.168.66.130 PREFIX=24 GATEWAY=192.168.66.2 PEERDNS=no PEERROUTES=no DNS1=192.168.66.2
|
重启网络设置
1 2
| [root@localhost network-scripts] Restarting network (via systemctl): [ OK ]
|
查看 ip 和 route
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| [root@localhost network-scripts] 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:3d:bc:2d brd ff:ff:ff:ff:ff:ff inet 192.168.0.130/24 brd 192.168.0.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe3d:bc2d/64 scope link valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:3f:8b:8f brd ff:ff:ff:ff:ff:ff inet 192.168.66.130/24 brd 192.168.66.255 scope global noprefixroute ens37 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe3f:8b8f/64 scope link valid_lft forever preferred_lft forever 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:77:ee:04 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000 link/ether 52:54:00:77:ee:04 brd ff:ff:ff:ff:ff:ff [root@localhost network-scripts] default via 192.168.66.2 dev ens37 proto static metric 101 192.168.0.0/24 dev ens33 proto kernel scope link src 192.168.0.130 metric 100 192.168.66.0/24 dev ens37 proto kernel scope link src 192.168.66.130 metric 101 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
|
代理