Linux下的网络配置

一.主机IP的查看
1.ifconfig查看主机ip

[kiosk@foundation4 ~]$ ifconfig 
br0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.25.4.250  netmask 255.255.255.0  broadcast 172.25.4.255
        inet6 fe80::bcac:c4ff:feb5:ef01  prefixlen 64  scopeid 0x20<link>
        ether b8:2a:72:ba:72:06  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 10  bytes 1402 (1.3 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 1  (Local Loopback)
        RX packets 21  bytes 1756 (1.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21  bytes 1756 (1.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

p1p1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether b8:2a:72:ba:72:06  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

privbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.25.250.250  netmask 255.255.255.0  broadcast 172.25.250.255
        ether 52:54:00:1a:a2:6c  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: 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:40:d5:8e  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

virbr1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:14:de:92  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=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:40:d5:8e  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

wlp6s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.102  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::143f:4446:54c2:7637  prefixlen 64  scopeid 0x20<link>
        ether 9c:ad:97:15:8e:37  txqueuelen 1000  (Ethernet)
        RX packets 5352  bytes 4263231 (4.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4194  bytes 700944 (684.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[kiosk@foundation4 ~]$ file:///usr/share/doc/HTML/en-US/index.htmlfile:///usr/share/doc/HTML/en-US/index.html

2.ip addr show查看主机ip

[kiosk@foundation4 ~]$ ip addr show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: p1p1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast master br0 state DOWN qlen 1000
    link/ether b8:2a:72:ba:72:06 brd ff:ff:ff:ff:ff:ff
3: wlp6s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 9c:ad:97:15:8e:37 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.102/24 brd 192.168.1.255 scope global dynamic wlp6s0
       valid_lft 6512sec preferred_lft 6512sec
    inet6 fe80::143f:4446:54c2:7637/64 scope link 
       valid_lft forever preferred_lft forever
4: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether b8:2a:72:ba:72:06 brd ff:ff:ff:ff:ff:ff
    inet 172.25.4.250/24 brd 172.25.4.255 scope global br0
       valid_lft forever preferred_lft forever
    inet 172.25.254.4/24 brd 172.25.254.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::bcac:c4ff:feb5:ef01/64 scope link 
       valid_lft forever preferred_lft forever
5: privbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:1a:a2:6c brd ff:ff:ff:ff:ff:ff
    inet 172.25.250.250/24 brd 172.25.250.255 scope global privbr0
       valid_lft forever preferred_lft forever
6: privbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master privbr0 state DOWN qlen 1000
    link/ether 52:54:00:1a:a2:6c brd ff:ff:ff:ff:ff:ff
7: virbr1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:14:de:92 brd ff:ff:ff:ff:ff:ff
8: virbr1-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr1 state DOWN qlen 1000
    link/ether 52:54:00:14:de:92 brd ff:ff:ff:ff:ff:ff
9: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:40:d5:8e brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
10: virbr0-nic: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:40:d5:8e brd ff:ff:ff:ff:ff:ff
[kiosk@foundation4 ~]$ 

二.主机IP的设定
1.临时ip的设定
(1)ip设定

[root@localhost Desktop]# ifconfig eth0 172.25.254.108
[root@localhost Desktop]# ping 172.25.254.108
PING 172.25.254.108 (172.25.254.108) 56(84) bytes of data.
64 bytes from 172.25.254.108: icmp_seq=1 ttl=64 time=0.036 ms
64 bytes from 172.25.254.108: icmp_seq=2 ttl=64 time=0.034 ms
64 bytes from 172.25.254.108: icmp_seq=3 ttl=64 time=0.033 ms
^C
--- 172.25.254.108 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.033/0.034/0.036/0.005 ms

(2)主机ip的删除

[root@localhost Desktop]# ip addr del dev eth0 172.25.254.108/16
[root@localhost Desktop]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:00:04:0a brd ff:ff:ff:ff:ff:ff
    inet6 fe80::5054:ff:fe00:40a/64 scope link 
       valid_lft forever preferred_lft forever

(3)添加主机ip

[root@localhost Desktop]# ip addr add dev eth0 172.25.254.108/16
[root@localhost Desktop]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:00:04:0a brd ff:ff:ff:ff:ff:ff
    inet 172.25.254.108/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe00:40a/64 scope link 
       valid_lft forever preferred_lft forever

2.有图形界面设置主机ip
3.无图形界面设置主机ip
命令:nnmtui
(1)设置界面Edit a connection
在这里插入图片描述
(2)添加ip
在这里插入图片描述
(3)在这里插入图片描述(4)添加名称及ip
在这里插入图片描述
(5)退出Quit
在这里插入图片描述

设置完成重启网络

[root@localhost Desktop]# systemctl restart network[root@localhost Desktop]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.204  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fe00:40a  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:04:0a  txqueuelen 1000  (Ethernet)
        RX packets 1427  bytes 63256 (61.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 807  bytes 47145 (46.0 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 0  (Local Loopback)
        RX packets 458  bytes 41100 (40.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 458  bytes 41100 (40.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4.静态网络的配置
在这里插入图片描述
在这里插入图片描述
设置静态网络
在这里插入图片描述

[root@localhost Desktop]# systemctl restart network
[root@localhost Desktop]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:00:04:0a brd ff:ff:ff:ff:ff:ff
    inet 172.25.254.204/24 brd 172.25.254.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe00:40a/64 scope link 
       valid_lft forever preferred_lft foreve

5.动态网络的配置
在这里插入图片描述
三.不同网段的ip通过网关进行访问
1.2台虚拟机不同网段ip进行访问
(1)单网卡设置网关并实验

[root@localhost Desktop]# ping 172.25.254.104
connect: Network is unreachable
[root@localhost Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
[root@localhost Desktop]# vim /etc/sysconfig/network
[root@localhost Desktop]# systemctl restart network
[root@localhost Desktop]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         1.1.1.205       0.0.0.0         UG    1024   0        0 eth0
1.1.1.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
[root@localhost Desktop]# ping 172.25.254.205
PING 172.25.254.205 (172.25.254.205) 56(84) bytes of data.
64 bytes from 172.25.254.205: icmp_seq=1 ttl=64 time=0.499 ms
64 bytes from 172.25.254.205: icmp_seq=2 ttl=64 time=0.328 ms
^C
--- 172.25.254.205 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.328/0.413/0.499/0.087 ms

(2)双网卡设置路由
a.查看网卡及设置ip
在这里插入图片描述
b.开启防火墙

[root@lyq Desktop]# systemctl start firewalld.service    ##开启防火墙服务 
[root@lyq Desktop]# firewall-cmd --list-all    ##查看服务信息
public (default, active)
  interfaces: eth0 eth1
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no      ##功能未开启
  forward-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@lyq Desktop]# firewall-cmd --permanent --add-masquerade  ##开启伪装功能
success
[root@lyq Desktop]# systemctl restart firewalld.service ## 重启防火墙服务
[root@lyq Desktop]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0 eth1
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade:yes    ##伪装功能成功开启
  forward-ports: 
  icmp-blocks: 
  rich rules: 

c.开启系统路由功能

[root@lyq Desktop]# sysctl -a |grep ip_forward  ##从所有系统参数中筛选ip_forward 参数
net.ipv4.ip_forward = 0                         ##该值为0,表示系统路由功能未开启
[root@lyq Desktop]# vim /etc/sysctl.conf        ##修改系统文件 写入 net.ipv4.ip_forward = 1
[root@lyq Desktop]# cat /etc/sysctl.conf        ## 查看该文件内容
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward = 1
[root@lyq Desktop]# sysctl -p   ##从指定的文件加载系统参数,如不指定从/etc/sysctl.conf中加载
net.ipv4.ip_forward = 1         ##系统路由功能开启

四.命令方式设定网络
nmcli ##NetworkManager必须开启
nmcli device connect eth0 ##启用eth0网卡
device disconnect eth0 ##关闭eth0网卡
device show eth0 ##查看网关信息
device status eth0 ##查看网卡服务接口的信息
connection show ##显示连接网卡
connection down westos ##断开连接
(1) nmcli device disconnect eth0 ##关闭eth0网卡

[root@localhost Desktop]# nmcli device disconnect eth0
[root@localhost Desktop]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       ether 52:54:00:d3:e2:6b  txqueuelen 1000  (Ethernet)
       RX packets 89190  bytes 5925145 (5.6 MiB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 3962  bytes 255295 (249.3 KiB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       ether 52:54:00:cc:31:7a  txqueuelen 1000  (Ethernet)
       RX packets 89293  bytes 5810795 (5.5 MiB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 383  bytes 31855 (31.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 0  (Local Loopback)
       RX packets 2522  bytes 222550 (217.3 KiB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 2522  bytes 222550 (217.3 KiB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(2)nmcli device connect eth0 ##启用eth0网卡

[root@localhost Desktop]# nmcli device connect eth0  ##启用eth0网卡
Device 'eth0' successfully activated with 'f53e5391-4a38-4472-aab3-266dafe35dbd'.
[root@localhost Desktop]# ifconfig   ##查看ip
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.96  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fed3:e26b  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:d3:e2:6b  txqueuelen 1000  (Ethernet)
        RX packets 89580  bytes 5951842 (5.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4020  bytes 261971 (255.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:cc:31:7a  txqueuelen 1000  (Ethernet)
        RX packets 89708  bytes 5840804 (5.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 383  bytes 31855 (31.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 0  (Local Loopback)
        RX packets 2535  bytes 223834 (218.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2535  bytes 223834 (218.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(3)nmcli device show ##查看网卡信息

[root@localhost Desktop]# nmcli device show  ##查看网卡信息
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         52:54:00:D3:E2:6B
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     hahaha
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/14
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         ip = 172.25.254.96/24, gw = 172.25.254.250
IP4.ADDRESS[2]:                         ip = 172.25.254.205/32, gw = 172.25.254.250
IP4.ROUTE[1]:                           dst = 172.25.0.250/32, nh = 172.25.254.250, mt = 0
IP4.DNS[1]:                             172.25.254.250
IP4.DOMAIN[1]:                          example.com
IP6.ADDRESS[1]:                         ip = fe80::5054:ff:fed3:e26b/64, gw = ::

GENERAL.DEVICE:                         eth1
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         52:54:00:CC:31:7A
GENERAL.MTU:                            1500
GENERAL.STATE:                          30 (disconnected)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
WIRED-PROPERTIES.CARRIER:               on

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         ip = 127.0.0.1/8, gw = 0.0.0.0
IP6.ADDRESS[1]:                         ip = ::1/128, gw = ::

(4) nmcli device status ##查看网卡服务接口信息

[root@localhost Desktop]# nmcli device status ##查看网卡服务接口信息
DEVICE  TYPE      STATE         CONNECTION 
eth0    ethernet  connected     hahaha     
eth1    ethernet  disconnected  --            

五.DNS域名的解析
1.概念
dns是一台服务器,可以提供回答客户主机和ip对应关系的功能
2.dns的设定 (以www.baidu.com为例)
(1)将真机作为路由器,使得虚拟机可以通过真机连接网络

[root@client Desktop]# ping 183.232.231.174
PING 183.232.231.174 (183.232.231.174) 56(84) bytes of data.
64 bytes from 183.232.231.174: icmp_seq=1 ttl=55 time=42.0 ms
64 bytes from 183.232.231.174: icmp_seq=2 ttl=55 time=42.3 ms
64 bytes from 183.232.231.174: icmp_seq=3 ttl=55 time=41.0 ms
64 bytes from 183.232.231.174: icmp_seq=4 ttl=55 time=41.1 ms
64 bytes from 183.232.231.174: icmp_seq=5 ttl=55 time=40.8 ms
64 bytes from 183.232.231.174: icmp_seq=6 ttl=55 time=41.6 ms
64 bytes from 183.232.231.174: icmp_seq=7 ttl=55 time=40.8 ms
64 bytes from 183.232.231.174: icmp_seq=8 ttl=55 time=41.1 ms
64 bytes from 183.232.231.174: icmp_seq=9 ttl=55 time=41.9 ms
^C
--- 183.232.231.174 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8009ms
rtt min/avg/max/mdev = 40.853/41.455/42.386/0.581 ms

(2)本地解析文件 /etc/hosts. 提供域名ip地址解析

[root@client Desktop]# ping www.baidu.com #通过域名进行里连接
ping: unknown host www.baidu.com          ##无法连接
[root@client Desktop]# vim /etc/hosts
[root@client Desktop]# cat  /etc/hosts     ##本地解析文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.254.254 classroom.example.com
172.25.254.254 content.example.com
172.25.231.208  
183.232.231.174 www.baidu.com             ##添加www.baidu.com 的域名解析
[root@client Desktop]# ping www.baidu.com  ##使用域名成功连接
PING www.baidu.com (183.232.231.174) 56(84) bytes of data.
64 bytes from www.baidu.com (183.232.231.174): icmp_seq=1 ttl=55 time=91.3 ms
64 bytes from www.baidu.com (183.232.231.174): icmp_seq=2 ttl=55 time=67.3 ms
64 bytes from www.baidu.com (183.232.231.174): icmp_seq=3 ttl=55 time=42.7 ms
64 bytes from www.baidu.com (183.232.231.174): icmp_seq=4 ttl=55 time=41.6 ms
64 bytes from www.baidu.com (183.232.231.174): icmp_seq=5 ttl=55 time=42.3 ms
64 bytes from www.baidu.com (183.232.231.174): icmp_seq=6 ttl=55 time=42.1 ms
^C
--- www.baidu.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5007ms
rtt min/avg/max/mdev = 41.683/54.612/91.382/18.821 ms

(3)使用DNS服务器进行域名解析。/etc/resolv.conf 存放dns服务器的地址

[root@client Desktop]# vim /etc/resolv.conf    ##编辑文件,添加dns服务器的地址
[root@client Desktop]# cat  /etc/resolv.conf 
 # Generated by NetworkManager
domain ilt.example.com
search ilt.example.com example.com localdomain.localdomain
nameserver 114.114.114.114                ##新添加的dns服务器地址
[root@client Desktop]# ping baidu.com     ##使用域名进行网站通讯
PING baidu.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216: icmp_seq=1 ttl=50 time=32.0 ms
64 bytes from 220.181.57.216: icmp_seq=2 ttl=50 time=30.7 ms
64 bytes from 220.181.57.216: icmp_seq=3 ttl=50 time=31.5 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3004ms
rtt min/avg/max/mdev = 30.757/31.456/32.077/0.560 ms
[root@client Desktop]# ping taobao.com    ##**加粗样式**使用域名进行网站通讯
PING taobao.com (140.205.94.189) 56(84) bytes of data.
64 bytes from 140.205.94.189: icmp_seq=1 ttl=38 time=49.1 ms
64 bytes from 140.205.94.189: icmp_seq=2 ttl=38 time=45.9 ms
64 bytes from 140.205.94.189: icmp_seq=3 ttl=38 time=49.5 ms
^C
--- taobao.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 45.916/48.183/49.509/1.630 ms

3.设定解析的优先级

 [root@localhost Desktop]# vim /etc/hosts  ##添加百度的真实网址及链接
[root@localhost Desktop]# cat /etc/resolv.conf  ##查看是否有DNS服务器      
# Generated by NetworkManager  
search ilt.example.com

# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com
[root@localhost Desktop]# vim /etc/resolv.conf  ##添加DNS服务器
[root@localhost Desktop]# cat /etc/resolv.conf  
# Generated by NetworkManager
search ilt.example.com

nameserver 114.114.114.114     ##添加成功
# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com
[root@localhost Desktop]# vim /etc/hosts   ##重新设置百度网址及链接
[root@localhost Desktop]# ping www.baidu.com
PING www.baidu.com (172.25.5.105) 56(84) bytes of data.
64 bytes from www.baidu.com (172.25.5.105): icmp_seq=1 ttl=64 time=0.051 ms  ##以/etc/hosts为主
64 bytes from www.baidu.com (172.25.5.105): icmp_seq=2 ttl=64 time=0.056 ms
^C
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.051/0.053/0.056/0.007 ms
[root@localhost Desktop]# vim /etc/nsswitch.conf   ##更改优先级  files 与 dns位置更换 在39行
[root@localhost Desktop]# ping www.baidu.com
PING www.a.shifen.com (111.13.100.92) 56(84) bytes of data.       ##验证此时以dns中百度的ip优先
64 bytes from 111.13.100.92: icmp_seq=1 ttl=52 time=74.5 ms
64 bytes from 111.13.100.92: icmp_seq=2 ttl=52 time=121 ms
64 bytes from 111.13.100.92: icmp_seq=3 ttl=52 time=107 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2009ms
rtt min/avg/max/mdev = 74.500/101.126/121.253/19.634 ms
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值