Ifconfig, ip, and route for linux network configuration commands

Source: Internet
Author: User
Tags network function nameserver

1. As a crazy linux enthusiast, the first thing we need to know is how to configure basic linux network configurations for IP addresses, routes, and host names in linux, of course, if we want to access the Internet, the basic IP Address Configuration is not enough. To access the network, we need to configure the DNS of our domain name resolution server, as a cainiao, I will introduce the basic network configuration commands we usually use. If you are not writing well, please forgive me ~~~~~

This article describes the basic commands for viewing and configuring networks in linux: ifconfig, ip, and route. Then, refer to the configuration of DNS, host name, and other information!

2. Before configuring the network, let's first understand the Network Interface Types and naming rules and network address types in linux.

1. Network Interface Type:

Lo: local loopback interface

Eth [0-9]: Ethernet Interface

Pppx: point-to-point connection

Of course, we usually use lo and Ethernet interfaces. lo is only used in the system for loop retrieval testing, the eth Ethernet interface is the physical interface that we often use to connect to the network cable.

2. Naming rules for Ethernet NICs:

The naming rules are different under different systems. Let's take the RedHat 5-32bit system as an example:

REHL 5-32bit: The definition file of the Ethernet NIC naming rule is/etc/modprod. conf.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 120 "border =" 0 "alt =" image 120 "src =" http://img1.51cto.com/attachment/201304/27/6466723_1367030995vopA.png "width =" 691 "height =" 194 "/>

3. Network Address type:

Temporary network address: the network information configured by using commands such as ifconfig takes effect immediately, but the network service or system will fail to be restarted.

Permanent Network Address: changes made by modifying the network configuration file in the system do not take effect immediately. You need to restart the network service or the system will take effect permanently.

Iii. ifconfig of the network IP address configuration command

1. ifconfig: View active network card information, which is limited to active network cards.

For example, first view the local Nic information as follows-ifconfig

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 121 "border =" 0 "alt =" image 121 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA9C47-1.png "width =" 682 "height =" 154 "/>

To show that ifconfig only displays the active Nic information, we will remove the lo Nic.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 122 "border =" 0 "alt =" image 122 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA955c-2.png "width =" 685 "height =" 219 "/>

2. related sub-commands of ifconfig

Ifconfig eth [0-9]: You can directly view the information of a network card, such as viewing only the information of eth0.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 123 "border =" 0 "alt =" image 123 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA95111-3.png "width =" 685 "height =" 205 "/>

Ifconfig-a: displays information about all NICs, including active or inactive NICs.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 124 "border =" 0 "alt =" image 124 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA95559-4.png "width =" 716 "height =" 330 "/>

The following uses ifconfig to configure the IP address of the NIC. Of course, this address is a temporary address. Once the network service is restarted or the network system is restarted, it will become invalid !!

Format: ifconfig ethx IP/MASK: configure the IP address of a NIC

For example, if the IP address of eth0 is set to 172.16.35.1/16, you can directly type ifconfig eth0 172.16.36.5/16 in the command line.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 126 "border =" 0 "alt =" image 126 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA94F5-5.png "width =" 699 "height =" 256 "/>

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 127 "border =" 0 "alt =" image 127 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA925D-6.png "width =" 705 "height =" 271 "/>

 

Restart the network service, and the configuration will expire, and then be restored to the original IP address, as shown in

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 128 "border =" 0 "alt =" image 128 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA961S-7.png "width =" 713 "height =" 334 "/>

3. management commands for network services. I have mentioned a command for restarting network services. What are the control commands for network services ??? It is very simple, that is, the start, stop, restart, status commands.

Format:

/Etc/init. d/network {start | stop | restart | status} or service network {start | stop | restart | status}

Iv. permanent IP Address Configuration

Through the above, we learned how to use ifconfig to configure temporary network IP addresses. Of course, this does not meet our needs. We cannot configure IP addresses every time we use computers ~ This is too troublesome, so we need to configure the IP address as a fixed IP address or go back through the DHCP service. In this way, we need to modify the network configuration file of the system, the network configuration text on REHL5 is/etc/sysconfig/network-scripts/ifcfg-eth [0-9]. Let's take a look at how to define the content in this file.

There are two Configuration Methods: DHCP and fixed IP

DHCP settings are relatively simple. You only need to specify the BOOTPROTOL type as DHCP. Of course, before configuration, let's first understand what content is defined in the configuration file? Details are shown in

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 129 "border =" 0 "alt =" image 129 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA91141-8.png "width =" 717 "height =" 242 "/>

Of course, we are setting a fixed IP address, so what about DHCP? DHCP is simpler. The configuration parameters are as follows:

DEVICE = eth0
BOOTPROTO = DHCP
ONBOOT = YES
HWADDR = ......

5. In the above content, we learned how to configure temporary IP addresses and permanent IP addresses for the linux system through the ifconfig command and configuration file, next, let's take a look at the system route settings. The route-related information setting command is route.

1. route: You can directly view the route information on our system.

Route-n: displays route information in numbers.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 130 "border =" 0 "alt =" image 130 "src =" http://img1.51cto.com/attachment/201304/27/6466723_1367031031WEHn.png "width =" 716 "height =" 218 "/>

2. sub-options of the route command

Add route

Route add-host: add host route

Route add-net: add a network route

Route add-net 0.0.0.0: add default route

Format: route add-net | host DEST gw NEXTHOP

For example, add a route to allow the host to access the CIDR Block 192.168.0.0/24 through 172.16.7.3.

Route add-net 192.168.0.0/24 gw 172.16.7.3

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 131 "border =" 0 "alt =" image 131 "src =" http://img1.51cto.com/attachment/201304/27/6466723_1367031034HiLy.png "width =" 716 "height =" 194 "/>

Delete A route

Route del-host: deletes a host route

Route del-net: delete a network route

For example, you can delete the route information route del-net 192.168.0.0 and 24.

3. The configuration added by using the route command will expire when the network service or system is restarted. This is only temporary configuration, to make the route information we configured permanently valid, we need to define the configuration information in the configuration file:/etc/sysconfig/network-scripts/route-ethx, ethx indicates the corresponding Nic route

Configuration format:

Format 1: DEST objective) via NEXTTOP next hop)

Format 2: ADDRESS0 = network address destination address), NETMASK0 = subnet mask destination network ),

GATEWAY0 = gateway that uses the NIC), ADDRESS1 =, NETMASK1 = ...... And so on. You can set a route entry.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 132 "border =" 0 "alt =" image 132 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA93c2-11.png "width =" 688 "height =" 279 "/>

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 133 "border =" 0 "alt =" image 133 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA91017-12.png "width =" 693 "height =" 192 "/>

The preceding configuration does not take effect immediately, but the restart of the network service or system takes effect permanently.

 

6. Another powerful command for network configuration: IP

The IP command is a command in the iproute2 package. It is more powerful than ifconfig and can configure IP and route information for the system.

1. ip link: Configure Network Interface Properties

Ip link show: view all network interface attributes

Ip-s link show: view all statistics

Ip link set ethX {up | down | arp {on | off}: sets the operation attribute of the network interface.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 134 "border =" 0 "alt =" image 134 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA921Z-13.png "width =" 710 "height =" 268 "/>

2. ip addr: configure the network address

Ip addr show: View network information. The information displayed is similar to ip link show.

Ip addr add IP dev ethX: Configure ip address the NIC information configured by this command cannot be viewed by ifconfig, and you need to view it by ip addr show)

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 135 "border =" 0 "alt =" image 135 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA91528-14.png "width =" 747 "height =" 368 "/>

Ip addr add IP dev ethx label ethX: X: configure the sub-Ip and add its alias

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 139 "border =" 0 "alt =" image 139 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA91645-15.png "width =" 771 "height =" 405 "/>

Delete the configured IP Address

Ip addr del IPADDR dev STRING

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 140 "border =" 0 "alt =" image 140 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA95629-16.png "width =" 755 "height =" 376 "/>

3. We have said that IP commands are powerful commands, because we can not only use IP commands to create IP address-related configurations, you can also use the IP command to set route information on the system. The main command is as follows:

Ip route: route information

Ip route change | replace: modifies route information

Ip route add to destination CIDR Block dev ethx via IP (Next Hop IP)

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 143 "border =" 0 "alt =" image 143 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA93U8-17.png "width =" 714 "height =" 247 "/>

The above is the IP command usage. Of course, the operations under these command lines are only temporary. Once the network service or system is restarted, these configurations will become invalid. If you want to make them take effect permanently, you need to use the relevant method to set it in the configuration file.

7. We have already introduced the IP Address Configuration. Here we will not repeat it. The following describes how to configure the configuration file of the network alias eth0: 0 for the host.

 

 
 
  1. (1) command configuration method: ifconfig and ip
  2.  
  3. Ifconfig ethx: x IP/netmask
  4.  
  5. Ip addr add IP dev ethx label ethX: X
  6.  
  7. The command is used to configure a temporary IP address. If you restart the network service or the system, the IP address is invalid.
  8.  
  9. (2) configuration file configuration method:
  10.  
  11. Modify/etc/sysconfig/network-scripts/ifcfg-ethx: x
  12.  
  13. DEVICE = ethx: x
  14.  
  15. BOOTPROTO = static
  16.  
  17. IPIPADDR = IP Address
  18.  
  19. NETMASK = Subnet Mask
  20.  
  21. GATEWAY = GATEWAY
  22.  
  23. ONBOOT = YES enabled or not
  24.  
  25. HWADDR =... MAC
  26.  
  27. Non-Primary addresses cannot be obtained using the DHCP service and must be static.

8. DNS configuration file:

 

 
 
  1. (1) DNS configuration file location:/etc/resolv. conf
  2.  
  3. (2) DNS configuration format:
  4.  
  5. Nameserver DNS_IP1
  6.  
  7. Nameserver DNS_IP2
  8.  
  9. Specify local resolution:
  10.  
  11. Add under/etc/hosts
  12.  
  13. Host Name of the target host
  14.  
  15. Fg: 172.16.36.1 www.chris.com
  16.  
  17. DNS resolution process -->/etc/hosts --> DNS Server

IX. Host Name configuration file:

 

 
 
  1. (1) Configuration File Location:/etc/sysconfig/network
  2.  
  3. (2) configuration format:
  4.  
  5. HOSTNAME = Name
  6.  
  7. NETWORKING = {yes | no}: whether to enable the network function

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image 145 "border =" 0 "alt =" image 145 "src =" http://www.bkjia.com/uploads/allimg/131227/1TA93313-18.png "width =" 724 "height =" 281 "/>

This article is from the "Chris On the way" blog, please be sure to keep this source http://chrinux.blog.51cto.com/6466723/1188108

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.