Configure network connection information for CentOS
The basic network configuration includes configuring the host name, configuring the NIC, and setting the client name resolution service.
1. Configure the Host Name
To view the name of the current host, run the hostname command. To set the host name temporarily, run the "new hostname" command, this command will not save the new host name to the/etc/sysconfig/network configuration file. Therefore, after the system is restarted, the host name will be restored to the host name set in the configuration file.
[Root @ localhost ~] # Hostname
Localhost. localdomain
To temporarily set the host name to redhat, run the following command:
[Root @ localhost ~] # Hostname redhat
[Root @ localhost ~] # Hostname
Redhat
After a new host name is set, the prompt on the left of "#" cannot be changed synchronously. After logging out with logout and logging on again, a new host name is displayed.
If the change of the host name takes effect for a long time, you should directly modify it in the/etc/sysconfig/network configuration file. When the system starts, the host name information will be obtained from the configuration file, and set the host name. The content in the network configuration file is as follows:
[Root @ localhost ~] # Cat/etc/sysconfig/network
NETWORKING = yes // whether the system uses the network service function
HOSTNAME = localhost. localdomain // set the Host Name
GATEWAY = *** // Default GATEWAY
FORWARD_IPV4 = false // whether to enable IP packet forwarding. this parameter is generally set to false for a single Nic.
2. Configure the NIC
The configuration of the NIC (network interface card) device, Nic IP address, subnet mask, and default gateway is the main aspect of the host network configuration, and is directly related to whether the current host can be normally connected and communicated. Nic configuration includes hardware driver configuration, IP address and gateway configuration.
1> network configuration file
The network configuration file/etc/sysconfig/network is used to configure the overall network service. If the network function is enabled, whether the IP packet forwarding service is enabled or not. When no Nic is configured or installed, you also need to set this file so that the local loopback device (lo) can work normally. This device is the basis for linux internal communication. Common settings are as follows:
(1) NETWORKING is used to set whether the system uses the network service function. Generally, set yes. If no, the network cannot be used, and many system services cannot be started. The method set in the configuration file is: NETWORKING = yes | no
(2) FORWARD_IPV4: Use sub-settings to check whether IPV4 packet forwarding is enabled. When there is only one network card, it is generally set to false. If two network cards are installed and the IP packet forwarding function is enabled, it is set to true, for example, when you use a dual-nic proxy to access the Internet or connect two network segments for communication.
FORWARD_IPV4 = false | true
You can also edit/etc/sysctl. conf configuration file, where the net. ipv4.ip _ forward = 0 statement changed to net. ipv4.ip _ forward = 1 to enable the packet forwarding function of the kernel.
(3) HOSTNAME is used to set the host name. The host name set in/etc/hosts must be the same as the host name set here.
(4) DOMAINNAME is used to set the host domain name.
(5) GATEWAY is used to set the gateway ip address of the local machine.
(6) GATEWAYDEV is used to set the name of the NIC used for communication with the gateway.
Typical network Configuration:
NETWORKING = yes
FORWARD_IPV4 = false
GATEWAY = ****
GATEWAYDEV = eth0
HOSTNAME = ****
DOMAINNAME = localdomain
2> Configure the device driver module of the NIC
To make the NIC work properly, you must first configure the driver of the NIC. This is similar to installing the driver of the NIC in windows.
Depending on the bus type, the currently used Ethernet cards are primarily ISA, PCI, and PCMCIA NICs. When the network adapter is running normally, the module configuration file (/etc/modules. conf or/etc/modprode. conf), and the name of the driver used by the NIC, the kernel service kmod automatically loads the driver module using the system command modprobe (insmod) to enable the device to work normally.
In linux, the module configuration file is used to load the hardware driver modules required by the system, such as the NIC, sound card, and USB drivers, when the system is started. In general, the linux installer can automatically detect and recognize the NIC and automatically configure the NIC in the module configuration file. The NIC driver modules to be loaded are different for NICs of different hardware. If multiple NICs exist in the system, specify the driver modules to be loaded for each Nic.
The module configuration file is generally written to the module's load command or module's alias definition. For example, there may be a similar line in the module configuration file: alias eth0 pcnet32
3> Nic configuration file
The device name, IP address, subnet mask, default gateway, and other configuration information of the NIC are stored in the NIC configuration file. A Nic corresponds to a configuration file, the configuration file is located in the/etc/sysconfig/network-scripts directory. The configuration file name has the following format: ifcfg-nic type and nic serial number
The type of the Ethernet Card is eth. Therefore, the configuration file name of the first Nic is ifcfg-eth0, the configuration file name of the second Nic is ifcfg-eth1, and so on. Other Nic configuration files can be cp command copy ifcfg-eth0 configuration file to get, and then as needed when the modification.
Linux also supports binding multiple IP addresses to a physical network card. In this case, a virtual network card is required for each bound IP address. The device name of the network card is ethN: M, the format of the corresponding configuration file name is ifcfg-ethN: M, where N and M are numbers starting from 0, representing the serial number. For example, the configuration file for the first virtual network card (device name eth0: 0) bound to the first ethernet card is ifcfg-eth0: 0. Linux supports up to 255 IP aliases, which can be obtained by copying the ifcfg-eth0 profile and modifying its configuration content.
In the NIC configuration file, each line is a configuration item. The functions and lines in the configuration file are as follows:
Project name
Set Value
Function
DEVICE
Eth0
Indicates the name of the current Nic device.
BOOTPROTO
Static or dhcp
Set the IP address acquisition method
BROADCAST
****
Broadcast address
IPADDR
****
IP address of the ENI
NETMASK
****
Subnet Mask Of the NIC
NETWORK
****
Network Address of the network where the network card is located
GATEWAY
****
Default Gateway
ONBOOT
Yes | no
Set whether to start the NIC device when the system starts.
To view the configuration file of the eth0 Nic, run the following command:
[Root @ localhost ~] # More/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE = "eth0"
BOOTPROTO = none
DEFROUTE = "yes"
20174_failure_fatal = "yes"
IPV6INIT = "no"
NAME = "System eth0"
NM_CONTROLLED = "yes"
ONBOOT = "yes"
TYPE = "Ethernet"
UUID = "5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03"
LAST_CONNECT = 1299464967
IPADDR = ****
PREFIX = 24
GATEWAY = ****
HWADDR = 00: 1E: EC: A7: 8A: 5B
DNS1 = ****
DNS2 = ****
To bind another IP address of 199.199.199.199 to the eth0 Nic, the binding method is:
[Root @ localhost ~] # Cd/etc/sysconfig/network-scripts
[Root @ localhost network-scripts] # cp ifcfg-eth0 ifcfg-eth0: 0
[Root @ localhost network-scripts] # cat ifcfg-eth0: 0
DEVICE = "eth0: 0"
BOOTPROTO = static
ONBOOT = yes
IPADDR = 199.199.199.199
NETMASK = 255.255.255.0
To temporarily bind an IP address to the NIC, run the following command:
Ifconfig eth0 add 199.199.199.199 netmask 255.255.255.0
Set the IP address of the NIC
Ifconfig Nic device name IP address netmask Subnet Mask
This command does not modify the NIC configuration file. The configured IP address is only valid for this time. restart the system or Nic after it is disabled, the IP address is set to the IP address specified in the NIC configuration file.
Disable NIC: ifconfig Nic device name down
Or ifdown Nic device name
Enable NIC: ifconfig Nic device name up
Or ifup Nic device name
Set the Default Gateway
A gateway is a device that connects hosts in the current network to other network hosts for communication. After the IP address and subnet mask of the host are set, the host can communicate with other hosts in the same network segment, but cannot communicate with hosts in other network segments at this time, in order to communicate with hosts of different network segments, the default gateway address must be set. The gateway address must be the address of the current network and cannot be the address of another network segment.
Set the default gateway, that is, set the default route. You can use the route command provided by linux to add or delete route information.
1> View Current route information
[Root @ localhost ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
59.79.87.0*255.255.255.0 U 1 0 0 eth0
Default 59.79.87.254 0.0.0.0 UG 0 0 0 eth0
2> Add/delete a default gateway
Add Default Gateway:
Route add default gw gateway IP address dev Nic device name
Delete the Default Gateway:
Route del default gw gateway IP Address
For example, to set the default gateway address of the network adapter eth0 to 59.79.87.1, run the following command:
[Root @ localhost ~] # Route add default gw 59.79.87.1 dev eth0
[Root @ localhost ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
59.79.87.0*255.255.255.0 U 1 0 0 eth0
Default 59.79.87.1 0.0.0.0 UG 0 0 0 eth0
Default 59.79.87.254 0.0.0.0 UG 0 0 0 eth0
To delete the default gateway, run the following command:
[Root @ localhost ~] # Route del default gw 59.79.87.1
3> Add/delete route information
Add a route record to the current route table. command usage:
Route add-net network address netmask subnet mask [dev Nic device name] [gw gateway]
To delete a route record, use the following command:
Route del-net network address netmask Subnet Mask
Bind IP address and MAC address
Binding an IP address to a MAC address prevents IP address theft. Usage:
First create the/etc/ethers file with the content "ip address mac address"
Then run the arp-f command. The binding takes effect.
Modify the MAC address of an Eni
Disable the NIC device to be modified, and then use the following command format to modify the settings:
Ifconfig Nic device name hw ether MAC address
Set DNS Server
The configuration file is/etc/resolv. conf.
[Root @ localhost ~] # Cat/etc/resolv. conf
# Generated by NetworkManager
Nameserver 192.168.252.253
Nameserver 192.168.252.254
Hosts file
/Etc/hosts is an early Method for host name resolution, including the correspondence between IP addresses and host names. During name resolution, the system will directly read the corresponding records of IP addresses and host names set in the file. The behavior comment line starting with "#" in the file. Each other behavior is recorded. The IP address is on the left, the host name, the host name, and the host alias are on the right. The default content of this configuration file is:
[Root @ localhost ~] # More/etc/hosts
127.0.0.1 localhost. localdomain localhost localhost4
: 1 localhost6.localdomain6 localhost6
61.55.167.141 bbs.chinaunix.net
DNS order
You can use the "hosts:" configuration option in the/etc/nsswitch. conf configuration file to specify the DNS server. The default resolution sequence is the hosts file.
[Root @ localhost ~] # Grep hosts/etc/nsswitch. conf
# Hosts: db files nisplus nis dns
Hosts: files dns # files indicates that the hosts file is used for name resolution.
Source http://blog.sina.com.cn/s/blog_620f4cf70100pi81.html