Configure the CentOS network
Configure the steps for CentOS hosts to access the TCP/IP network (take CentOS 6 as an example):
1. Configure IP and subnet mask:
Static assignment (effective immediately, but not permanently):
Operation
Ifcfg class command
Ip class command
Display all network card information
# ifconfig [- a]
# ip addr show [up]
Displays the specified network card information
# ifconfig eth0
# ip addr show eth0
Enable the network card
# ifconfig eth0 up or # ifup eth0
# ip link set eth0 up
Disable the network card
# ifconfig eth0 down or # ifdown eth0
# ip link set eth0 down
Modify IP
# ifconfig eth0 192.168.1.8 ifconfig eth0 24 or # ifconfig eth0 192.168.1.8 netmask 255.255.255.0
/ / 255.255.0.0 corresponds to 16255.255.255.0 corresponding to 24, modified test: # ping 192.168.1.8
Add a single IP
# ip addr add 192.168.1.9/24 dev eth0
Delete a single IP
# ip addr del 192.168.1.9/24 dev eth0
Delete all IP
# ip addr flush dev eth0
Add a network card alias
# ifconfig eth0:0 192.168.1.10/24 up
# ip addr add 192.168.1.10/24 label eth0:0 dev eth0
Delete network card alias
# ifconfig eth0:0 down
# ip addr del 192.168.1.10 Compact 24 label eth0:0 dev eth0 or
# ip addr flush label eth0:0 dev eth0
DHCP dynamic allocation
Modify the configuration file (permanent, but not immediately effective):
# vim / etc/sysconfig/network-scripts/ifcfg-eth0 / / can configure IP, subnet mask, gateway and DNS at the same time
Parameter name
Parameter meaning
DEVICE=eth0
The device to which this configuration is associated. The device name should be the same as the file name ifcfg-.
HWADDR=00:50:56:BF:57:9E
MAC address of the corresponding device (hardware address)
TYPE=Ethernet
Device types: such as Ethernet (Ethernet), Bridge (bridging), etc.
UUID=365f2df0-d0e1-4372-ac96-ac4622da7f91
Unique identification number of the device
ONBOOT=yes
Whether to activate this device during system boot
NM_CONTROLLED=no
Whether to accept NetworkManager script control or not, CentOS 6 recommends "no"
BOOTPROTO=static
Address configuration protocol used to activate this device, including {none | static | dhcp | bootp}
IPADDR=192.168.1.115
IP address
NETMASK=255.255.255.0
Subnet mask
GATEWAY=192.168.1.254
Gateway
DNS1=114.114.114.114
Primary DNS server address
DNS2=8.8.8.8
Secondary DNS server address
USERCTL=no
Whether to allow ordinary users to enable or disable this device
IPV6INIT=no
Whether to automatically initialize the IPv6 protocol address
PEERDNS=no
If the value of BOOTPROTO is "dhcp", whether to allow the DNS server information assigned by DHCP Server to be directly overwritten to the / etc/resolv.conf configuration file
The implementation takes effect immediately after configuration: # service network restart
Configure multiple addresses on a single network card through the alias of the network card:
# cd / etc/sysconfig/network-scripts
# vim ifcfg-eth0:0 / / there is no need to configure MAC address, UUID and GATEWAY
Parameter name
Parameter meaning
DEVICE=eth0:0
The device to which this configuration is associated. The device name should be the same as the file name ifcfg-.
TYPE=Ethernet
Device types: such as Ethernet (Ethernet), Bridge (bridging), etc.
ONBOOT=yes
Whether to activate this device during system boot
NM_CONTROLLED=no
Whether to accept NetworkManager script control or not, CentOS 6 recommends "no"
BOOTPROTO=static
The address configuration protocol used when activating this device
Non-primary address does not support booting using DHCP protocol. {none | static} is supported.
IPADDR=192.168.1.116
IP address
NETMASK=255.255.255.0
Subnet mask
DNS1=114.114.114.114
Primary DNS server address
DNS2=8.8.8.8
Secondary DNS server address
The implementation takes effect immediately after configuration: # service network restart
2. Configure routing (required for cross-network communication):
Static assignment (effective immediately, but not permanently):
Operation
Ifcfg class command
Ip class command
Display routing information
# route-n
/ / Gateway 0.0.0.0: indicates the local network routing entry, which does not need to be forwarded through the gateway and does not require any routing.
# ip route show
Add host rout
Target host 192.168.1.11
Next Hop Gateway 192.168.199.254
# route add-host 192.168.1.11 gw 192.168.199.254
# ip route add 192.168.1.11 via 192.168.199.6 dev eth0
/ / No subnet mask can be added after IP
Delete host rout
# route del-host 192.168.1.11
# ip route del 192.168.1.11
/ / No subnet mask can be added after IP
Add Network rout
Target network 192.168.0.0Universe 24
Next Hop Gateway 192.168.199.6
# route add-net 192.168.0.0swap 24 gw 192.168.199.6 or
# route add-net 192.168.0.0 netmask 255.255.255.0 gw 192.168.199.6
# ip route add 192.168.0.0/24 via 192.168.199.6
Delete a network route
# route del-net 192.168.0.0According 24 or
# route del-net 192.168.0.0 netmask 255.255.255.0
# ip route del 192.168.0.0/24
Add default rout
Next Hop Gateway 192.168.199.6
# route add default gw 192.168.199.6 or
# route add-net 0.0.0.0 gw 192.168.199.6
/ /-net 0.0.0.0: indicates that the destination address is any address, and a new route entry of 0.0.0.0 will be added in Destination
# ip route add default via 192.168.199.6
Delete default rout
# route del default
# ip route del default
Delete all routing information
# ip route flush dev eth0
Modify the configuration file (permanent, but not immediately effective):
# vim / etc/sysconfig/network-scripts/route-eth0 / / does not exist by default and needs to be created manually. Choose one of the following two configuration methods
Each line defines a route entry, such as: DEST via NEXT_HOP / / next-hop address must exist, that is, someone uses
Example:
192.168.1.10 via 172.16.100.77
192.168.0.0/24 via 172.16.100.177
Define a route entry every three lines, as follows:
ADDRESS#=Dest / / Target host or destination network
NETMASK#=Netmask / / subnet mask
GATEWAY#=Gateway / / Gateway
Example:
ADDRESS0=192.168.10.0
NETMASK0=255.255.255.0
GATEWAY0=172.16.100.177
ADDRESS1=192.168.20.0
NETMASK1=255.255.255.0
GATEWAY1=172.16.100.177
The implementation takes effect immediately after configuration: # service network restart
3. Configure DNS (required when using hostname to communicate):
Modify the configuration file (effective immediately and permanently):
# vim / etc/resolv.conf
Up to 3 nameserver DNS_SERVER_IP / / can be specified.
/ / commonly used DNS:114.114.114.114 and 8.8.8.8. You can also point to the address where the company's DNS server is located.
Test whether DNS is valid:
Positive solution test (FQDN-- > IP)
# dig-t A www.google.com.hk
# host-t A www.google.com.hk
Inverse solution test (IP-- > FQDN)
# dig-x 74.125.128.199
# host-t PTR 74.125.128.199
4. Configure hostname:
Static assignment (effective immediately, but not permanently):
Set hostname: # hostname hostname
View hostname: # hostname
Modify the configuration file (permanent, but not immediately effective):
# vim / etc/sysconfig/network
Parameter name
Parameter meaning
NETWORKING=yes
Whether the network feature is enabled on this machine
HOSTNAME=test
Set hostname
It can take effect immediately with hostname + logout command.
Hostname storage location: # cat / proc/sys/kernel/hostname
/ / Editing results cannot be saved directly using vim, but can be modified using the echo command