Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10494898
  • 博文数量: 2905
  • 博客积分: 20098
  • 博客等级: 上将
  • 技术积分: 36298
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-23 05:00
文章存档

2012年(1)

2011年(3)

2009年(2901)

分类: LINUX

2009-03-23 10:53:04

一、设置IP

我们可以为网卡设置固定的IP,也可设置一个用于测试的临时IP。要设置一个固定IP,可以通过命令setup和netconfig或通过修过配置文件来完成。

1、本例我们假设已配置了第1个网卡的IP,将为第2个网卡配置IP地址。过程如下:
[root@OracleOnLinux root]# cd /etc/sysconfig/network-scripts/   
[root@OracleOnLinux network-scripts]# ls    //查看当前目录中的文件,可以看到没有第2个网卡的配置文件
ifcfg-eth0      ifdown-ipv6  ifup          ifup-isdn    ifup-sit
ifcfg-lo        ifdown-isdn  ifup-aliases  ifup-plip    ifup-sl
ifdown          ifdown-post  ifup-cipcb    ifup-plusb   ifup-wireless
ifdown-aliases  ifdown-ppp   ifup-ippp     ifup-post    init.ipv6-global
ifdown-cipcb    ifdown-sit   ifup-ipv6     ifup-ppp     network-functions
ifdown-ippp     ifdown-sl    ifup-ipx      ifup-routes  network-functions-ipv6

[root@OracleOnLinux network-scripts]# cat ifcfg-eth0  //通过配置文件查看eth0的配置
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.154
NETMASK=255.255.255.0
GATEWAY=192.168.0.254

[root@OracleOnLinux network-scripts]# cp ifcfg-eth0 ifcfg-eth1  //拷贝一份以建立第二个网卡eth1的配置文件
[root@OracleOnLinux network-scripts]# vi ifcfg-eth1           //修改原配置文件为以下内容

DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.16.0.254
NETMASK=255.255.255.0
GATEWAY=172.16.0.1


~
"ifcfg-eth1" [已转换] 6L, 101C 已写入                         
[root@OracleOnLinux network-scripts]# service network restart     //重启网络服务,使修改生效
正在关闭接口 eth0: [  确定  ]
正在关闭接口 eth1: [  确定  ]
关闭环回接口: [  确定  ]
设置网络参数: [  确定  ]
弹出环回接口: [  确定  ]
弹出界面 eth0: [  确定  ]
弹出界面 eth1: [  确定  ]

[root@OracleOnLinux network-scripts]# ifconfig   //查看当前IP的配置,可见第二个网卡的IP已配置成功
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2E:70:80  
          inet addr:192.168.0.154  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:963 errors:0 dropped:0 overruns:0 frame:0
          TX packets:664 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:87678 (85.6 Kb)  TX bytes:67862 (66.2 Kb)
          Interrupt:10 Base address:0x1080

eth1      Link encap:Ethernet  HWaddr 00:0C:29:2E:70:8A  
          inet addr:172.16.0.254  Bcast:172.16.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:192 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:15035 (14.6 Kb)  TX bytes:168 (168.0 b)
          Interrupt:9 Base address:0x1400

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

[root@OracleOnLinux network-scripts]# ifconfig | grep inet   //只查看IP部分
          inet addr:192.168.0.154  Bcast:192.168.0.255  Mask:255.255.255.0
          inet addr:172.16.0.254  Bcast:172.16.0.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0


2、现在我们来设置一个临时用于测试的IP
[root@OracleOnLinux network-scripts]# ifconfig eth0:1 10.0.0.254 netmask 255.255.255.0    //对eth0再分配一个临时IP
[root@OracleOnLinux network-scripts]# ifconfig | more
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2E:70:80  
          inet addr:192.168.0.154  Bcast:192.168.0.255  Mask:255.255.255.0
        ……

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:2E:70:80    
      //可见临时IP已设置好
          inet addr:10.0.0.254  Bcast:10.255.255.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:269 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:20922 (20.4 Kb)  TX bytes:168 (168.0 b)
          Interrupt:10 Base address:0x1080

eth1      Link encap:Ethernet  HWaddr 00:0C:29:2E:70:8A  
          inet addr:172.16.0.254  Bcast:172.16.0.255  Mask:255.255.255.0
         ……

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          ……


[root@OracleOnLinux network-scripts]# service network restart  //重启网络服务
[root@OracleOnLinux network-scripts]# ifconfig | more   //再次查看设置的临时IP已消失
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2E:70:80  
          inet addr:192.168.0.154  Bcast:192.168.0.255  Mask:255.255.255.0
        ……

eth1      Link encap:Ethernet  HWaddr 00:0C:29:2E:70:8A  
          inet addr:172.16.0.254  Bcast:172.16.0.255  Mask:255.255.255.0
         ……

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          ……

===================================================================================================
二、设置路由

1、查看本地路由
[root@linux-tys root]# netstat -rn       //查看本机路由表,也可用route命令
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
172.16.0.0      0.0.0.0         255.255.255.0   U         0 0          0 eth1
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
12.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 eth0
0.0.0.0         192.168.0.254   0.0.0.0         UG        0 0          0 eth0
0.0.0.0         12.0.0.254      0.0.0.0         UG        0 0          0 eth0


2、添加一条路由
[root@linux-tys root]# route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.0.154 dev eth0  //添加一个网络路由
[root@linux-tys root]# route add -host 172.16.0.169 gw 172.16.0.254              //添加一个主机路由
[root@linux-tys root]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
172.16.0.169    172.16.0.254    255.255.255.255 UGH       0 0          0 eth1
192.168.3.0     192.168.0.154   255.255.255.0   UG        0 0          0 eth0
172.16.0.0      0.0.0.0         255.255.255.0   U         0 0          0 eth1
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
12.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 eth0
0.0.0.0         192.168.0.254   0.0.0.0         UG        0 0          0 eth0
0.0.0.0         12.0.0.254      0.0.0.0         UG        0 0          0 eth0

---------------------------------
注:
Flags:路由标志。其中U表示路由项是活动的;H表示目标为单个主机;G表示使用网关;R表对动态路由进行修复;D表示路由项是动态安装的;M表示动态修改路由;!表示拒绝路由。
Ref:依赖于本路由的其它路由数目。
Use:该路由项被使用的次数。
Iface:该路由项发送数据包使用的网络接口。
-------------------------------------------------------
[root@linux-tys root]# route del -net 192.168.3.0 netmask 255.255.255.0        //删除网络路由
[root@linux-tys root]# route del -host 172.16.0.169       //删除主机路由
[root@linux-tys root]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
172.16.0.0      0.0.0.0         255.255.255.0   U         0 0          0 eth1
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
12.0.0.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U         0 0          0 lo
0.0.0.0         192.168.0.1     0.0.0.0         UG        0 0          0 eth0
0.0.0.0         192.168.0.254   0.0.0.0         UG        0 0          0 eth0
0.0.0.0         12.0.0.254      0.0.0.0         UG        0 0          0 eth0


三、用命令ping测试网络连通性

[root@linux-tys root]# ping 192.168.0.16      //本命令需用用ctrl+c中止
PING 192.168.0.16 (192.168.0.16) 56(84) bytes of data.
64 bytes from 192.168.0.16: icmp_seq=1 ttl=64 time=0.550 ms
64 bytes from 192.168.0.16: icmp_seq=2 ttl=64 time=0.341 ms
64 bytes from 192.168.0.16: icmp_seq=3 ttl=64 time=0.222 ms
64 bytes from 192.168.0.16: icmp_seq=4 ttl=64 time=0.220 ms
64 bytes from 192.168.0.16: icmp_seq=5 ttl=64 time=0.208 ms
64 bytes from 192.168.0.16: icmp_seq=6 ttl=64 time=0.274 ms

--- 192.168.0.16 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5095ms
rtt min/avg/max/mdev = 0.208/0.302/0.550/0.120 ms
[root@linux-tys root]# ping --help        //查看帮助
ping: invalid option -- -
Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
            [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
            [-M mtu discovery hint] [-S sndbuf]
            [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
[root@linux-tys root]# ping -c 4 -s 1000 192.168.0.16
PING 192.168.0.16 (192.168.0.16) 1000(1028) bytes of data.
1008 bytes from 192.168.0.16: icmp_seq=1 ttl=64 time=0.314 ms
1008 bytes from 192.168.0.16: icmp_seq=2 ttl=64 time=0.251 ms
1008 bytes from 192.168.0.16: icmp_seq=3 ttl=64 time=0.231 ms
1008 bytes from 192.168.0.16: icmp_seq=4 ttl=64 time=0.233 ms

--- 192.168.0.16 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3058ms
rtt min/avg/max/mdev = 0.231/0.257/0.314/0.035 ms

四、命令netstat的使用


1、netstat命令常用参数简介
1) –t  显示当前TCP协议连接情况
2) –l  查看系统监听情况
3)–ln 同上,但将以数字形式表示端口信息
4)netstat –i  查看网卡传送、接收数据包的情况
5) –an 查看所有连接,并以数字形式表示端口信息
6) –r  查看本机的路由

2、netstat命令使用示例
[root@linux-tys root]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:1024          0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:1025            0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      
tcp        0     52 192.168.0.154:22        192.168.0.16:1059       ESTABLISHED
tcp        0      0 192.168.0.154:22        192.168.0.16:1049       ESTABLISHED
udp        0      0 0.0.0.0:1024            0.0.0.0:*                           
udp        0      0 0.0.0.0:10000           0.0.0.0:*                           
udp        0      0 0.0.0.0:846             0.0.0.0:*                           
udp        0      0 0.0.0.0:111             0.0.0.0:*                           
udp        0      0 0.0.0.0:631             0.0.0.0:*                           
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     8474   /tmp/.esd/socket
unix  13     [ ]         DGRAM                    1580   /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     7552   /tmp/.fam_socket
unix  2      [ ACC ]     STREAM     LISTENING     2161   /tmp/.font-unix/fs7100
unix  2      [ ACC ]     STREAM     LISTENING     8553   /tmp/ssh-XXVkjhRL/agent.4243
unix  2      [ ACC ]     STREAM     LISTENING     6092   /tmp/ssh-XXG3eSwq/agent.3773
unix  2      [ ]         STREAM                   8473   
unix  3      [ ]         STREAM     CONNECTED     7727   /tmp/.famaJaaeu
unix  3      [ ]         STREAM     CONNECTED     7726   
unix  2      [ ]         DGRAM                    5428   
unix  2      [ ]         DGRAM                    5165   
unix  2      [ ]         DGRAM                    4860   
unix  2      [ ]         DGRAM                    2237   
unix  2      [ ]         DGRAM                    2192   
unix  2      [ ]         DGRAM                    2179   
unix  2      [ ]         DGRAM                    2055   
unix  2      [ ]         DGRAM                    2013   
unix  2      [ ]         DGRAM                    1771   
unix  2      [ ]         DGRAM                    1630   
unix  2      [ ]         DGRAM                    1595   

[root@linux-tys root]# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 tys.linux.com:ssh       192.168.0.16:1059       ESTABLISHED
tcp        0      0 tys.linux.com:ssh       192.168.0.16:1049       ESTABLISHED

[root@linux-tys root]#
                                                                             -----待续--------

阅读(726) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~