路由和 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

相关文章
|
3月前
|
Ubuntu Linux 网络安全
Linux服务器之Ubuntu的安装与配置
Ubuntu Desktop是目前最成功、最流行的图形界面的Linux发行版;而Ubuntu Server也在服务器端市场占据了较大的份额。今天为大家详细介绍了Ubuntu Server的安装与配置,希望对你能有所帮助。关于VMware、VirtualBox等虚拟化软件的使用,朱哥还会在后续的文章中为大家详细介绍,敬请关注!
|
1月前
|
存储 Linux 开发工具
Linux环境下使用Buildroot配置软件包
使用Buildroot可以大大简化嵌入式Linux系统的开发和维护工作,但它需要对Linux系统和交叉编译有深入的理解。通过上述步骤,可以有效地配置和定制软件包,为特定的嵌入式应用构建高效、稳定的系统。
196 11
|
4月前
|
Kubernetes Linux 网络安全
Rocky Linux 8.9配置Kubernetes集群详解,适用于CentOS环境
初始化成功后,记录下显示的 `kubeadm join`命令。
266 0
|
6月前
|
关系型数据库 MySQL Java
安装和配置JDK、Tomcat、MySQL环境,以及如何在Linux下更改后端端口。
遵循这些步骤,你可以顺利完成JDK、Tomcat、MySQL环境的安装和配置,并在Linux下更改后端端口。祝你顺利!
459 11
|
5月前
|
Linux 网络安全 开发工具
在Linux下配置gitee与Github的远程仓库
注意,git push后,是输入你的账号与密码。这个步骤可以通过特殊设置省去,但是一开始还是不要太省。
317 0
|
7月前
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
320 10
|
7月前
|
人工智能 Kubernetes Ubuntu
linux配置IP
linux配置IP
702 1
|
8月前
|
监控 Shell Linux
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
|
Linux 开发工具 数据安全/隐私保护
阿里云linux实战体验报告
通过阿里云 linux 实战教学体验提升自我
202 1