当前位置: 移动技术网 > 网络运营>服务器>Linux > 荐 Linux网络设置详解

荐 Linux网络设置详解

2020年07月20日  | 移动技术网网络运营  | 我要评论

前言

[Linux操作系统是当前主流的操作系统之一,不仅在服务器领域占据绝对的优势,而且在桌面领域也发展迅猛。《Linux网络服务配置详解》着重介绍了Linux操作系统下各种网络服务的功能及实用性配置,采用了大量的企业案例进行分析、重现,在讲解上由浅入深、循序渐进,并配有大量的实验,以方便用户进行实际操作,从实践中掌握各种网络服务的功能及配置方法。 《Linux网络服务配置详解》基本上每一章末尾都配有一个综合案例,如bind服务器的view视图案例、网络自动安装服务器构架、apache和tomcat整合后虚拟主机的配置、企业级邮件服务器应用案例等。

云计算

用户可以通过网络灵活获取计算机资源池(CPU、内存、存储、带宽)中的资源,并且具有按量计费和弹性伸缩的特性。

一:查看网络接口信息

1.1使用ifconfig 命令查看网络接口地址

  • 主机的网络接口卡(网卡)通常称为"网络接口"。
  • 查看所有活动网络接口的信息
  • ifconfig命令不带任何选项和参数时,将显示当前主机中已启用(活动)的网络接口信息
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.41  netmask 255.255.255.0  broadcast 20.0.0.255
        inet6 fe80::fce1:77f:7a9:b103  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:84:c1:e3  txqueuelen 1000  (Ethernet)
        RX packets 40289  bytes 21460824 (20.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5081  bytes 779870 (761.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1301  bytes 109664 (107.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1301  bytes 109664 (107.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:95:60:04  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
#en33:是第一块以太网卡的名称 
inet 20.0.0.41:这个是IP地址 
netmask 255.255.255.0:毋庸置疑了这是子网掩码
broadcast 20.0.0.255:这是广播地址
ether 00:0c:29:84:MAC地址
lo:Loopback缩写 回环网卡 测试TCP/IP协议是否正常     位于网络最底层 

查看所有网络的接口信息

[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.41  netmask 255.255.255.0  broadcast 20.0.0.255
        inet6 fe80::fce1:77f:7a9:b103  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:84:c1:e3  txqueuelen 1000  (Ethernet)
        RX packets 40722  bytes 21580609 (20.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5111  bytes 783487 (765.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1301  bytes 109664 (107.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1301  bytes 109664 (107.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:95:60:04  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0-nic: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:95:60:04  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

查看指定网络接口信息

当只需要查看其中一个网络信息时,可以使用网络接口的名称作为ifconifg命令的参数,列如,执行“ifconifg ens33可以只查看ens33的配置信息

[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.41  netmask 255.255.255.0  broadcast 20.0.0.255
        inet6 fe80::fce1:77f:7a9:b103  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:84:c1:e3  txqueuelen 1000  (Ethernet)
        RX packets 41084  bytes 21675143 (20.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5146  bytes 789379 (770.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

1.2网卡介绍

vmnet 0:(桥接)把虚拟机暴露在局域网环境下,可以直接访问

vmnet 1:(仅主机)把虚拟机之间构建成局域网,无法访问外部网络

vmnet 8:(nat) 把虚拟机连接到三层交换的虚拟网卡上,不仅可以内部通信还可以外部通信。

1.3查看主机名称hostname

显示和设置系统的主机名

hostname命令 用于显示和设置系统的主机名称。环境变量HOSTNAME也保存了当前的主机名。在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器之后还是原来的主机名。如果需要永久修改主机名,需要同时修改/etc/hosts/etc/sysconfig/network的相关内容。

语法

hostname【主机名】

选项

-v:详细信息模式;
-a:显示主机别名;
-d:显示DNS域名;
-f:显示FQDN名称;
-i:显示主机的ip地址;
-s:显示短主机名称,在第一个点处截断;
-y:显示NIS域名。

参数

主机名:指定要设置的主机名。

实例

临时修改主机名
hostname 主机名

永久修改主机名
hostnamectl set-hostname 主机名
实例:
 ##永久修改
[root@localhost ~]# hostnamectl set-hostname shuai    
[root@localhost ~]# su
[root@shuai ~]#
##临时修改
[root@shuai ~]# hostname hua
[root@shuai ~]# su
[root@hua ~]# 

修改配置文件
[root@hua ~]# vim /etc/hostname
进入编辑主机名重启即可

1.4查看路由表route

  • 直接执行route命令可以查看到当前主机中的路由表信息

  • linux系统中的路由表决定着从本主机向其他主机,其他网络发送数据的去向,是排除网络故障的关键信息

[root@hua ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr
'Destination列对应目标网段的地址'
'Gateway列对应下一跳路由器地址'
'Genmask列对应子网掩码'
'Iface列对应发送数据的网络接口'
'目标网段为default时,表示此行时默认网关记录'

[root@localhost ~]# route -n       ##以数字显示相关的主机地址、端口
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         20.0.0.2        0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

1.5查看网络连接情况netstat

mark

-a:显示当前主机中所有的活动的网络连接信息(包括监听、非监听

状态的服务端口)

-n:以数字形式显示相关的主机地址、端口等信息。

-p:显示与网络连接相关的进程号、进程名称

-t:查看TCP协议相关的信息

-u:显示UDP协议连接

-r:查看路由表
查看本机是否监听“TCP  80”端口
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# netstat -ntap |grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      15132/httpd
显示UDP协议
[root@localhost ~]# netstat -nuap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           8624/avahi-daemon:  
udp        0      0 0.0.0.0:57287           0.0.0.0:*                           8624/avahi-daemon:  
udp        0      0 192.168.122.1:53        0.0.0.0:*                           9718/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           9718/dnsmasq        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1/systemd           
udp        0      0 127.0.0.1:323           0.0.0.0:*                           8695/chronyd        
udp        0      0 0.0.0.0:735             0.0.0.0:*                           8621/rpcbind        
udp6       0      0 :::111                  :::*                                1/systemd           
udp6       0      0 ::1:323                 :::*                                8695/chronyd        
udp6       0      0 :::735                  :::*                                8621/rpcbind        

1.6 socket(ss)命令

查看系统的网络连接情况,获取socket统计信息

第一时间截取你的连接信息,访问TCP_diag模块,立马访问ssh访问信息连接分析模块

高并发连接场景不建议用netstat ,可以用ss

ss【选项】

常用选项

-a:显示当前主机中所有的活动的网络连接信息(包括监听、非监听

状态的服务端口)

-n:取消服务名称,改为端口

-p:pid进程号

-t:查看TCP协议相关的信息

-u:显示UDP协议连接

-r:查看路由表

-l:查看监听状态下的信息

实列:

[root@hua ~]# ss -nt      ##n取消服务名称,改为端口
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
ESTAB       0      36     20.0.0.41:22                 20.0.0.3:65146              
[root@hua ~]# ss -t    ##查看已连接状态信息
State       Recv-Q Send-Q Local Address:Port                 Peer Address:Port                
ESTAB       0      36     20.0.0.41:ssh                  20.0.0.3:65146   

其他命令跟netstat大同小异            

二:测试网络连接ping

2.1ping 【选项】目标主机

实例
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (180.101.49.11) 56(84) bytes of data.
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=1 ttl=128 time=6.04 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=2 ttl=128 time=5.89 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=3 ttl=128 time=5.33 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=4 ttl=128 time=6.20 ms
64 bytes from 180.101.49.11 (180.101.49.11): icmp_seq=5 ttl=128 time=6.04 ms
^C
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4008ms
rtt min/avg/max/mdev = 5.334/5.907/6.209/0.306 ms
按Ctrl C 组合键终止ping测试'

2.2使用traceroute命令跟踪数据包

  • 测试从当前主机到目的主机之间经过的网络节点
  • traceroute 目标主机地址
[root@shuai ~]# traceroute www.baidu.com
traceroute to www.baidu.com (112.80.248.76), 30 hops max, 60 byte packets
 1  gateway (20.0.0.2)  0.144 ms  0.078 ms  0.120 ms   ##首先寻找DNS网关
 2  * * *
 3  * * *
 4  * * *
 5  * * *           ##这里不在同一局域网没有找到 做了NAT地址转换 以*显示
 6  * * *             
 7  * * *       
 8  * * *
 9  * * *
10  * * *
11  * * *
12  * * *
13  * * *
14  * * *
15  * * *
16  * * *
17  * * *
18  * * *
19  * * *
20  * * *
21  * * *
22  * * *
23  *^C

2.3 nslookup命令测试DNS域名解析

nslookup命令用于查询DNS的记录,查看域名解析是否正常,在网络故障的时候用来诊断网络问题

nslookup 目标主机地址 【DNS服务地址】
23  *^C
[root@shuai ~]# nslookup www.baidu.com
Server:		20.0.0.2          ##DNS服务器
Address:	20.0.0.2#53

注意这里的是DNS端口有两个
一个是TCP:用于安全链接
一个是UDP:解析

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
Name:	www.a.shifen.com
Address: 112.80.248.76
Name:	www.a.shifen.com
Address: 112.80.248.75
[root@shuai ~]# ping 112.80.248.75
PING 112.80.248.75 (112.80.248.75) 56(84) bytes of data.
64 bytes from 112.80.248.75: icmp_seq=1 ttl=128 time=13.8 ms

64 bytes from 112.80.248.75: icmp_seq=2 ttl=128 time=15.0 ms
64 bytes from 112.80.248.75: icmp_seq=3 ttl=128 time=22.2 ms
64 bytes from 112.80.248.75: icmp_seq=4 ttl=128 time=15.1 ms
64 bytes from 112.80.248.75: icmp_seq=5 ttl=128 time=17.4 ms
64 bytes from 112.80.248.75: icmp_seq=6 ttl=128 time=17.9 ms
64 bytes from 112.80.248.75: icmp_seq=7 ttl=128 time=16.8 ms
64 bytes from 112.80.248.75: icmp_seq=8 ttl=128 time=13.9 ms
64 bytes from 112.80.248.75: icmp_seq=9 ttl=128 time=15.3 ms
64 bytes from 112.80.248.75: icmp_seq=10 ttl=128 time=20.4 m
64 bytes from 112.80.248.75: icmp_seq=11 ttl=128 time=15.6 m
64 bytes from 112.80.248.75: icmp_seq=12 ttl=128 time=12.1 m
64 bytes from 112.80.248.75: icmp_seq=13 ttl=128 time=18.1 m
64 bytes from 112.80.248.75: icmp_seq=14 ttl=128 time=16.2 m
64 bytes from 112.80.248.75: icmp_seq=15 ttl=128 time=10.7 m
64 bytes from 112.80.248.75: icmp_seq=16 ttl=128 time=40.0 m
64 bytes from 112.80.248.75: icmp_seq=17 ttl=128 time=11.7 m
64 bytes from 112.80.248.75: icmp_seq=18 ttl=128 time=15.7 ms
64 bytes from 112.80.248.75: icmp_seq=19 ttl=128 time=16.6 ms
^C
--- 112.80.248.75 ping statistics ---
19 packets transmitted, 19 received, 0% packet loss, time 18179ms
rtt min/avg/max/mdev = 10.761/17.138/40.062/6.052 ms
[root@shuai ~]# dig www.baidu.com   ##也是解析域名相对nslooup更详细

; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53123
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; MBZ: 0005 , udp: 4096
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		5	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	5	IN	A	112.80.248.75
www.a.shifen.com.	5	IN	A	112.80.248.76

;; Query time: 3 msec
;; SERVER: 20.0.0.2#53(20.0.0.2)
;; WHEN: 四 7月 02 23:27:39 CST 2020
;; MSG SIZE  rcvd: 101

三:使用网络配置命令

3.1设置网络参数的方式

3.11 临时配置一使用命令调整网络参数
●简单、快速,可直接修改运行中的网络参数
●一般只适合在调试网络的过程中使用
●系统重启以后,所做的修改将会失效
3.12 固定设置.一通过配置文件修改网络参数
●修改各项网络参数的配置文件
●适合对服务器设置固定参数时使用
●需要重载网络服务或者重启以后才会生效

3.2 设置网络接口参数ifconfig

设置网络接口的IP地址、子网掩码

ifconfig 网络接口 IP地址 【netmask】子网掩码

ifconfig 网络接口 IP地址【/子网掩码长度】
##这边的修改网卡是用于临时修改
[root@shuai ~]# ifconfig ens33 20.0.0.13 netmask 255.255.255.0、

禁用或者重新激活网卡

ifconfig 网络接口 up

ifconfig 网络接口 down

列如:
[root@shuai ~]# ifconfig down    ##临时禁用ens33
[root@shuai ~]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1301  bytes 109664 (107.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1301  bytes 109664 (107.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:95:60:04  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@shuai ~]# ifconfig ens33 up   ##重新激活ens33
[root@shuai ~]# ifconfig   
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.25  netmask 255.255.255.0  broadcast 20.0.0.255
        inet6 fe80::20c:29ff:fe84:c1e3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:84:c1:e3  txqueuelen 1000  (Ethernet)
        RX packets 50328  bytes 23361374 (22.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8024  bytes 1224907 (1.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1301  bytes 109664 (107.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1301  bytes 109664 (107.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:95:60:04  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 

3.3 设置虚拟网络接口

再对服务器网络进行调试的过程中,有时候需要在同一个网卡上使用一个新的IP地址,但是又不能覆盖原本的IP地址而导致服务程序不可用,此时可以为网卡绑定一个虚拟化的接口,然后在位于虚拟机接口配置新的IP地址

[root@shuai ~]# ifconfig ens33:0 20.0.0.44
[root@shuai ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.25  netmask 255.255.255.0  broadcast 20.0.0.255
        inet6 fe80::20c:29ff:fe84:c1e3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:84:c1:e3  txqueuelen 1000  (Ethernet)
        RX packets 50483  bytes 23381169 (22.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8183  bytes 1245419 (1.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.44  netmask 255.0.0.0  broadcast 20.255.255.255
        ether 00:0c:29:84:c1:e3  txqueuelen 1000  (Ethernet)

3.31 使用route命令设置路由记录

使用route命令不仅可以用于查看路由表信息,还可以用来添加,删除静态的路由表条目,其中也包括设置默认网关地址(默认网关记录是一条特殊的静态路由条目)

默认万贯的IP 地址应该与本机其中之一接口的IP地址在同一个网段

  • 添加到指定的路由表信息
[root@shuai ~]# route         ##原本的路由条目
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
20.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@shuai ~]# route add -net 20.0.30.0/24 gw 20.0.0.40      ##添加静态理由 本机访问另一个网段20.0.0.30/24数据都发给20.0.0.40
[root@shuai ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         20.0.0.2        0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
20.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens33
20.0.30.0       20.0.0.40       255.255.255.0   UG    0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

删除到指定网段的路由记录

    x25 (CCITT X.25) 
[root@shuai ~]# route del -net 20.0.30.0/24       ##删除20.0.0.30/24静态路由
[root@shuai ~]# route -n                          
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         20.0.0.2        0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
20.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

向路由表的中默认网关记录

[root@shuai ~]# route add default gw 192.168.122.10吃  ##添加到192.168.122.10默认网关记录
[root@shuai ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 virbr0
default         gateway         0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
20.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

删除路由表中的默认网关记录

       inet_route [-FC] flush      NOT supported
[root@shuai ~]# route del default gw 192.168.122.10
[root@shuai ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
20.0.0.0        0.0.0.0         255.255.255.0   U     100    0        0 ens33
20.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

同一个主机的路由表只应有一条默认网关记录,否则会导致主机的网络了解出现故障

四:本地主机映射 /etc/hosts

  • 保存主机名与IP地址的映射文件

  • hosts文件和DNS服务器的比较

  • 默认情况下,系统首先从hosts文件查找解析记录

  • hosts文件只对当前的主机有效

  • hosts文件可减少DNS查询过程,从而加快访问速度

域名解析会优先去查看/etc/hosts 映射文件,若是没有,就会再去找dns服务器

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
6.6.6.6 www.baidu.com            

0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0


同一个主机的路由表只应有一条默认网关记录,否则会导致主机的网络了解出现故障



## 四:本地主机映射 /etc/hosts

- 保存主机名与IP地址的映射文件

- hosts文件和DNS服务器的比较

- 默认情况下,系统首先从hosts文件查找解析记录

- hosts文件只对当前的主机有效
- hosts文件可减少DNS查询过程,从而加快访问速度

域名解析会优先去查看/etc/hosts 映射文件,若是没有,就会再去找dns服务器

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
6.6.6.6 www.baidu.com


本文地址:https://blog.csdn.net/weixin_47151643/article/details/107097852

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网