ifconfig命令

一、基本用法

ifconfig网卡操作命令,可以用来启动、设置和修改网络配置。

直接使用命令可以查看当前主机所有网卡的信息:

[ma@localhost ~]$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 20:89:84:49:EF:46  
          inet addr:192.168.123.58  Bcast:192.168.123.255  Mask:255.255.255.0
          inet6 addr: abcd::ff03/120 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:919 errors:0 dropped:0 overruns:0 frame:0
          TX packets:419 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:78144 (76.3 KiB)  TX bytes:47656 (46.5 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

其中,eth0是本地网卡信息,lo是本地环回地址,可以直接在命令后面加网卡名查看指定网卡的信息:

[ma@localhost ~]$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 20:89:84:49:EF:46  
          inet addr:192.168.123.58  Bcast:192.168.123.255  Mask:255.255.255.0
          inet6 addr: abcd::ff03/120 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:949 errors:0 dropped:0 overruns:0 frame:0
          TX packets:439 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:80840 (78.9 KiB)  TX bytes:50696 (49.5 KiB)

二、启动/停止网卡

启动和停止网卡的命令为:

ifconfig 网卡名 up # 启动网卡
ifconfig 网卡名 down # 关闭网卡

例如启动eth0,则输入命令ifconfig eth0 up即可。

三、设置网卡的ip地址

如果想要给网卡eth0设置ip地址,可以通过以下方式来实现:

ifconfig eth0 192.168.10.2
ifconfig eth0 192.168.10.2/24 # ip地址+掩码位数
ifconfig eth0 192.168.10.2 netmask 255.255.255.0 # ip地址+子网地址
ifconfig eth0 192.168.10.2/24 broadcast 192.168.10.255 # 指定广播地址

四、多网卡

和windows以下,linux环境下的网卡也可以设置多个ip地址,如需给网卡eth0添加额外的地址,可以通过以下方式来完成:

ifconfig eth0:1 192.168.10.1
ifconfig eth0:2 192.168.10.2

此时,使用ifcofnig即可看到多块网卡:

eth0      Link encap:Ethernet  HWaddr 20:89:84:49:EF:46  
          inet addr:192.168.123.58  Bcast:192.168.123.255  Mask:255.255.255.0
          inet6 addr: abcd::ff03/120 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1199 errors:0 dropped:0 overruns:0 frame:0
          TX packets:570 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:102345 (99.9 KiB)  TX bytes:67010 (65.4 KiB)

eth0:1    Link encap:Ethernet  HWaddr 20:89:84:49:EF:46  
          inet addr:192.168.10.1  Bcast:192.168.10.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:2    Link encap:Ethernet  HWaddr 20:89:84:49:EF:46  
          inet addr:192.168.10.2  Bcast:192.168.10.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

五、ipv6地址

如果想要给网卡设定ipv6地址,则需通过指定inet6来完成。

添加一个ipv6地址:

ifconfig eth0:1 inet6 add abcd::ff10
ifconfig eth0:2 inet6 add abcd::ff11

查看地址信息:

> ifconfig eth0:1 
eth0:1    Link encap:Ethernet  HWaddr 20:89:84:49:EF:46  
          inet addr:192.168.10.1  Bcast:192.168.10.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

发现并没有ipv6地址,实际上,ipv6地址信息显示在主网卡上面,要查询eth0才能看到:

> ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 20:89:84:49:EF:46  
          inet addr:192.168.123.58  Bcast:192.168.123.255  Mask:255.255.255.0
          inet6 addr: abcd::ff11/0 Scope:Global
          inet6 addr: abcd::ff10/0 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1492 errors:0 dropped:0 overruns:0 frame:0
          TX packets:743 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:128419 (125.4 KiB)  TX bytes:89202 (87.1 KiB)

删除ipv6地址:

ifconfig eth0 inet6 del abcd::ff10/0
ifconfig eth0 inet6 del abcd::ff11/0
最后修改:2018 年 12 月 16 日
如果觉得我的文章对你有用,请随意赞赏