One linux command per day: the ifconfig command

Keywords: network Mac Linux ssh

Many windows are very familiar with the ipconfig command line tool, which is used to obtain and modify network interface configuration information. Linux systems have a similar tool, ifconfig(interfaces config). Usually you need to log in as root or use sudo to use the ifconfig tool on your Linux machine. Depending on the option attributes used in the ifconfig command, the ifconfig tool can be used not only to simply obtain network interface configuration information, but also to modify these configurations.

1. Command format:

ifconfig [network device] [parameters]

2. Command function:

The ifconfig command is used to view and configure network devices. This command can be used to configure the network when the network environment changes.

3. Command parameters:

  • up starts the specified network device/adapter.
  • down closes the specified network device/adapter. This parameter can effectively prevent the flow of IP information through the specified interface. If we want to close an interface permanently, we need to delete all the routing information of the interface from the core routing table.
  • The ARP setting specifies whether the network card supports the ARP protocol.
  • - promisc sets whether the promiscuous mode of the network card is supported. If this parameter is selected, the network card will receive all the packets sent to it in the network.
  • - If this parameter is selected, the network card will receive all multicast packets in the network.
  • - a Displays all interface information
  • - s Displays summary information (similar to netstat-i)
  • add Configure IPv6 Address to the Designated Network Card
  • del deletes the IPv6 address of the specified network card
  • < Hardware Address > Configure the Maximum Transmission Unit of Network Card
  • MTU < bytes > sets the maximum transmission unit (bytes) of the network card
  • Netmask < subnet mask > set the subnet mask of the network card. A mask can be a 32-bit hexadecimal number with a prefix of 0x or four decimal numbers separated by points. If you do not intend to divide the network into subnets, you can ignore this option; if you want to use subnets, remember that every system in the network must have the same subnet mask.
  • tunel Establishes Tunnel
  • dstaddr sets a remote address and establishes point-to-point communication
  • - Broadcast < Address > Sets Broadcast Protocol for Designated Network Card
  • - pointtopoint < address > sets point-to-point communication protocol for network card
  • multicast Sets multicast Logo for Network Card
  • Addresses Sets IPv4 address for Network Card
  • Txqueuelen < Length > Sets the length of the transmission queue for the network card

4. Use examples:

Example 1: Display network device information (active state)
Order:

ifconfig

Output:

[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:20  
          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:596390239 (568.7 MiB)  TX bytes:2886956 (2.7 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:68 errors:0 dropped:0 overruns:0 frame:0
          TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2856 (2.7 KiB)  TX bytes:2856 (2.7 KiB)

Explain:

eth0 represents the first network card, where HWaddr represents the physical address of the network card. You can see that the physical address (MAC address) of the current network card is 00:50:56:BF:26:20.

inet addr is used to represent the IP address of the network card. The IP address of the network card is 192.168.120.204, the broadcast address is Bcast:192.168.120.255, and the mask address is Mask:255.255.255.0.

lo is the bad address of the host, which is generally used to test a network program, but do not want users of local area network or external network to be able to view, can only run on this host and view the network interface used. For example, if the HTTPD server is assigned to a bad address, you can see your WEB website by typing 127.0.0.1 into the browser. But as you can see, no other host or user in the LAN knows.

Line 1: Connection Type: Ethernet (Ethernet) HWaddr (Hardware mac Address)
Line 2: IP address, subnet, mask of network card
Line 3: UP (for network card open state), RUNNING (for network card connected), MULTICAST (for multicast), MTU:1500 (maximum transmission unit): 1500 bytes
Fourth and Fifth Lines: Statistics of Receiving and Sending Data Packets
Line 7: Receive and send data byte count statistics.

Example 2: Start and close the specified network card
Order:

ifconfig eth0 up
ifconfig eth0 down

Explain:

ifconfig eth0 up is the starting network card eth0; ifconfig eth0 down is the closing network card eth0. ssh login linux server operation to be careful, closed can not open, unless you have multiple network cards.

Example 3: Configure and delete IPv6 addresses for network cards
Order:

ifconfig eth0 add 33ffe:3240:800:1005::2/64
ifconfig eth0 del 33ffe:3240:800:1005::2/64

Explain:

ifconfig eth0 add 33ffe:3240:800:1005::2/64 to configure IPv6 address for eth0 network card;
ifconfig eth0 add 33ffe:3240:800:1005::2/64 delete IPv6 address for eth0 network card;

In practice, ssh login linux server operation to be careful, closed can not be opened, unless you have multiple network cards.

Example 4: Modify MAC address with ifconfig
Order:

ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE

Output:

[root@localhost ~]# ifconfig eth0 down//close network card
[root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE//Modify MAC address
[root@localhost ~]# ifconfig eth0 up//boot network card
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:AA:BB:CC:DD:EE  
          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:596390239 (568.7 MiB)  TX bytes:2886956 (2.7 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:68 errors:0 dropped:0 overruns:0 frame:0
          TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2856 (2.7 KiB)  TX bytes:2856 (2.7 KiB)
[root@localhost ~]# ifconfig eth0 hw ether 00:50:56:BF:26:20//Close network card and modify MAC address 
[root@localhost ~]# ifconfig eth0 up//boot network card
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:20  
          inet addr:192.168.120.204  Bcast:192.168.120.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:596390239 (568.7 MiB)  TX bytes:2886956 (2.7 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:68 errors:0 dropped:0 overruns:0 frame:0
          TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2856 (2.7 KiB)  TX bytes:2856 (2.7 KiB) 

Example 5: Configure IP address
Output:

[root@localhost ~]# ifconfig eth0 192.168.120.56 
[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 
[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255

Explain:

ifconfig eth0 192.168.120.56
Configuring IP Place for eth0 Network Card: 192.168.120.56
ifconfig eth0 192.168.120.56 netmask 255.255.255.0
Configure IP address for eth0 network card: 192.168.120.56, and add sub-mask: 255.255.255.0
ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255
/ Configure IP address for eth0 network card: 192.168.120.56, add submask: 255.255.255.0, add broadcast address: 192.168.120.255

Example 6: Enabling and closing ARP protocol
Order:

ifconfig eth0 arp
ifconfig eth0 -arp

Output:

[root@localhost ~]# ifconfig eth0 arp 
[root@localhost ~]# ifconfig eth0 -arp

Explain:

ifconfig eth0 arp opens the arp protocol of eth0.
Ifconfig eth0-arp closes the ARP protocol of eth0;

Example 7: Setting the maximum transmission unit
Order:

ifconfig eth0 mtu 1500

Output:

[root@localhost ~]# ifconfig eth0 mtu 1480
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:1F  
          inet addr:192.168.120.203  Bcast:192.168.120.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1480  Metric:1
          RX packets:8712395 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36631 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:597062089 (569.4 MiB)  TX bytes:2643973 (2.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:518096 (505.9 KiB)  TX bytes:518096 (505.9 KiB)

[root@localhost ~]# ifconfig eth0 mtu 1500
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:56:BF:26:1F  
          inet addr:192.168.120.203  Bcast:192.168.120.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8712548 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36685 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:597072333 (569.4 MiB)  TX bytes:2650581 (2.5 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:518096 (505.9 KiB)  TX bytes:518096 (505.9 KiB)

[root@localhost ~]# 

Explain:

Set the maximum packet size that can be passed to 1500 bytes

Note: The network card information configured with ifconfig command does not exist after the reboot of the network card. In order to store the above configuration information in the computer forever, it is necessary to modify the configuration file of the network card.

Posted by BigJohn on Wed, 27 Mar 2019 15:21:29 -0700