
linux route命令 用于显示和 *** 作IP路由表 。
要实现 两个不同子网之间的通信 ,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。
在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址 设置为 Linux机器的默认路由 。
格式:
[root@linux ~]# route [-nee]
[root@linux ~]# route add [-net|-host] [网域或主机] netmask [mask] [gw|dev]
[root@linux ~]# route del [-net|-host] [网域或主机] netmask [mask] [gw|dev]
1、 查看相关参数:
-n :不要使用通讯协定或主机名称,直接使用 IP 或 port number;
-ee :使用更详细的资讯来显示
2、增加 (add) 、删除 (del) 路由的相关参数:
-net :表示后面接的路由为一个网域;
-host :表示后面接的为连接到单部主机的路由;
netmask :与网域有关,可以设定 netmask 决定网域的大小;
gw :gateway 的简写,后续接的是 IP 的数值喔,与 dev 不同;
dev :如果只是要指定由那一块网卡连线出去,则使用这个设定,后面接 eth0 等
设置默认路由
格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}
其中,
参数{IP-ADDRESS): 用于指定路由器(网关)的IP地址;
参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。使用/sbin/ifconfig -a可以显示所有接口信息。
例:
添加到指定网络的路由规则
格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
其中
参数{NETWORK-ADDRESS}: 用于指定网络地址
参数{NETMASK}: 用于指定子网掩码
参数{INTERFACE-NAME}: 用于指定接口名称,如eth0。
例:
屏蔽一条路由,设置到指定网络为不可达 ,避免在连接到这个网络的地址时程序过长时间的等待
格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject
例:
删除路由设置 ,参数指定的方式与route add相似。
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject
例:
注意的是 ,直接在命令行下执行route命令来添加路由, 不会永久保存 ,当网卡重启或者机器重启之后,该路由就 失效 了; 要想永久保存 ,有如下方法:
1. 在/etc/rc.local里添加
2. 在/etc/sysconfig/network里添加到末尾
3. /etc/sysconfig/static-router :
any net x.x.x.x/24 gw y.y.y.y
Router>enable 进入特权模式Router#configure terminal 进入配置模式
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#interface e 0/0 进入接口以太0/0
Router(config-if)#ip
Router(config-if)#ip address 192.168.1.1 255.255.255.0 配置IP地址和子网掩码
Router(config-if)#no shutdown 开启端口
%LINK-3-UPDOWN: Interface Ethernet0/0, changed state to up
Router(config-if)#exit退出
Router(config)#ip host xiay 192.168.1.2默认域名指向(可以不配置的)
Router(config)#exit退出
Router#show interfaces e 0/0查看以太0/0信息
Ethernet0/0 is up, line protocol is up开启端口
Hardware is Lance, address is 000C.4198.5973 (bia 000C.4198.5973)MAC地址
Internet address is 192.168.1.1/24IP和掩码
MTU 1500 bytes, BW 10000 Kbit, DLY 1000 usec, rely 255/255, load 1/255
Encapsulation ARPA, loopback not set, keepalive set (10 sec)
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:00:00, output 00:00:00, output hang never
Last clearing of show interface counters never
Queueing strategy: fifo
Output queue 0/40, 0 dropsinput queue 0/75, 0 drops
5 minute input rate 1000 bits/sec, 2 packets/sec
5 minute output rate 1000 bits/sec, 2 packets/sec
0 packets input, 0 bytes, 0 no buffer
Received 0 broadcasts, 0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
0 input packets with dribble condition detected
0 packets output, 0 bytes, 0 underruns
0 output errors, 0 collisions, 0 interface resets
0 babbles, 0 late collision, 0 deferred
0 lost carrier, 0 no carrier
0 output buffer failures, 0 output buffers swapped out
Router#ping 192.168.1.2pingIP地址
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms通了
Router#ping xiayping 域名(就是刚才定向的那个)
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms通了
Router#copy startup-config tftp 拷贝我的配置文件到tftp
Address or name of remote host []?xiay拷贝到这(也可以输入IP)
Destination filename []?1保存文件名
No NVRAM configuration present失败
Router#
Router#show startup-config 检查我的配置 没有
%% Non-volatile configuration memory is not present
Router#copy running-config startup-config 把我ram上的配置copy到nvram上去
Destination filename [startup-config]?
Building configuration...
[OK]
Router#sh startup-config 再查看下 OK东西有了
Current configuration : 677 bytes
!
Version 12.1
service timestamps debug uptime
service timestamps log uptime
no service password-encryption
!
hostname Router
!
!
!
ip subnet-zero
ip host xiay 192.168.1.2
!
!
!
!
!
!
!
!
!
Router#copy startup-config tftp 拷贝 同上
Address or name of remote host []?xiay
Destination filename []?1
!!!!!!!!!!成功了!!!!!!!
[OK]
8390 bytes copied in 7 secs
===================================
boson 行敲出来顺便给你贴上去的 够详细了把 不加分对不起我啊
平时工作经常需要配置路由,接下来总结一下linux服务器配置路由的方法。
1、首先看一下本机的网路环境网络信息及路由信息:ip addrroute -n。
2、接着使用route命令添加一条静态路由:route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.2.1。
3、然后使用route命令添加一条路由,并指定eth1网卡:route add -net 192.168.4.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth1。
4、接着使用route命令添加一条指向某一个主机的路由:route add -host 192.168.5.125 gw 192.168.2.1 。
5、最后使用ip命令添加一条静态路由,并指定从eth1网卡收发数据:ip route add 192.168.7.0/24 via 192.168.2.1 dev eth1,如下图所示添加静态路由就完成了。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)