首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

linux ifconfig下令学习

2013-10-19 
linux ifconfig命令学习ifconfig命令用来配置网络接口。1. ifconfig默认会显示已经active的网络接口,sh-# i

linux ifconfig命令学习

ifconfig命令用来配置网络接口。
1. ifconfig默认会显示已经active的网络接口,
sh-# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:E7:06:00:00
          UP BROADCAST MULTICAST  MTU:1500  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:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:40 Base address:0x2000

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:1244 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1244 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:94792 (92.5 Kb)  TX bytes:94792 (92.5 Kb)

sh-#

2. ifconfig也可以查看指定网卡的信息,
sh-# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:E7:06:00:00
          UP BROADCAST MULTICAST  MTU:1500  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:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:40 Base address:0x2000

sh-#

3. 使用ifconfig命令修改IP地址,
sh-# ifconfig eth0 192.168.0.111 netmask 255.255.255.0
sh-#
sh-# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:E7:06:00:00
          inet addr:192.168.0.111  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:92 errors:0 dropped:59 overruns:0 frame:0
          TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:11155 (10.8 Kb)  TX bytes:5318 (5.1 Kb)
          Interrupt:40 Base address:0x2000

sh-#

sh-# ifconfig eth0:0 192.168.0.112 netmask 255.255.255.0
sh-#

4. 使用ifconfig配置一块新的网卡eth0:0,但是发现配置之后ping不通了,
sh-#
sh-# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:E7:06:00:00
          inet addr:192.168.0.111  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:781 errors:0 dropped:480 overruns:0 frame:0
          TX packets:141 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:106519 (104.0 Kb)  TX bytes:20610 (20.1 Kb)
          Interrupt:40 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:E7:06:00:00
          inet addr:192.168.0.112  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:40 Base address:0x2000

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:1276 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1276 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:95816 (93.5 Kb)  TX bytes:95816 (93.5 Kb)

sh-#

5. 禁用指定的网卡,
sh-# ifconfig eth0 down
[   70.149765] star: star_stop(eth0)
sh-#
sh-# ifconfig
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:23 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:736 (736.0 b)  TX bytes:736 (736.0 b)

sh-#

6. 网卡被禁用后,可以使用ifconfig -a查看那些未激活的网卡信息,
sh-# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:E7:06:00:00
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:1547 errors:0 dropped:24 overruns:0 frame:0
          TX packets:1030 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2072118 (1.9 Mb)  TX bytes:83723 (81.7 Kb)
          Interrupt:40 Base address:0x2000

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:151 errors:0 dropped:0 overruns:0 frame:0
          TX packets:151 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:11968 (11.6 Kb)  TX bytes:11968 (11.6 Kb)

sh-#

7. 如果要启动未激活的网卡,
sh-# ifconfig eth0 up

8. 使用ifconfig配置网卡的MAC地址,
sh-# ifconfig eth0 down
sh-# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:E7:06:00:00
          inet addr:192.168.0.174  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:259 errors:0 dropped:59 overruns:0 frame:0
          TX packets:170 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:81474 (79.5 Kb)  TX bytes:25650 (25.0 Kb)
          Interrupt:40 Base address:0x2000

sh-#

可以看到,原来想将eth0禁止掉,但是eth0却仍然是激活的状态。
这是因为eth0一直处于忙碌的状态,需要先将对外的网络断掉,再试一下,
sh-# ifconfig eth0 down
[  336.042795] star: star_stop(eth0)
sh-#
sh-# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:00:00:00:FC:CB
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:389 errors:0 dropped:116 overruns:0 frame:0
          TX packets:199 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:99361 (97.0 Kb)  TX bytes:29032 (28.3 Kb)
          Interrupt:40 Base address:0x2000

sh-#

接下来修改MAC地址,
sh-# ifconfig eth0 hw ether 00:00:00:00:FC:CB
sh-# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:00:00:00:FC:CB
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:389 errors:0 dropped:116 overruns:0 frame:0
          TX packets:199 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:99361 (97.0 Kb)  TX bytes:29032 (28.3 Kb)
          Interrupt:40 Base address:0x2000

sh-#

可以看到MAC地址修改成功,接下来再将eth0激活,
sh-# ifconfig eth0 up
[  450.933630] star: star_open(eth0)
[  450.934173] star: request interrupt vector=40
[  450.938711] star: Internal PHY mode

sh-#
sh-# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:00:00:00:FC:CB
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:389 errors:0 dropped:116 overruns:0 frame:0
          TX packets:199 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:99361 (97.0 Kb)  TX bytes:29032 (28.3 Kb)
          Interrupt:40 Base address:0x2000

sh-#

此时MAC地址已经正式修改完成了,插上网线,ping试试看,
sh-# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:00:00:00:FC:CB
          inet addr:192.168.0.174  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:403 errors:0 dropped:118 overruns:0 frame:0
          TX packets:220 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:101677 (99.2 Kb)  TX bytes:32218 (31.4 Kb)
          Interrupt:40 Base address:0x2000

sh-#
sh-# ping www.google.com
PING www.google.com (74.125.239.144) 56(84) bytes of data.
64 bytes from nuq05s02-in-f16.1e100.net (74.125.239.144): icmp_req=1 ttl=53 time=262 ms
64 bytes from nuq05s02-in-f16.1e100.net (74.125.239.144): icmp_req=2 ttl=53 time=252 ms
64 bytes from nuq05s02-in-f16.1e100.net (74.125.239.144): icmp_req=3 ttl=53 time=262 ms
^C
--- www.google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2017ms
rtt min/avg/max/mdev = 252.234/259.069/262.895/4.862 ms

sh-#

ifconfig所做的网络设定会在系统重启后消失,如果需要这些设定在系统启动时自动生效,
则需要将其配置到对应的网络配置文件中。

ifconfig命令的功能很强大,还有很多其它的功能,
在此不多说,有需要时再查资料。

热点排行