How to modify IP address, gateway, and DNS in Linux Command Line
Method 1:
Ifconfig eth0 192.168.1.18 netmask 255.255.255.0
Note: This method can make changes take effect immediately, and will be restored to the original IP address after restart.
Method 2:
Vi/etc/sysconfig/network-scripts/ifcfg-eth0
Note: This method takes effect after being restarted and is permanent.
If you want to change it immediately and it will take effect permanently, you can only use the two methods at the same time.
The preceding method modifies the IP address through the linux Command Line.
Nic eth0 IP address changed to 102.160.1
Ifconfig eth0 102.160.1 netmask 255.255.255.0
Change gateway to 102.160.254
Route add default gw 102.160.254
Linux Command Line dns Modification
Echo "nameserver 202.202.202.20">/etc/resolv. conf
Restart NETWORK SERVICE
Example: Fixed IPDEVICE = eth0HWADDR = 00: 0C: 29: F7: EF: BFONBOOT = yesTYPE = EthernetNETMASK = 255.255.255.0IPADDR = 192.168.0.68GATEWAY = 192.168.0.1 and red
Restart NIC:/etc/init. d/network restart
Ifconfig eth0 new ip then edit/etc/sysconfig/network-scripts/ifcfg-eth0, modify ip
[Aeolus @ db1 network-scripts] $ vi ifcfg-eth0
DEVICE = eth0ONBOOT = yesBOOTPROTO = staticIPADDR = 219.136.241.211NETMASK = 255.255.255.128GATEWAY = 219.136.241.254
[Aeolus @ db1 etc] $ vi resolv. conf
Nameserver 202.96.128.68nameserver 219.136.241.206
----------------------- Modify the nic ip address and gateway in Linux
We recommend that you modify the IP address vi/etc/sysconfig/network-scripts/ifcfg-eth0DEVICE = eth0BOOTPROTO = noneBROADCAST = mask = 192.168.1.33NETMASK = 255.255.255.0NETWORK = 192.168.1.0ONBOOT = yesUSERCTL = noPEERDNS = noTYPE = ethernet ~
Vi/etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE = eth1ONBOOT = yesBOOTPROTO = noneIPADDR = 192.168.2.34NETMASK = 255.255.255.0USERCTL = noPEERDNS = noTYPE = EthernetNETWORK = 192.168.2.0BROADCAST = 192.168.2.255 2 modify gateway vi/etc/sysconfig/network
NETWORKING = yesHOSTNAME = AaronGATEWAY = 192.168.1.1
3. restart the network configuration/etc/init. d/network restart
Bytes -----------------------------------------------------------------------------------
Modify configuration file
/Etc/sysconfig/network-scripts/has a configuration file
For example, the file: ifcfg-eth0 represents the configuration file of the actual Ethernet NIC 0
For example, the file: ifcfg-eth0: 1 represents the actual Ethernet NIC 0 configuration file
Domain Name Server Configuration File:/etc/resolv. conf
Modify the IP address immediately effective: # ifconfig eth0 192.168.0.20 netmask 255.255.255.0 start effective: Modify/etc/sysconfig/network-scripts/ifcfg-eth0
Modify the default gateway to take effect immediately: # route add default gw 192.168.0.254 start effective: Modify/etc/sysconfig/network-scripts/ifcfg-eth0
Modification of dns modification/etc/resolv. conf takes effect immediately.
Modification of host name takes effect immediately: # hostname fc2 takes effect upon startup: Modify/etc/sysconfig/network
# Realtek semiconduco ., ltd. RTL-8139/8139C/8139C + DEVICE = eth0 Interface Name BOOTPROTO = none static configuration, if this value is "dhcp" is dynamic, static also indicates the static IP address BROADCAST = 192.168.10.255 BROADCAST address. HWADDR = 00: 13: D3: 27: 9F: is automatically calculated based on the IP address and subnet mask: 80 IPADDR = 192.168.10.2381166init = bytes = yesNETMASK = 255.255.255.0NETWORK = 192.168.10.0 specify the network. The ONBOOT = yes automatically loads GATEWAY = 192.168.10.1TYPE = EthernetPEERDNS = yesUSERCTL = no
Ifdown eth0 disable network ifconfig eth0 down disable network
Ifup eth0 enable Network ifconfig eth0 up enable Network
Set dns/etc/resolv. conf
Nameserver 61.144.56.101nameserver 202.96.128.166
[Yeger @ yeger ~] $ Cat/etc/resolv. conf # Generated by NetworkManagernameserver 202.96.128.86nameserver 202.96.128.166
Other aspects: service network start // start the network service network stop // stop the network service network restart // restart the network service network status // view the network service status
Ifconfig eth0 192.168.10.222 netmask 255.255.255.0 // temporarily modify the interface IP address (no need to restart the interface)
[Yeger @ yeger ~] $ Sudo ifconfig wlan0 192.168.21.199 netmask 255.255.255.0 [yeger @ yeger ~] $ Ifconfig wlan0wlan0 Link encap: Ethernet HWaddr 00: 02: 72: 77: BB: D1inet addr: 192.168.21.199 Bcast: 192.168.21.255 Mask: 109addr: fe80: 202: 72ff: fe77: bbd1/64 Scope: LinkUP broadcast running multicast mtu: 1500 Metric: 1RX packets: 3246 errors: 0 dropped: 0 overruns: 0 frame: 0TX packets: 1947 errors: 0 dropped: 0 overruns: 0 carrier: 0 collisions: 0 txqueuelen: 1000RX bytes: 4514869 (4.3 MiB) TX bytes: 177732 (173.5 KiB) wlan0 indicates the first wireless Ethernet Card Link encap. The name HWaddr indicates the MAC Address (Hardware Address) of the network card in the OSI Physical Layer) inet addr indicates that the IP Address Bcast of the network adapter in the TCP/IP network indicates that the broadcast Address (Broad Address) Mask indicates that the Subnet Mask (Subnet Mask) MTU indicates the maximum transmission unit, MTU values are not necessarily the same for different local networks. For Ethernet networks, MTU is set to 1500 bytes Metric to indicate the Metric value, it is usually used to calculate the route cost. RX indicates the received data packet. TX indicates the sent data packet. collisions indicates the number of packet conflicts. txqueuelen indicates the Transfer Queue) interrupt indicates the IRQ interrupt Number of the network adapter. Base address indicates the I/O address.
Configure the virtual network interface IP Address: the network interface must have multiple IP addresses.
Command Format: ifconfig Nic name: Virtual Nic id ip address netmask Subnet Mask
[Yeger @ yeger ~] $ Sudo ifconfig wlan0: 1 192.168.21.188 netmask 255.255.255.0
Change the nic mac address
Ifconfig Nic name hw ether MAC address
[Yeger @ yeger ~] $ Ifconfig wlan0 hw ether 00: 11: 22: 33: 44: 55
SIOCSIFHWADDR: operation not allowed [yeger @ yeger ~] $ Sudo ifconfig wlan0 hw ether 00: 11: 22: 33: 44: 55 SIOCSIFHWADDR: the device or resource is busy [yeger @ yeger ~] $ Ifconfig wlan0 downSIOCSIFFLAGS: insufficient permissions [yeger @ yeger ~] $ Sudo ifconfig wlan0 down [yeger @ yeger ~] $ Sudo ifconfig wlan0 hw ether 00: 11: 22: 33: 44: 55 changed successfully
[Yeger @ yeger ~] $ Netstat-ant view port information a all n numbers display t tcp u udp Active Internet connections (servers and established have established a connection) proto Recv-Q Send-Q Local Address Foreign Address State protocol Local Address remote Address connection status type tcp 0 0 0.0.0.0: 57798 0.0.0.0: * LISTEN listen indicates listening status tcp 0 0 0.0.0.0: 111 0.0.0.0: * LISTEN tcp 0 0 192.168.122.1: 53 0.0.0.0: * LISTEN tcp 0 0 0.0.0.0: 22 0.0.0.0: * LISTEN tcp 0 0 127.0.0.1: 631 0.0.0.0: * LISTEN tcp 0 0 127.0.0.1: 25 0.0.0.0: * LISTEN tcp 0: 111: * LISTEN tcp 0 0: 22: * LISTEN tcp 0 0: 1: 631 ::: * LISTEN
[Yeger @ yeger ~] $ Netstat-r view route table Kernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt mask * 255.255.255.0 U 0 0 0 0 wlan0192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0default 192.168.21.1 0.0.0.0 UG 0 0 0 0 0
[Yeger @ yeger ~] $ Netstat-I view network Interface status Kernel Interface tableIface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP Flg eth0 1500 0 0 0 0 0 0 0 0 0 BMUlo 16436 0 0 0 0 0 230 0 0 0 LRUvirbr0 1500 0 0 0 0 29 0 0 0 BMRUwlan0 1500 0 10546 0 0 0 7060 0 0 0 0 0 0 BMRUwmaster0 0 0 0 0 0 0 0 RU [yeger @ yeger ~] $
MTU field: indicates the maximum transmission unit, that is, the maximum number of packets transmitted by network interfaces. Met field: indicates the metric value. The smaller the value, the higher the priority. RX-OK/TX-OK: number of packets received and sent, respectively. RX-ERR/TX-ERR: Number of error packets received and sent. RX-DRP/TX-DRP: indicates the number of dropped. RX-OVR/TX-OVR: indicates the number of dropped packets.
[Yeger @ yeger ~] $ Nslookup www.baidu.com test domain name resolution Server: 202.96.128.86Address: 202.96.128.86 #53
Non-authoritative answer: www.baidu.com canonical name = www.a.shifen.com. Name: www. a. shifen. comAddress: 119.75.218.45Name: www.a.shifen.com
Address: 119.75.218.45
The IP address of Baidu is 119.75.218.45.