临时配置IP地址

1)网络接口控制
2)查看网卡eth0的配置信息、禁用网卡eth0
3)列出所有网络接口,确认启用状态
4)激活网卡eth0,将地址设置为 192.168.4.5/24
5)为网卡eth0添加一个IP地址 192.168.8.5/24
6)为本机设置静态路由记录
7)访问网段200.0.0.0/24可经192.168.8.100抵达
8)默认网关的IP地址为 192.168.8.254
临时配置:简单快速,可直接更改运行中的地址参数,适合在调试网络的过程中使用;系统重启以后,所做的修改将会失效。
配置静态路由时,下一跳地址一定要是本网段地址。目标地址要写网段ID,必须写上子网掩码。配置网关地址时,网关地址必须为本网段地址。
步骤

实现此案例需要按照如下步骤进行。

步骤一:网络接口控制

查看网卡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)

步骤二:为网卡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 桌面]#

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值