This article is the best practice of Linux. it aims to show Chinese cabbage how to configure the Linux network. Linux systems have a large share in the server market, especially when the Internet is connected, 11.3 Linux network configuration
Linux systems occupy a large share of servers. to use computing, you must first understand the network configuration. This section describes the network configuration of Linux systems.
11.3.1 Linux network configuration file
Linux network configuration files vary depending on the directory names of different releases, but they are similar, mainly including the following directories or files.
(1)/etc/sysconfig/network: the main function is to modify the host name and whether to start the network.
(2)/etc/sysconfig/network-scrips/ifcfg-ethN: the file that sets Nic parameters, such as IP address, subnet mask, broadcast address, and Gateway. N is a number, and the name of the 1st Nic is a ifcfg-eth0. 2nd for the ifcfg-eth1, and so on.
(3)/etc/resolv. conf: This file sets DNS-related information for resolving domain names to IP addresses.
(4)/etc/hosts: host name corresponding to the computer IP address or IP address corresponding to the domain name, by setting/etc/nsswitch. the options in conf can be DNS resolution priority or local settings priority.
(5)/etc/nsswitch. conf (name service switch configuration, name service switch configuration): specifies the channels through which and the order in which specific types of information are queried.
11.3.2 configure the IP address of the Linux system
To set the IP address of the host, you can directly set it through terminal commands. to set the IP address to take effect after the system is restarted, you can set the corresponding network interface file, as shown in Example 11-18.
[Example 11-11]
[Root @ CentOSnetwork-scripts] # cat ifcfg-eth0
DEVICE = eth0
HWADDR = 00: 0C: 29: 7F: 08: 9D
ONBOOT = yes
BOOTPROTO = static
BROADCAST = 192.168.3.255
IPADDR = 192.168.3.100
NETMASK = 255.255.255.0
Table 11.7 lists the meanings of each field.
Table 11.7 network interface settings
Parameters |
Description |
DEVICE |
Device name. The name is 1st NICs, and the corresponding network interface is eth0. |
HWADDR |
MAC address of the NIC |
ONBOOT |
Whether to set this network interface when the system starts |
BOOTPROTO |
Dynamic IP address or static IP address |
BROADCAST |
Broadcast address |
IPADDR |
IP address |
NETMASK |
Subnet mask |
After setting the ifcfg-eth0 file, you need to restart the network service to take effect, restart the ifconfig to check whether the settings take effect:
[Root @ CentOSnetwork-scripts] # service network restart
You can set multiple IP addresses for the same network interface, as shown in Example 11-12.
[Example 11-12]
[Root @ CentOS ~] # Ifconfig eth0: 5 192.168.3.105 netmask 255.255.255.0 up
[Root @ CentOSnetwork-scripts] # ifconfig
Eth0 Link encap: Ethernet HWaddr00: 0C: 29: 7F: 08: 9D
Inet addr: 192.168.3.100 Bcast: 192.168.3.255 Mask: 255.255.255.0
Inet6 addr: fe80: 20c: 29ff: fe7f: 89d/64 Scope: Link
Upbroadcast running multicast mtu: 1500 Metric: 1
RXpackets: 27400 errors: 0 dropped: 0 overruns: 0 frame: 0
TXpackets: 28086 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 fig: 1000
RXbytes: 2375573 (2.2 MiB) TXbytes: 12120151 (11.5 MiB)
Eth0: 5 Link encap: Ethernet HWaddr00: 0C: 29: 7F: 08: 9D
Inet addr: 192.168.3.105 Bcast: 192.168.3.255 Mask: 255.255.255.0
Upbroadcast running multicast mtu: 1500 Metric: 1
To restart the server, add the command to the/etc/rc. d/rc. local file.
11.3.3 set host name
The host name identifies a computer on the network. you can use the hostname command to set the host name. The host name can be set arbitrarily in the case of a single machine, as shown in the following command. after you log on again, you will find that the host name has changed.
[Root @ CentOSnetwork-scripts] # hostname mylinux
To modify the host name that takes effect after restart, you can modify the HOSTNAME line in the/etc/sysconfig/network file. See Example 11-13.
[Example 11-13]
[Root @ mylinux ~] # Cat/etc/sysconfig/network
NETWORKING = yes
HOSTNAME = mylinux
11.3.4 set the default gateway
After the IP address is set, if you want to access other subnets or the Internet, you still need to set the route. we will not introduce it here. here we use the method of setting the default gateway. In Linux, there are two ways to set the default gateway:
(1) The 1st method is to directly use the route command. before setting the default gateway, use the route-n command to view the route table. Run the following command to set the Gateway.
[Root @ CenOS/] # route add default gw 192.168.1.254
(2) add the following fields to the/etc/sysconfig/network file:
GATEWAY = 192.168.10.254
Similarly, as long as the script file is changed, you must restart the network service to make the settings take effect. you can execute the following command:
[Root @ CentOS/] #/etc/rc. d/init. d/network restart
For the 1st methods, if you do not want to execute the route command every time you start the system, you should write the command to be executed into the/etc/rc. d/rc. local file.
11.3.5 configure the DNS server
To set the DNS server, you must modify the/etc/resolv. conf file. The following is an example of a resolv. conf file.
[Example 11-14]
[Root @ CentOS ~] # Cat/etc/resolv. conf
Nameserver 192.168.3.1
Nameserver 192.168.3.2
Options rotate
Options timeout: 1 attempts: 2
Among them, 192.168.3.1 is the first-name server, 192.168.3.2 is the second-name server, and the option rotate option refers to polling between the two dns servers, option timeout: 1 indicates the resolution timeout value of 1 s (the default value is 5 seconds), and attempts indicates the number of attempts to resolve the domain name. To add a DNS server, you can directly modify this file.