Network settings in Linux
Linux systems have powerful network functions, such as routing, bonding, and so on.
I. Basic network configuration
Once the Linux system installation is complete, the network card configuration file is automatically generated, under/etc/sysconfig/network-scripts/, with the file name Ifcfg-eth0. If you do not have this file after adding a new network card, you can create a corresponding file yourself.
Ifcfg-eth0 network card file format:
1. Configuration options:
HWADDR:对应的设备MAC地址BOOTPROTO:激活此设备时使用的地址配置协议,常用的dhcp, static, none,bootpNM_CONTROLLED:NM是NetworkManager的简写,此网卡是否接受NM控制;建议CentOS6为“noONBOOT:在系统引导时是否激活此设备TYPE:接口类型;常见有的Ethernet, BridgeUUID:设备的惟一标识GATEWAY: 默认网关DNS1:第一个DNS服务器指向DNS2:第二个DNS服务器指向USERCTL:普通用户是否可控制此设备PEERDNS:如果BOOTPROTO的值为“dhcp”,是否允许dhcp server分配的dns服务器指向信息直接覆盖至/etc/resolv.conf文件中*IPADDR:指明IP地址*NETMASK:子网掩码*DEVICE:此配置文件对应的设备
- One of the last three configurations with an * number is required, and others need to be configured according to the actual situation.
2. Restart the network service for the configuration file to take effect
centos 6:service NetworkManager stop NetworkManager会和network冲突,所以建议关闭service network restart 重启network服务centos 7:systemctl restart network
Two. Routing function configuration
In actual production we can also configure the Linux system as a router.
As shown, a total of 5 hosts, the middle R1,r2,r3 three as a router to use. First we configure the relevant IP addresses for 5 hosts.
1. Turn on the routing feature
To see if the routing feature is turned on:
Cat/proc/sys/net/ipv4/ip_forward 1 for open, 0 for non-open
Open Routing function
Echo 1 >/proc/sys/net/ipv4/ip_forward This modification of the kernel file is temporary and the modification takes effect, but it will expire after the reboot.
Writing related files
/etc/sysctl.conf file, modify Net.ipv4.ip_forward = 1
2. Adding static routes
Route configuration using the route command
Route
route add/del [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]add 添加路由del 删除路由-host 配置为主机路由-net 配置为网段路由default 默认路由target 目标地址或目标网段gw 后面跟上网关的地址dev 后面跟上路由出口的网卡设备名
First, we configure the gateway for two hosts, or add a default route. Take host A as an example:
Route add default 192.168.17.3 dev eth0
Static routes are then configured based on the network specifics. Taking R1 as an example
R1:
Route add-net 192.168.17.0/24 GW 192.168.27.2
Route add-net 192.168.87.0/24 GW 192.168.37.3
This completes the configuration of the routing functionality, but these routes represent temporary and we need to write to the file.
- 1. Create the corresponding network card configuration file, under the/etc/sysconfig/network-scripts/folder, the name is Route-iface IFACE is the specific name of the network card
- 2. The file content format is: 192.168.17.0/24 via 192.168.27.2
- 3. Each NIC corresponds to a different file and its corresponding route
The system firewall defaults to prohibit forward forwarding, so you can execute the following command to clear the firewall settings when you experiment
Iptables-f
This way, the routing table is not lost because of a reboot.
Three. Bonding function Configuration
With bonding, two NICs are bound to the same IP address for high availability or load balancing. After binding, the two NICs will be modified to the same MAC address.
3 common modes of operation in 1.Bonding
Mode 0 (BALANCE-RR) sends packets from beginning to end on each NIC.
Mode 1 (active-backup) only one network card is working, the standby network card will be activated when there is a problem with the work card
Mode 3 (broadcast) transmits all messages on all network cards
2.Bonding Configuration
Here, I am based on the eth0, eth1 two network card.
First, create the bond0 configuration file Ifcfg-bond0, under/etc/sysconfig/network-scripts/.
Ifcfg-bond0 file:
Device=bond0
Bootproto=none
bonding-opts= "miimon=100 mode=1" mode is the value of the pattern, miion indicates the query interval between two network cards
ipaddr=192.168.37.100
Prefix=24
Configuration of the Nic file, take ifcfg-eth1 as an example
Device=eth1 the corresponding device name
The bond that master=bond0 belongs to
Slave=yes
3. Restart the service
Service Network Restart CentOS 6
Systemctl Restart Network CentOS 7
Sometimes restarting the service does not make the bonding effective, this time you can unload, load the corresponding function module to enable the service to take effect.
Rmmode Bonding Uninstall Bonding
Modprobe Bonding Reload Bonding
View Bond Status
Cat/proc/net/bonding/bond0
4. Delete Bonding
First Disable BOND0
Ifconfig bond0 Down
Remove the corresponding function module
Rmmod Bonding
Finally, the corresponding configuration file is deleted.
Four. The network adapter in CentOS 7 is traditionally named
The CentOS 7 system uses a new name for the network card, which is named after the NIC's nature. Sometimes because of personal habits, we can modify the traditional naming method.
(1) Editing the/etc/default/grub configuration file
grub_cmdline_linux= "RHGB quiet" quiet after adding net.ifnames=0
Or: Modify/boot/grub2/grub.cfg
(2) generate its configuration file for Grub2
Grub2-mkconfig-o/etc/grub2.cfg
(3) Rebooting the system
Reboot
Common network configurations in Linux