Centos Network

最後更新: 2021-02-10

 

目錄

 


Disable IPv6

 

[方法1] 不用 reboot

sysctl -w net.ipv6.conf.all.disable_ipv6=1

sysctl -w net.ipv6.conf.default.disable_ipv6=1

[方法2]

/etc/sysconfig/grub

GRUB_CMDLINE_LINUX=" ... ipv6.disable=1"

grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

[方法3]

/etc/sysconfig/network-scripts/ifcfg-eth0

IPV6INIT=no

 


PEERDNS

 

 

DHCP without update DNS

BOOTPROTO=dhcp
# Do not overwrite /etc/resolv.conf
PEERDNS=no
DNS1=8.8.8.8
DNS2=8.8.4.4

PEERDNS=yes|no

yes

  This interface will modify your system's /etc/resolv.conf file entries
  to use the DNS servers provided by the remote system when a connection is established.

no

  The /etc/resolv.conf file will not be changed.

ifcfg-X Config File without update resolv.conf

在 "BOOTPROTO=none" 情況下, 當 "systemctl restart network" 時

系統會根據 ifcfg-X 內的 DOMAIN, DNS1, DNS2, ... 去 update resolv.conf

PEERDNS defaults to no unless DHCP is used, in which case it defaults to yes.

search datahunter.org
nameserver 192.168.123.1
nameserver 192.168.123.254

Remark

# 必定不影響 resolv.conf 的方法

chattr +i /etc/resolv.conf

 

 


Centos 6

 

Centos6 - NM_CONTROLLED

# The default for EL6 install both "NetworkManager" and "network" services

# If you want to use the older network service approach then NetworkManager

# prevent NetworkManager configuring this device

/etc/sysconfig/network

NM_CONTROLLED=no

Centos6 - Zero Configuration Network (ZEROCONF)

Default:

route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.88.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.88.1    0.0.0.0         UG    0      0        0 eth0

/etc/sysconfig/network

NOZEROCONF=yes

/etc/networks

default 0.0.0.0
loopback 127.0.0.0
link-local 169.254.0.0

Centos 6 - add route on startup

/etc/sysconfig/network-scripts/route-eth0

# default gateway
# default X.X.X.X dev interface

# Define a static route
X.X.X.X/X via X.X.X.X dev interface

Centos 6 - 設定 Interface (ifcfg-eth?)

設定檔在

/etc/sysconfig/network-scripts/ifcfg-eth?

 * 注意

Backup 時要開 Folder, 不可以有 ifcfg-eth0.bak file !!

因為 "systemctl restart network" 會將它當作 sub-interface

DHCP

/etc/sysconfig/network

# It is global configuration file

# It allows us to define whether we want networking

NETWORKING=yes

/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

# Non-root users are not allowed to control this device.
USERCTL=no

 

設定 Fix IP

DEVICE=eth?
ONBOOT=yes
BOOTPROTO=static            # 其他選項: none | bootp | dhcp | static
HWADDR=52:54:00:F1:60:01    # 當 NIC 的 hwaddr 與此值不同時 (ip link), 那 interface 會 up 唔到
IPADDR=
NETMASK=
NETWORKING=
HOSTNAME=
GATEWAY=

 

Centos 6 - Bridge

 

/etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0
TYPE=Bridge
IPADDR=192.168.1.250
NETMASK=255.255.255.0
NETWORK=192.168.1.0
GATEWAY=192.168.1.1
BROADCAST=192.168.1.255
DNS2=8.8.4.4
DNS1=8.8.8.8
ONBOOT=yes
BOOTPROTO=none
# to prevent NetworkManager from altering the file.
NM_CONTROLLED=no
DELAY=0

/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=ethX
TYPE=Ethernet
ONBOOT=yes
HWADDR=AA:BB:CC:DD:EE:FF
BOOTPROTO=none
NM_CONTROLLED=no
BRIDGE=br0

# 套用設定

service network restart

 * 注意 restart 後有機會 connect 唔到

 

Centos6 - Sub-Interface

 

DEVICE=eth0:1
BOOTPROTO=none
# "ONBOOT=YES" would pull up the parent interface even if that is configured to not come up on boot.
# 當 "ONPARENT=yes" 時, PARENT up 時, sub interface 才 up
ONPARENT=yes
IPADDR=192.168.0.2
NETMASK=255.255.255.0

 

Centos6 - DEFROUTE & Gateway & GATEWAYDEV

 

DEFROUTE

/etc/sysconfig/network-scripts/ifcfg-eth?

Assume that u have two network cards.

You have provided IP,subnet and gateway in both network card. Now system uses both cards to reach the destination.

When you put "DEFROUTE=yes" on one card then system always use card one on priority to reach every destination.

Gateway

/etc/sysconfig/network or /etc/sysconfig/network-scripts/ifcfg-eth?

Configuring Default Gateway

network scripts parse "GATEWAY" step (Last Win)

1. /etc/sysconfig/network

2. ifcfg files

The last GATEWAY directive to be read is used to compose a default route in the routing table

GATEWAYDEV  (Global Option)

/etc/sysconfig/network

more than one network interface is present with "GATEWAY"

GATEWAYDEV device 's directive will take precedence

"DEFROUTE=no" command in the ifcfg files for those interfaces which do not lead to the default gateway.

 


Other Opts

 

PEERROUTES

Whether the information for the routing table entry that defines the default gateway for the interface is obtained from the DHCP server.

IPV4_FAILURE_FATAL

Whether the device is disabled if IPv4 configuration fails.

 



Centos7

Centos7 - set IP

 

# 在基本設定上, "0" 不是必須的

IPADDR0=x.x.x.x

# PREFIX0, NETMASK0 2 擇其一
# NETMASK0=255.255.255.252
PREFIX0=29

# 如果 IPADDR 加了 "0", 那 GATEWAY 亦要加 "0"
GATEWAY0=x.x.x.x

DNS1=8.8.8.8
DNS2=8.8.4.4

 

Interface no IP

BOOTPROTO=none
#IPADDR=0.0.0.0
#NETMASK=255.255.255.255

 

Centos 7 Interface Dual IP

 

# 設定

ifcfg-eth0

....

IPADDR1=192.168.88.114
PREFIX1=24

IPADDR2=192.168.88.115
PREFIX2=24

# 在用到 IPADDR1 及 IPADDR2 的設定時, 那一定要在 GATEWAY 加 1 或 2
GATEWAY1=192.168.88.1

# 套用

service network restart

# Checking

ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:31:34:7c:04 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.114/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.88.115/24 brd 192.168.88.255 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:31ff:fe34:7c04/64 scope link
       valid_lft forever preferred_lft forever

 

Centos 7 subinterface 與 mulit-IP NIC

 

Subinterface

# 假設 Master NIC 係 ifcfg-eth0, 那就建立 ifcfg-eth0:115

ifcfg-eth0:115

DEVICE=eth0:115
ONBOOT=yes
BOOTPROTO="none"
IPADDR=192.168.88.115
PREFIX=24

runtime 測試

ifup eth0:115

ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:31:34:7c:04 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.114/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.88.115/24 brd 192.168.88.255 scope global secondary eth0:115
       valid_lft forever preferred_lft forever

全面測試

ifdown eth0:115

service network restart

mulit-IP NIC

ifcfg-ens192

TYPE=Ethernet
ONBOOT=yes
BOOTPROTO="none"
NAME=ens192
DEVICE=ens192

GATEWAY=192.168.88.1
IPADDR1=192.168.88.11
PREFIX1=24
IPADDR2=192.168.88.12
PREFIX2=24

 

Centos 7 bridge

 

yum -y install bridge-utils

/etc/sysconfig/network-scripts/ifcfg-brN

DEVICE=brN
TYPE=Bridge
NM_CONTROLLED=no
ONBOOT=yes

# prevent the bridge from waiting while it monitors traffic
DELAY=0

BOOTPROTO=static
IPADDR=192.168.88.100
PREFIX=24
GATEWAY=192.168.88.1
DNS0=192.168.88.1

/etc/sysconfig/network-scripts/ifcfg-ethX

# Optional
HWADDR=52:54:31:34:7C:04
DEVICE=ethX
TYPE=Ethernet
NM_CONTROLLED=no
BRIDGE=br0
ONBOOT=yes
BOOTPROTO=none

service network restart

 


Fix NIC Name

 

# rename the device using the ip command

/sbin/ip link set eth1 down
/sbin/ip link set eth1 name eth123
/sbin/ip link set eth123 up

# udev rule

/usr/lib/udev/rules.d/60-net.rules

ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="00:0c:29:e9:58:77", NAME="lan"

ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="00:0c:29:e9:58:81", NAME="wan"

ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="1", PROGRAM="/lib/udev/rename_device",
        RESULT=="?*", NAME="$result"

# kernel boot options

To restore the old naming convention,

Need to edit the /etc/default/grub file and add the following at the end of the variable(GRUB_CMDLINE_LINUX)

GRUB_CMDLINE_LINUX="... net.ifnames=0 biosdevname=0"

grub2-mkconfig -o /boot/grub2/grub.cfg

more info.

net.ifnames

1 => Network interfaces are renamed to give them predictable names when possible.

biosdevname Naming Convention

                                     Old            New 
Embedded network interface (LOM)     eth[0123…]     em[1234…]
PCI card network interface           eth[0123…]     p<slot>p<ethernet port>
Virtual function                     eth[0123…]     p<slot>p<ethernet port>_<virtual interface>

# For this feature to take effect, the biosdevname package must also be installed.

yum install biosdevname

Centos7 interface name

Centos7 會根據 /etc/sysconfig/network-scripts/ifcfg-wan 內與 "HWADDR=MAC" 相符的 "DEVICE=wan" 去 rename interface name

有關的 Rules

 - /usr/lib/udev/rules.d/60-net.rules

 - /usr/lib/udev/rules.d/71-biosdevname.rules

kernel parameter "biosdevname=0" was not given

 - /lib/udev/rules.d/75-net-description.rules

 - /usr/lib/udev/rules.d/80-net-name-slot.rules

kernel parameter "net.ifnames=0" was not given

 


Centos 8

 

Centos 8 沒有了以下 Package

  • bridge-utils
  • systemctl restart network

Restart Network

systemctl restart NetworkManager

/etc/sysconfig/network-scripts/ifcfg-lxcbr0 不可以有

NM_CONTROLLED=no

Apply New IP Setting

ifdown eth0; ifup eth0

OR

nmcli con down br0; nmcli con up br0

建立 bridge

nmcli con add type bridge ifname lxcbr2

Connection 'bridge-lxcbr2' (4c3c3768-514d-4694-8ec1-661748c74a1e) successfully added.

nmcli con modify bridge-lxcbr2 connection.id lxcbr2

nmcli con modify lxcbr2 bridge.stp no

# NIC 沒有 IP 是不能 up 的

nmcli con mod lxcbr2 ipv4.method disabled

nmcli con up lxcbr2

nmcli con sh [lxcbr2]

ip link show lxcbr2

Add NIC(eth1) to BR(br0)

nmcli con add type bridge-slave ifname eth1 master lxcbr2

Shows interfaces belonging to bridge

ip link | grep master

12: c1-eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master lxcbr0 state UP mode DEFAULT group default qlen 1000

OR

bridge link

12: c1-eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master lxcbr0 state forwarding priority 32 cost 2

詳見: https://datahunter.org/nmcli

 


 

 

 

 

 

Creative Commons license icon Creative Commons license icon