Set or modify network configurations (IP & gateway & DNS) in CentOS)
CentOS IP address Modification
# Ifconfig eth0 192.168.1.80
In this way, the IP address is changed192.168.1.80 (if you find that the network cannot be connected, you may need to change the gateway and DNS, which will be mentioned later), but after you restart the system or network adapter, it will still change back to the original address. This modification method only applies to temporary IP address modification. To make a permanent modification, you must modify/Etc/sysconfig/network-scripts/ifcfg-eth0 this file, the main content of this file is as follows (you can manually add items not in your file ):
[Python] view plaincopy
- # Vi/etc/sysconfig/network-scripts/ifcfg-eth0
- DEVICE = eth0 # describe the DEVICE alias corresponding to the NIC
- BOOTPROTO = static # Set the way for the NIC to obtain the IP address. The options can be static, dhcp, or bootp.
- BROADCAST = 192.168.1.255 # corresponding subnet BROADCAST address
- HWADDR = 00: 07: E9: 05: E8: B4 # physical address of the corresponding Nic
- IPADDR = 12.168.1.80 # This field is required only when the NIC is set to static.
- NETMASK = 255.255.255.0 # network mask corresponding to the network adapter
- NETWORK = 192.168.1.0 # The NETWORK address corresponding to the NIC, that is, the NETWORK segment to which the ENI belongs
- ONBOOT = yes # whether to set this network interface when the system starts. If it is set to yes, the device is activated when the system starts.
Modify Gateway# Route add default gw 192.168.1.1 dev eth0
In this way, the gateway is changed192.168.1.1. This modification is only temporary. After you restart the system or network card, it will change back to the original gateway. To make a permanent modification, you must modify/Etc/sysconfig/network. The main content of this file is as follows (you can manually add items that are not in your file ):
[Python] view plaincopy
- # Vi/etc/sysconfig/network
- NETWORKING = yes # indicates whether the system uses the network, which is generally set to yes. If it is set to no, the network cannot be used.
- HOSTNAME = centos # Set the Host Name of the local machine. The host name set here must correspond to the host name set in/etc/hosts.
- GATEWAY = 192.168.1.1 # Set the IP address of the GATEWAY connected to the local machine.
* ********* After the above files are modified, You need to restart the NIC to take effect:# Service network restart********
Modify DNSAfter all the above changes, when you ping a domain name, it is certainly not available, but the corresponding IP address is the same, then we need to modify the DNS. Modify DNS by modifying/Etc/resolv. conf file:
[Python] view plaincopy
- # Vi/etc/resolv. conf
- Nameserver8.8.8.8 # google Domain Name Server
- Nameserver8.8.4.4 # google Domain Name Server
SummaryThrough all the above settings, the system should be able to access the Internet.
If you have multiple NICs, you only need to write one copy in the same way as eth0.
If the system is built on a virtual machine, select the 'Bridge adapt' connection when setting the network of the virtual machine.