Linux Network Configuration (TCP/IP key parameters are: IP, Netmask, Gateway, DNS)

Keywords: network DNS Linux Red Hat

The main parameters of TCP/IP are IP, Netmask, Gateway, DNS.

The main network-related configuration files in Linux are: / ect/hosts # Configuration Host Name (Domain Name) and Private IP Address Correspondence

/etc/sysconfig/network# Configure host name and gateway
/etc/sysconfig/network-scripts/ifcfg-eth0#eth0 configuration file, eth1 file named ifcfg-eth1, and so on (ethX here is the name of the network card interface, or other names, such as emX)
/etc/resolv.conf # Configure DNS (about which DNS server to use)
 

1./etc/sysconfig/network-scripts/ifcfg-file

In Red Hat or CentOS, the configuration file of the system network device is stored in the directory of / etc/sysconfig/network-scripts. ifcfg-eth0 contains the configuration information of the first network card and ifcfg-eth1 contains the configuration information of the second network card. At startup, the system reads the configuration file to determine whether or not a network card is started and how to configure it.

Following is an example of the / etc/sysconfig/network-scripts/ifcfg-eth0 file:

1. Setting IP manually:

  1. DEVICE=eth0    #Network Card Interface Name
  2. TYPE=Ethernet    #Network Card Type
  3. MACADDR=00:0C:29:96:38:F8    #The physical address of the machine, set when it leaves the factory.
  4. ONBOOT=yes    #Whether to start or not?
  5. BOOTPROTO=static    #Static address, and other options [none|dootp|dhcp] (no protocol at boot | BOOTP | DHCP dynamic access address)
  6. IPADDR=192.168.0.2    #The IP address assigned to the network card
  7. NETMASK=255.255.255.0    #Subnet masks, class C networks are all this masks, indicating that the first three digits represent the network address, and the last one represents the host address.
  8. GATEWAY=192.168.0.1    #Default gateway,.
  9.   
  10. BROADCAST=192.168.0.255    #Broadcast address
  11. USERCTL=no    #Are non-root users allowed to control the device?

2,DHCP:

  1. DEVICE=eth0     
  2. TYPE=Ethernet      
  3. MACADDR=00:0C:29:96:38:F8      
  4. ONBOOT=yes   
  5. BOOTPROTO=dhcp     

When using DHCP, / etc/resolv.conf is automatically set, so we can leave it alone.

After modifying the network configuration file, it is necessary to restart the network card in order to take effect.

Note: If you want to manually modify the network address or add new network connections, you can modify the corresponding file ifcfg - or create new files to achieve.

2./etc/resolv.conf file

The file / etc/resolv.conf configures the DNS client, which contains the DNS server address and domain name search configuration, and each line should contain a keyword and one or more parameters separated by spaces. (Configuring the correct DNS address is a necessary condition for connecting to an external network. If you do not connect to an external network, you may not set this item. You can also use the system-config-network-tui command, which is a text-mode window tool that can change the content of the configuration file directly after modification.
Here is an example file:
  1. search localdomain     
  2. nameserver 192.168.3.1 #Primary DNS Server  
  3. nameserver 8.8.8.8   #Secondary DNS Server  
search localdomain: Represents that when a host name (part name of the host name) is provided that does not include the full domain name, the suffix of the local domain is added after the host name; nameserver: Represents that the host specified by the address is the domain name server when the domain name is resolved. The domain name servers are queried in the order in which they appear in the files. Therefore, the most reliable servers should be given first. Currently, up to three name servers are supported.
 

3./etc/sysconfig/network file

This file is used to specify network configuration information on the server. Here is an example:
  1. NETWORK=yes    #Is the network configured?
  2. RORWARD_IPV4=yes    #Whether to turn on IP forwarding function?
  3. HOSTNAME= localhost.localdomain   #Represents the host name of the server
  4. GAREWAY=192.168.0.1    #Represents the IP address of a network gateway.
  5. GATEWAYDEV=eth0    #The device name of the gateway, that is, which network card to use.

4./etc/hosts file

When the machine starts, before DNS can be queried, the machine needs to query some matches between host names and IP addresses. The matching information is stored in the / etc/hosts file. In the absence of a domain name server, all network programs on the system resolve IP addresses corresponding to a host name by querying the file.
Here is an example of a / etc/hosts file:
  1. 127.0.0.1       localhost.localdomain localhost  
  2. ::1     localhost6.localdomain6 localhost6  

The left-most column is host IP information, and the middle column is host name. Any subsequent column is an alias for the host.
 
After modification, the network needs to be restarted to take effect. service network is used to invoke / sbin/service script to control network services:
  1. service network restart    #Restart network settings
  2. service network start    #Start Network Services
  3. service network stop    #Stop Network Services
  4. service network status    #View the status of network services
  5. It's fine too /etc/init.d/network [restart | start | stop | status]   

Network Profiles for Different Linux Distributions

Red Hat Linux/CentOS Series Network Profile Path:

  1. /etc/sysconfig/network-scripts/ifcfg-ethX      #Configuration files such as IP address, subnet mask, etc.
  2. /etc/sysconfig/network-scripts/ifcfg-lo        #Loop-back network card configuration
  3. /etc/sysconfig/network                         #Host name and gateway configuration file
  4. /etc/resolv.conf                               #DNS configuration file
  5. /etc/hosts                                     #Host and IP binding information

suse Linux Network Profile Path:

  1. /etc/sysconfig/network/ifcfg-eth-id-HWaddr     #Configuration files such as IP address, subnet mask, etc.
  2. /etc/HOSTNAME                                  #Global hostname configuration
  3. /etc/resolv.conf                               #DNS configuration file
  4. /etc/sysconfig/network/routes                  #Gateway Profile
  5. /etc/hosts                                     #Host and IP binding information

Debian/Ubuntu Linux Network Profile Path:

  1. /etc/network/interfaces                        #Configuration files such as IP address, subnet mask, etc.
  2. /etc/resolv.conf                               # DNS  
  3. /etc/hostname                                  #Global hostname configuration
  4. /etc/hosts                                     #Host and IP binding information

Slackware Linux Network Profile Path:

  1. /etc/rc.d/rc.inet1.conf                        #Configuration files such as IP address, subnet mask, etc.
  2. /etc/resolv.conf                               # DNS  
  3. /etc/hostname                                  #Global hostname configuration
  4. /etc/networks                                  #Setting up network number and other related information
  5. /etc/hosts                                     #Setting host and IP address bindings

Posted by excl209 on Thu, 20 Dec 2018 15:21:05 -0800