步骤
实现此案例需要按照如下步骤进行。
步骤一:网络接口控制
查看网卡eth0的配置信息、禁用网卡eth0,命令操作如下所示:
root@localhost 桌面]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:0D:06:9A
inet addr:192.168.30.124 Bcast:192.168.30.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe0d:69a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:174884 errors:0 dropped:0 overruns:0 frame:0
TX packets:920 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12269223 (11.7 MiB) TX bytes:91973 (89.8 KiB)
[root@localhost 桌面]# ifconfig eth0 down
[root@localhost 桌面]# ifconfig
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:105 errors:0 dropped:0 overruns:0 frame:0
TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8792 (8.5 KiB) TX bytes:8792 (8.5 KiB)
列出所有网络接口,确认启用状态,命令操作如下所示:
[root@localhost 桌面]# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:0C:29:0D:06:9A
inet addr:192.168.30.124 Bcast:192.168.30.255 Mask:255.255.255.0
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:175179 errors:0 dropped:0 overruns:0 frame:0
TX packets:920 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12289943 (11.7 MiB) TX bytes:91973 (89.8 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:16436 Metric:1
RX packets:105 errors:0 dropped:0 overruns:0 frame:0
TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8792 (8.5 KiB) TX bytes:8792 (8.5 KiB)
[root@localhost 桌面]#
激活网卡eth0,将地址设置为192.168.4.5/24,命令操作如下所示:
[root@localhost 桌面]# ifconfig
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:105 errors:0 dropped:0 overruns:0 frame:0
TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8792 (8.5 KiB) TX bytes:8792 (8.5 KiB)
[root@localhost 桌面]# ifconfig eth0 up
[root@localhost 桌面]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:0D:06:9A
inet addr:192.168.30.124 Bcast:192.168.30.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe0d:69a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:175553 errors:0 dropped:0 overruns:0 frame:0
TX packets:927 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12315815 (11.7 MiB) TX bytes:92551 (90.3 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:16436 Metric:1
RX packets:105 errors:0 dropped:0 overruns:0 frame:0
TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8792 (8.5 KiB) TX bytes:8792 (8.5 KiB)
[root@localhost 桌面]# ifconfig eth0 192.168.4.5/24
[root@localhost 桌面]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:0D:06:9A
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe0d:69a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:177266 errors:0 dropped:0 overruns:0 frame:0
TX packets:927 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12438744 (11.8 MiB) TX bytes:92551 (90.3 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:16436 Metric:1
RX packets:105 errors:0 dropped:0 overruns:0 frame:0
TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:8792 (8.5 KiB) TX bytes:8792 (8.5 KiB)
[root@localhost 桌面]#
步骤二:为网卡eth0添加一个IP地址 192.168.8.5/24
命令操作如下所示:
[root@localhost 桌面]# ifconfig eth0 192.168.8.5/24
[root@localhost 桌面]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:0D:06:9A
inet addr:192.168.8.5 Bcast:192.168.8.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe0d:69a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:182405 errors:0 dropped:0 overruns:0 frame:0
TX packets:927 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12804365 (12.2 MiB) TX bytes:92551 (90.3 KiB)
[root@localhost 桌面]#
步骤三:为本机设置静态路由记录
访问网段200.0.0.0/24可经192.168.8.100抵达。
添加一条静态路由route add -net 200.0.0.0/24 gw 192.168.8.100 ,相关说明如下所示:
route add:添加一条路由条目;
-net:目标网络ID和子网掩码;
gw:下一跳地址。
命令操作及输出结果如下所示:
[root@localhost 桌面]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.8.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
[root@localhost 桌面]# route add -net 200.0.0.0/24 gw 192.168.8.100
[root@localhost 桌面]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
200.0.0.0 192.168.8.100 255.255.255.0 UG 0 0 0 eth0
192.168.8.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
默认网关的IP地址为 192.168.8.254。
分析:为PC机配置网关,而对PC机而言数据只要是跨网通信都必须交给网关处理,这很像路由中的默认路由的概念。所以在Linux中为PC机配置网关,其实就是配置一条默认路由。
命令操作如下所示:
[root@localhost 桌面]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
200.0.0.0 192.168.8.100 255.255.255.0 UG 0 0 0 eth0
192.168.8.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
[root@localhost 桌面]# route add default gw 192.168.8.254
[root@localhost 桌面]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
200.0.0.0 192.168.8.100 255.255.255.0 UG 0 0 0 eth0
192.168.8.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 192.168.8.254 0.0.0.0 UG 0 0 0 eth0
[root@localhost 桌面]#