linux下网络常用命令摘要:

#ifconfig                            
 
 
#ifconfig eth0 up                    
 
 
#ifconfig eth0 down                  
 
 
#ifconfig 192.168.1.111              
 
 
#ifup eth0                            
 
 
#ifdown eth0                          
 
 
#/etc/init.d/network resatrt       
 
 
#ping                                
 
 
#route                                
 
 
#iwlist                              
 
 
#iwconfig                            
 
 
#host                                
 
 
#nslookup

        

命令详细解析:

在CentOS 6.0以前的版本里要用root身份且环境变量在root下才能用这些命令否则会提示 command not found 。其他linux发行版可能有的命令不可用或相同功能的命令是其它字母。使用时请注意,我是在Fedora 13下测试的,其它linux发行版情自行测试。

#ifconfig

[laopi@Think ~]$ ifconfig {interface} {up|down}      <==观察与启动关闭接口

[laopi@Think ~]$ ifconfig interface {options}        <==设置与修改接口

参数解析:

interface:网卡接口代号,包括eth0,eth1,wlan0,ppp0等

options:  可以选择参数,包括如下:

          up或down,启动(up)或关闭(down)该网络接口(没有任何参数)。                           

           mtu,可以设置不同的MTU(最大传输单元)值。

        

        

范例一:观察所有网络接口

[laopi@Think ~]$ ifconfig
 
 
eth0      Link encap:Ethernet  HWaddr 00:24:8C:79:1A:2B  
 
 
         inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
 
 
        
 
 
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
 
        
 
 
        
 
 
         collisions:0 txqueuelen:1000 
 
 
         RX bytes:409524257 (390.5 MiB)  TX bytes:16017703 (15.2 MiB)
 
 
         Interrupt:30 Base address:0x8000 
 
 
 
 
lo        Link encap:Local Loopback  
 
 
         inet addr:127.0.0.1  Mask:255.0.0.0
 
 
        
 
 
         UP LOOPBACK RUNNING  MTU:16436  Metric:1
 
 
        
 
 
        
 
 
         collisions:0 txqueuelen:0 
 
 
         RX bytes:4468 (4.3 KiB)  TX bytes:4468 (4.3 KiB)
 
 
 
 
wlan0     Link encap:Ethernet  HWaddr 00:24:2C:E7:CF:8F  
 
 
         inet addr:192.168.1.102  Bcast:192.168.1.255  Mask:255.255.255.0
 
 
        
 
 
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
 
        
 
 
        
 
 
         collisions:0 txqueuelen:1000 
 
 
         RX bytes:55744 (54.4 KiB)  TX bytes:5013 (4.8 KiB)

上述代码出现的各项数据解析:

eth0                            <==网卡号

lo                              <==loopback

wlan0                           <==网线网卡

HWaddr                          <==网卡的硬件地址

inet addr                       <==IPv4的IP地址

Bcast                           <==就是Blockcast(广播地址)

Mask                            <==就是Netmask(子网掩码)

inet6 addr                    

RX                              <==该行代表的是网络由启动到现在为止接收的数据包情况,                                     

                                  dropped代表数据包由于有问题而遭丢弃的数量。

TX                              <==与RX相反,该行代表的是网络由启动到现在为止发送的数据包情况,

collisions                      <==代表数据包碰撞的情况,如果发生的次数太多,表示你的网络情况不好

txqueuelen                      <==代表用来传输数据的缓存区的存储长度

RX Bytes,TX Bytes              <==总接收,传送的字节数

Interrupt                       <==网卡硬件的数据

范例二:暂时修改网络接口

[laopi@Think ~]$ ifconfig eth0 192.168.1.111

如果不加任何其他参数,则系统会依照IP所在的class(等级)范围,自动的计算出netmask以及network  broadcast等IP参数。

[laopi@Think ~]$ ifconfig eth0 mtu 3000

仅修改接口的MTU数值,其他保持不便。

[laopi@Think ~]$ ifconfig eth0 192.168.1.111 netmask 255.255.255.0 network 192.168.1.0\>

                broadcast 192.168.1.255 mtu 3000

同时设置IP,Netmask,Network,Broadcast,MTU。(\> 表示换行)

[laopi@Think ~]$ ifconfig eth0:0 192.168.2.123

仔细看这个接口,eth0:0,那就是在网络接口(eth0)上,在仿真个网络接口,就可以在一个网卡上设置多个IP。

#route

[laopi@Think ~]$ route [-nee]        

[laopi@Think ~]$ route add [-net|-host] [网段或主机] netmask [mask] [gw|dev]

[laopi@Think ~]$ route del [-net|-host] [网段或主机] netmask [mask] [gw|dev]

参数解析:

-n, 不要使用通信协议或主机名,直接使用IP或port Number;

-ee, 使用更详细的信息来显示

增加(add)与删除(del)路由的相关参数;

-net, 表示后面接的路由为一个网段;

-host, 表示后面接的为连接到单台主机的路由;

netmask, 与网段有关,可以设置netmask决定网段的大小;

gw, gateway的缩写,后面接IP的数值,与dev不同;

dev, 如果只是要指定哪一块网卡联机出去,使用这个设置,后面接eth0等。

范例一:

[laopi@Think ~]$ route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.0     *               255.255.255.0   U     1      0        0 eth0

default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

[laopi@Think ~]$ route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0

0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

仔细观察两个命令不同的地方你会发现第一个的default与*在第二个里变成了0.0.0.0与0.0.0.0 

上面输出的数据共有8个字段,你应该注意的有几个地方。

Destination :其实就是network(网段,该网段的第一个IP)的意思。

Gateway :就是该接口的网关的那个IP,若为*或0.0.0.0 表示不需要额外的IP。

Genmask :就是netmask(子网掩码)了。

Flags :共有多少个旗标可以来表示该网段或主机代表的意思。

U :代表该路由可用。

G :代表该网段需要经由Gateway来转发。

H :代表该路由为一台主机,而非为一整个网络。

Iface :就是interface(接口)意思。

上面路由表显示的内容为当要发送数据包时路由表中有的网段(Destination下面非0.0.0.0的网段)就是与本机同一个网段,可以直接发送数据而不用经过路由器的网段,计算机要发送的数据直接通过后面接的端口发出,当要发送的网段路由表中没有记录则就会通过默认(default)网关(Gateway下面非0.0.0.0的地址)转发到目的网段。

举例: 

当要发送数据到IP地址为192.168.1.12(netmask为255.255.255.0)的计算机时,因为该计算机的IP地址所在的网段与路由表中记录的网段相同所以不用经过路由器直接发送数据到目的计算机,当要发送的数据的目的计算机IP地址为192.168.2.23(netmask为255.255.255.0)时,因为此IP所在的网段,路由表中没有记录所以就要通过路由器转发到目的计算机去,默认网关就为192.168.1.1这就是路由表的基本作用。

范例二:

[laopi@Think ~]$ route del -net 192.168.2.123 netmask 255.255.255.0 dev eth0

上面的这个操作可以删除192.168.2.0的这个网段,请注意在删除的时候,需要将路由表上面的信息都写入,包括netmask dev等。



重新导入ifcfg-eth0网络配置文件

[root@Jeffery]# /etc/init.d/network reload

Shutting down interface eth0:                              [ OK ]

Shutting down loopback interface:                       [ OK ]

Bringing up loopback interface:                           [ OK ]

Bringing up interface eth0:                                  [ OK ]


网卡接口关闭与激活(X代表网卡编号)

[root@Jeffery]# ifdown ethX   #关闭网络

[root@Jeffery]# ifup ethX        #启动网络

或者


ifconfig ethX down

[root@Jeffery]# ifconfig ethX up         #启动网络




网络服务启动与关闭

方法一:
[root@localhost ~]# service network stop 
[root@localhost ~]# service network start
[root@localhost ~]# service network restart
方法二:
[root@localhost ~]# /etc/init.d/network stop
[root@localhost ~]# /etc/init.d/network start
[root@localhost ~]# /etc/init.d/network restart


网卡状态查询

[root@localhost ~]# service network status
Configured devices:         (配置的设备)
lo eth0
Currently active devices:  (当前活动的设备)
lo eth0

临时配置网卡信息,无需重启。

[root@localhost ~]# ifconfig eth0 10.1.1.10 netmask 255.0.0.0


查看网卡接口信息,默认列出所有接口

[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:13:5D:74
          inet addr:192.168.1.11  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe13:5d74/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:413 errors:0 dropped:0 overruns:0 frame:0
          TX packets:572 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:47701 (46.5 KiB)  TX bytes:64842 (63.3 KiB)
          Base address:0x2000 Memory:d8920000-d8940000
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:16436  Metric:1
          RX packets:407 errors:0 dropped:0 overruns:0 frame:0
          TX packets:407 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:70759 (69.1 KiB)  TX bytes:70759 (69.1 KiB)

查看当前路由及网关信息

[root@localhost ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags          MSS         Window  irtt Iface
192.168.1.0       *               255.255.255.0      U                0 0                0 eth0
169.254.0.0       *               255.255.0.0          U                0 0                0 eth0
default          192.168.1.1      0.0.0.0               UG             0 0                0 eth0