路由和 Linux 网卡配置实战 | 学习笔记(三)

简介: 快速学习路由和 Linux 网卡配置实战 .

开发者学堂课程【Linux 高级网络应用 - 网络管理与配置实战路由和 Linux 网卡配置实战学习笔记,与课程紧密联系,让用户快速学习知识.

课程地址:https://developer.aliyun.com/learning/course/576/detail/7969


路由和 Linux 网卡配置实战


目录

一、动态主机配置协议 DHCP

二、基本网络配置

三、CentOS 6、CentOS 7网卡名称的区别

四、CentOS 6网卡名称

五、CentOS 6网络配置方式

六、配置网络接口

七、route 命令


七、route 命令

1.査看: route -n

由于路由器的每个接口一旦配上地址,相邻网段的路由记录会自动生成,故增加IP地址会自动进行路由的增加。

2.添加:route add

根据路由的分类:仅主机路由、网络路由、默认路由

route add[-net 仅主机路由|-host网络路由]

target [netmask Nm] [gw Gw] [[dev] If] 路由添加时要指定网关和接口

[root@centos6 ~]#route -host add 6.6.6.6  gw 3.3.3.100  

//假设6.6.6.6为网关地址,3.3.3.100为路由器地址,系统将自行判断需要经过的路径。仅主机方式。

Lroot@centos6 ~]#route -n

Kerne1 IP routing table

Destination  Gateway  Genmask    Flags Metric Ref  Use Iface

6.6.6.6   3.3.3.100 255.255.255.255 UGH   0    0    eth1

192.168.30.0  0.0.0.0 255.255.255.0   U   1    0    eth0

3.3.3.0     0.0.0.0   255.255.255.0  U   0     0    eth1

[root@centos6 ~]#route add -net 8.8.8.8/32 gw 3.3.3.100

//另外一种等价写法。网络方式

SIOCADDRT: Invalid argument

[root@centos6 ~]#route add -net 8.8.8.8 netmask 255.255.255.255 gw 3.3.3.100

[ root@centos6 ~]#route -n

Kernel IP routing tab le

Destination Gateway Genmaskm     Flags Metric Ref   Use Iface

6.6.6.6    100   255.255.255.255 UGH     0    0       eth1

8.8.8.8   100   255.255.255.255 UGH     0    0       eth1

192.168.30.0 0.0.0.0 255.255.255.0 U     1    0       eth0

3.3.3.0   0.0.0.0    255.255.255.0 U     0    0       eth1

//8.8.8.8由于其本身为一台机器的网络,存在网络的特殊性,故8.8.8.8与6.6.6.6实际相等,仅存在语法的不同。

[root@centos6 ~] #route add -net 10.0.0.0 netmask 255.0.0.0 gw

192.168.30.100[root@centos6 ~] #route -n 

//代表一个网段的表达方式,即添加了一条到达10.0.0.0网段的路由记录。

Kerne1 IP routing table

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

6.6.6.6 3.3.3.100 255.255.255.255 UGH    0     0    eth1

8.8.8.8 3.3.3.100 255.255.255.255 UGH    O     0    eth1

192.168.30.0 0.0.0.0  255.255.255.0 U    0     0    eth0

3.3.3.0  0.0.0.0.     255.255.255.0 U    0     0    eth1

10.0.0.0 192.168.30.200 255.0.0.0  UG   0     0     eth0

[root@centos6 ~] #route add -net 100.0.0.0 netmask 255.0.0.0 gw 192.168.3.100[root@centos6 ~] #route -n

Kerne1 IP routing table

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

6.6.6.6 3.3.3.100 255.255.255.255 UGH    0         eth1

8.8.8.8 3.3.3.100 255.255.255.255 UGH    O         eth1

192.168.30.0 0.0.0.0  255.255.255.0 U    0         eth0

3.3.3.0  0.0.0.0.     255.255.255.0 U    0         eth1

10.0.0.0 192.168.30.100  255.0.0.0. UG   0         eth0

[root@centos6 ~] #route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.30.200  

//下一个路由器的临近本身的接口和当前路由器应该处在同一个网段。当前机器的IP为30.128或3.3,与3.100不处于同一网段,应进行更改。如30.200

[root@centos6 ~] #route add default gw 192.168.30.200

[root@centos6 ~] #   -n

Kerne1 IP routing table

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

6.6.6.6 3.3.3.100 255.255.255.255 UGH    0         eth1

8.8.8.8 3.3.3.100 255.255.255.255 UGH    O         eth1

192.168.30.0 0.0.0.0  255.255.255.0 U    0         eth0

3.3.3.0  0.0.0.0.     255.255.255.0 U    0         eth1

10.0.0.0 192.168.30.100  255.0.0.0. UG   0         eth0

[root@centos6 ~] #route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.30.2  

//增加路由记录

以上格式同样可以改为-net 10.0.0.0 netmask 255.0.0.0,但不如前者简洁。

[root@centos6 ~] #route add -net 0.0.0.0 netmask 0.0.0.0 gw 3.3.3.100

删除

两个路由即两个到达位置网的路径,与对方连接时并不能明确具体线路,此时需要删除一条路径。

[root@centos6 ~] #route del default gw 192.168.30.2

[root@centos6 ~] #   -n

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

6.6.6.6 3.3.3.100 255.255.255.255 UGH    0     0    eth1

8.8.8.8 3.3.3.100 255.255.255.255 UGH    O     0    eth1

192.168.30.0 0.0.0.0  255.255.255.0 U    0     0    eth0

3.3.3.0  0.0.0.0.     255.255.255.0 U    0     0    eth1

100.0.0.0 192.168.30.200 255.0.0.0  UG   0     0    eth0

0.0.0.0  3.3.3.100    0.0.0.0     UG     0     0    eth1

[root@centos6 ~] #   -n

[root@centos6 ~] #route dle -host 6.6.6.6

[root@centos6 ~] #   -n

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

8.8.8.8 3.3.3.100 255.255.255.255 UGH    O     0    eth1

192.168.30.0 0.0.0.0  255.255.255.0 U    0     0    eth0

3.3.3.0  0.0.0.0.     255.255.255.0 U    0     0    eth1

100.0.0.0 192.168.30.200 255.0.0.0  UG   0     0    eth0

0.0.0.0  3.3.3.100    0.0.0.0     UG     0     0    eth1

[root@centos6 ~] #route dle -net 100.0.0.0 netmask 255.255.0.0  

//网段删除

[root@centos6 ~] # -n

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

8.8.8.8 3.3.3.100 255.255.255.255 UGH    O     0    eth1

192.168.30.0 0.0.0.0  255.255.255.0 U    0     0    eth0

3.3.3.0  0.0.0.0.     255.255.255.0 U    0     0    eth1

0.0.0.0  3.3.3.100    0.0.0.0     UG     0     0    eth1

此些记录并非真正被保存,当电脑进行重启后,记录将会失去,仅系统自带的将被保存。而当通过网络服务重启的方法时,丢失的是部分命令。

[root@centos7~] # -n

Kerne1 IP routing table

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

0.0.0.0      172.20.0.1  0.0.0.0  UG    100   0     ens37

172.20.0.0   0.0.0.0  255.255.0.0 U     100   0     ens37

192.168.30.0 0.0.0.0  255.255.255.0 U   100   0     ens33

[root@centos7 ~] #route del default gw 172.20.0.1

[root@centos7 ~] # -n

Kerne1 IP routing table

Destination  Gateway  Genmask   Flags  Metric Ref  Use Iface

172.20.0.0   0.0.0.0  255.255.0.0 U     100   0     ens37

192.168.30.0 0.0.0.0  255.255.255.0 U   100   0     ens33

进行centos6、7地址的各自配备

centos7

ens37:192.168.100/24

centos6

eyth1:192.168.2.100/24

两台主机经分辨可得出不在同一网段的结论。可使用ifconfig进行地址的临时该改变。

[root@centos7~] #ifconfog ens37 192.168.1.100.24/  

//临时改变地址

[root@centos7~] #route -n

//查看路由,发现自动添加了本机器新IP对应的网段路由

[root@centos6~] #ifconfig eth1 192.168.2.100/24

[root@centos6~] #ifconfig

[root@centos6~] #route -n //查看路由,发现本机器同样自动添加了新IP对应的网段路由

由于两台主机不处于同一网络网段中,故在centos7中无路径,进行ping192.168.2.100操作会出现网络不可到达的提示。物理上两主机是相通的,使路由配通,即添加路由,即可实现此目的。

[root@centos7~]#ping192.168.2.100

[root@centos7~]#route add default dev ens37

[root@centos7~]#ping192.168.2.100 //再次进行ping命令

相当于现今两台电脑物理上相连于同一台交换机,但各自配置了不同的网段IP。相通理论上应加网关,正常情况下应该配置路由器。如今仅添加了路由,没有路由器。

只要通过ens37网卡发出,就能到达任何未知网络,ens37于centos6的两个主机位于同一网段,通过arp广播形式寻找,实现了相通。

此种情况出现时添加网关即可解决。

[root@centos6~]#route add default dev eth1

//加了路由未加网关,仅加了接口

路由表十分重要,当路由表拥有路径之后,便可按照其内里定义的路径进行通讯。在计算机中的通讯即由路由表转化数据实现。

一般情况下,添加地址就会自动生成路由记录。但由于没有对应127.0.0.1的地址,即每人访问此地址时不会通向互联网,尽在局域网中本地打转。

由于子网掩码为255.0.0.0,意味着但凡为127开头都无法走出局域网通向互联网。

现今使网卡centos6和centos7分别回归默认

[root@centos7 ~]#ifconfig lo 6.6.6.6/32

//增加回环网卡

[root@centos6 ~]#ifconfig lo 8.8.8.8/32 

//再次在centos6中为回环网卡增加地址,则centos6中地址为192.168.2.100,centos7中地址为192.168.30.100

[root@centos7 ~]#ping 8.8.8.8 //若在centos7中进行8.8.8.8的ping操作,系统显示不存在到达8.8.8.8的路由,为实现目的,进行默认路由到添加。

[root@centos7 ~]#route -n

[root@centos7 ~]#route add default fev ens37

[root@centos7 ~]#ping 8.8.8.8

//再次进行ping操作

[root@centos6 ~]#tcpdump -i eth1 icmp

//抓包命令

查询可知发出的数据包centos6处已然接收到。面对此种有去无回的情况,在回去的时候同样需要添加路由。

[root@centos6~]#route add default dev eth1

此时,两个主机进行了想通

加网关、IP地址的本质是为了添加路由。若路由出现错误,即使配配备了地址同样不能够使用。路由为重中之重。

现今centos7存在两个地址:192.168.30.100172.20.102.77

centos6中存在两个地址:192.168.30.128;172.18.108.3

两地址不相通

[root@centos7 ~]#ping 192.168.30.128

//两主机都为仅主机模式,可以进行正常的相ping命令。

[root@centos7 ~]#delete del -net 192.168.30.0 netmask 255.255.255.0 dev ens33

//此删除执行后,网络被断掉

尽管地址表面上仍然能够查询,但路由被删掉,故此时进行ping操作,不能实现相同。

此种情况中,IP地址不存在任何问题,但由于路由出现状况,使得命令无法正常执行。

3.目标: 192.168.1.3 网关: 172.16.0.1

route add -host 192.168.1.3 qw 172.16.0.1 dev ethO

目标:192.168.0.0网关:172.16.0.1

route add -net 192.168.0.0 netmask 255.255.255.0

相关文章
|
8天前
|
安全 Ubuntu Java
Linux配置使用篇
Linux配置使用篇
|
20天前
|
存储 负载均衡 索引
linux7安装elasticsearch-7.4.0集群配置
linux7安装elasticsearch-7.4.0集群配置
108 0
|
5天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
17 6
|
25天前
|
存储 Shell Linux
【Shell 命令集合 系统管理 】Linux 修改用户的属性和配置 usermod命令 使用指南
【Shell 命令集合 系统管理 】Linux 修改用户的属性和配置 usermod命令 使用指南
30 1
|
7天前
|
网络协议 Linux
在Linux中,管理和配置网络接口
在Linux中管理网络接口涉及多个命令,如`ifconfig`(在新版本中被`ip`取代)、`ip`(用于网络设备配置)、`nmcli`(NetworkManager的CLI工具)、`nmtui`(文本界面配置)、`route/ip route`(处理路由表)、`netstat/ss`(显示网络状态)和`hostnamectl/systemctl`(主机名和服务管理)。这些命令帮助用户启动接口、设置IP地址、查看连接和路由信息。不同发行版可能有差异,建议参考相应文档。
19 4
|
10天前
|
域名解析 监控 网络协议
Linux网卡与IP地址:通往网络世界的通行证 🌐
探索Linux网卡与IP地址关系,理解网卡作为网络通信的关键。Linux网卡需配置IP地址以实现唯一标识、通信、路由、安全管理和网络服务。无IP地址时,网卡在特定情况如局域网服务、网络监控、无线认证和网络启动可有限工作,但通用功能受限。配置IP地址通常通过`ifconfig`(传统)或`ip`(现代)命令,永久配置需编辑网络配置文件。配置错误如IP冲突、子网掩码错误、默认网关和DNS配置不当可能导致服务中断、网络拥堵、安全漏洞和数据丢失。重视网络配置的正确与安全至关重要。
Linux网卡与IP地址:通往网络世界的通行证 🌐
|
18天前
|
网络协议 Linux 网络安全
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
Linux服务器DNS服务器配置实现bind的正向解释和反向解释
17 0
|
25天前
|
存储 Linux Shell
【Shell 命令集合 系统设置 】Linux 配置和管理时区 timeconfig命令 使用指南
【Shell 命令集合 系统设置 】Linux 配置和管理时区 timeconfig命令 使用指南
37 0
|
25天前
|
Linux Shell C语言
【Shell 命令集合 系统设置 】Linux 配置声音卡sndconfig命令 使用指南
【Shell 命令集合 系统设置 】Linux 配置声音卡sndconfig命令 使用指南
29 0
|
25天前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】Linux 配置鼠标设备的相关设置 mouseconfig命令 使用指南
【Shell 命令集合 系统设置 】Linux 配置鼠标设备的相关设置 mouseconfig命令 使用指南
31 0