Use of Linux ifconfig Command

  • 2021-08-28 21:40:42
  • OfStack

1. Brief introduction to commands

The ifconfig (configure a network interface) command is a system administrator command for viewing and configuring network interfaces.

2. Command format


ifconfig [-v] [-a] [-s] [INTERFACE]
ifconfig [-v] INTERFACE [aftype] OPTIONS | ADDRESS ...

3. Description of options


-a
	显示所有接口的状态,包括那些关闭的接口
-s
	显示接口简短状态列表,类似于 netstat -i
-v
	冗余模式,遇到错误将报告错误信息
INTERFACE
	接口的名称。这通常是1个驱动程序名,后面跟着1个单元号,例如用于第1个以太网接口的 eth0
up
	此标志将导致激活接口。如果将地址分配给接口,则会隐式激活接口
down
	此标志用于关闭接口的驱动程序
[-]arp
	启用或关闭接口使用 ARP 协议
[-]promisc
	启用或禁用接口的混杂模式。如果选中,网络上的所有数据包都将由接口接收
[-]allmulti
	启用或禁用所有多播模式。如果选中,则接口将接收网络上的所有多播数据包
mtu N
	设置接口的最大传输单元
dstaddr ADDR
	为点对点链路(如 PPP)设置远程 IP 地址。关键字 dstaddr 现在已经过时了,使用 pointopoint 关键字代替
netmask ADDR
	设置接口的 IP 网络掩码。此值默认为通常的 A、B 或 C 类网络掩码(从接口 IP 地址派生),但可以设置为任何值
add ADDR/PREFIX_LEN
	向接口添加 IPv6 地址
del ADDR/PREFIX_LEN
	从接口中删除 IPv6 地址
tunnel ::aa.bb.cc.dd
	创建1个新的SIT(IPv6-in-IPv4)设备,通过隧道到达给定的目的地
irq ADDR
	设置此设备使用的中断行。并非所有设备都可以动态更改其 IRQ 设置
io_addr ADDR
	为该设备设置 I/O 空间中的起始地址
mem_start ADDR
	设置此设备使用的共享内存的起始地址。只有少数几个设备需要这个
media TYPE
	设置设备要使用的物理端口或介质类型。典型的类型值有10base2(细以太网)、10baseT(双绞线 10Mbps 以太网)、AUI(外部收发器)等。特殊的媒体类型可以使用 auto 来告诉驱动程序进行自动感知。注意,并非所有设备都可以更改此设置
[-]broadcast [ADDR]
	如果给定地址参数,则为该接口设置协议广播地址。否则设置(或清除)接口的 IFF_BROADCAST标志
[-]pointopoint [ADDR]
	这个关键字启用了接口的点对点模式,这意味着它是两台机器之间的直接连接,没有其他人监听它。如果给了地址参数,就像过时的 dstaddr 关键字1样,设置另1端的协议地址。否则设置或清除接口的 IFF_POINTOPOINT 标志
hw CLASS ADDR
	如果设备驱动程序支持此操作,则设置此接口的物理地址。CLASS 为硬件类型名称,ADDR 为物理地址。目前支持的硬件类包括 ether (Ethernet)、ax25 (AMPR AX.25)、ARCnet 和 netrom (AMPR NET/ROM)
multicast
	在接口上设置多播标志。1般不需要显示设置,因为驱动程序本身会设置正确的标志
ADDRESS
	接口的 IP 地址
txqueuelen LENGTH
	设置设备的传输队列的长度。对于具有高延迟(调制解调器链路,ISDN)的较慢设备,将其设置为小值是有用的,以防止快速批量传输过多地干扰诸如 telnet 之类的交互通信

4. Common examples

(1) View the network interface information in the active state.


ifconfig
eth0   Link encap:Ethernet HWaddr 00:16:3E:00:1E:51 
     inet addr:10.160.7.81 Bcast:10.160.15.255 Mask:255.255.240.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:61430830 errors:0 dropped:0 overruns:0 frame:0
     TX packets:88534 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:3607197869 (3.3 GiB) TX bytes:6115042 (5.8 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:56103 errors:0 dropped:0 overruns:0 frame:0
     TX packets:56103 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0
     RX bytes:5079451 (4.8 MiB) TX bytes:5079451 (4.8 MiB)

eth0 represents the first network card.

Line 1 Link encap: Ethernet indicates that the connection type is Ethernet, and HWaddr 00: 16: 3E: 00: 1E: 51 indicates the physical address of the network card;
Line 2 inet addr: 10.160. 7.81 is the IPv4 address of the network card, Bcast: 10.160. 15.255 is the broadcast address, and Mask: 255.255. 240.0 is the subnet mask;
The third line UP (representing the open state of the network card), BROADCAST (supporting broadcasting), RUNNING (representing the network cable of the network card is connected), MULTICAST (supporting multicast), MTU: 1500 (the maximum transmission unit is 1500 bytes), Metric: 1 indicates the priority of the network card routing packet, and the lower the value, the higher the priority;
Line 4. 5: Statistics of receiving and sending data packets;
Line 7: Receive and send data byte statistics.

lo is the loopback address of the host. This 1 is generally used to test a network program, which can only be accessed on the local machine, but the host of the local area network or external network cannot access the address.

(2) View all network interface information, whether it is activated or not.


ifconfig -a

(3) View the specified network interface information.


ifconfig eth0

(4) Start and close the designated network card.


#  Start the network card 
ifconfig eth0 up
#  Close the network card 
ifconfig eth0 down

(5) Modify the MAC address.


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

(6) Configure the IP address.


#  To  eth0  Network card configuration  IP  Address 
ifconfig eth0 192.168.1.56

#  To  eth0  Network card configuration  IP  Address and Subnet Mask 
ifconfig eth0 192.168.1.56 netmask 255.255.255.0 

#  To  eth0  Network card configuration  IP  Address, plus subnet mask, plus broadcast address 
ifconfig eth0 192.168.1.56 netmask 255.255.255.0 broadcast 192.168.1.255

(7) Enable and close the ARP protocol.


#  Open the network card  eth0  Adj.  arp  Agreement 
ifconfig eth0 arp
#  Close the network card  eth0  Adj.  arp  Agreement 
ifconfig eth0 -arp

(8) Set the maximum transmission unit.


#  Set the maximum packet size that can pass to  1500 bytes
ifconfig eth0 mtu 1500

The above is the use of Linux ifconfig command details, more information about Linux ifconfig command please pay attention to other related articles on this site!


Related articles: