Linux DNS域名解析、 nmcli命令

本文介绍了Linux系统中DNS域名解析的基本概念,包括如何设置本地解析文件和DNS导向,探讨了本地解析与DNS导向的优先级。同时,详细讲解了nmcli命令的使用,如设备连接、断开、状态查询以及创建网络连接等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

域名解析
一、概念
dns是一台服务器
这台服务器提供了回答客户主机和ip对应关系的功能。
二、设定dns
1、本地解析文件(默认本地优先集高)
/etc/hosts
(1)先本地ping百度查看百度ip(183.232.231.172)
(2)进入文件,将百度ip写进文件

[root@localhost network-scripts]# vim /etc/hosts

在这里插入图片描述

(3)将ip加入文件后可直接访问域名二不用访问ip
ping www.baidu.com

[root@localhost network-scripts]# ping www.baidu.com
PING www.baidu.com (183.232.231.173) 56(84) bytes of data.
64 bytes from www.baidu.com (183.232.231.173): icmp_seq=1 ttl=51 time=204 ms
64 bytes from www.baidu.com (183.232.231.173): icmp_seq=2 ttl=51 time=51.5 ms
^C
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 51.546/127.907/204.268/76.361 ms
[root@localhost network-scripts]# 

2、DNS导向
真机ping淘宝(223.99.232.254)
进入文件**/etc/resolv.conf** (提供域名导向)
添加:nameserver 114.114.114.114
实验:

[root@localhost network-scripts]# ping www.taobao.com
PING www.taobao.com.danuoyi.tbcache.com (223.99.232.253) 56(84) bytes of data.
64 bytes from 223.99.232.253: icmp_seq=1 ttl=37 time=100 ms
64 bytes from 223.99.232.253: icmp_seq=2 ttl=37 time=60.9 ms
^C
--- www.taobao.com.danuoyi.tbcache.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 60.968/80.810/100.652/19.842 ms
[root@node2 network-scripts]# 

(3)查看本地解析及DNS导向的优先级(默认本地优先集高)
保存优先级的文件:/etc/nsswitch.conf
进入文件:39行 39 hosts: files dns (本地优先级高于DNS导向)

实验:以淘宝网为例子
1)将淘宝ip加入etc/hosts
223.99.232.253 www.taobao.com

   [root@localhost network-scripts]# ping www.taobao.com
PING www.taobao.com (223.99.232.253) 56(84) bytes of data. 
64 bytes from www.taobao.com (223.99.232.253): icmp_seq=1 ttl=37 time=231 ms
64 bytes from www.taobao.com (223.99.232.253): icmp_seq=2 ttl=37 time=40.7 ms
^C
--- www.taobao.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 40.767/136.150/231.534/95.384 ms
[root@node2 network-scripts]# 

显示的ip为223.99.232.253
2)改变/etc/nsswitch.conf文件中本地解析及DNS导向的优先级
39 hosts: dns files

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ping www.taobao.com
PING www.taobao.com.danuoyi.tbcache.com (223.99.232.254) 56(84) bytes of data.
64 bytes from 223.99.232.254: icmp_seq=1 ttl=37 time=79.3 ms
64 bytes from 223.99.232.254: icmp_seq=2 ttl=37 time=68.1 ms
64 bytes from 223.99.232.254: icmp_seq=3 ttl=37 time=66.9 ms
^C
--- www.taobao.com.danuoyi.tbcache.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 66.969/71.496/79.325/5.562 ms
[root@node2 network-scripts]# 
 显示的ip为223.99.232.254为DNS自动分配

、上面DNS的设置均在静态(eth0中 BOOTPROTO=none)
当BOOTPROTO=dhcp时DNS也会随机分配,现设置固定的DNS
当eth0中BOOTPROTO=dhcp时,DNS也会随机分配,即使/etc/resolv.conf 中设置了DNS也无效。

实验:
(1)将或取ip的方式改为自动或取

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
#IPADDR=1.1.1.200
NETMASK=255.255.255.0
#GATEWAY=1.1.1.100
#PEERDNS=no

(2)给/etc/resolv.conf 中设置了DNS(114.114.114.114)

#Generated by NetworkManager
domain example.com
search example.com example2.com
nameserver 172.25.254.254
nameserver 114.114.114.114

(3)重启后

[root@localhost network-scripts]# vim /etc/resolv.conf 
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# cat /etc/resolv.conf 
#Generated by NetworkManager
domain example.com
search example.com example2.com
nameserver 172.25.254.254  ##只剩下了系统自动分配的

现设置固定的DNS
需先给eth0中添加 PEERDNS=no
PEERDNS=yes 是自动获取dns
PEERDNS=no 关闭自动获取dns

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
#IPADDR=1.1.1.200
NETMASK=255.255.255.0
#GATEWAY=1.1.1.100
PEERDNS=no

重启服务:

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# vim /etc/resolv.conf 
#Generated by NetworkManager
search example2.com
nameserver 114.114.114.114
nameserver 172.25.254.254
#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 network-scripts]# cat /etc/resolv.conf 
#Generated by NetworkManager
search example2.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

可以看出关闭自动获取dns后,DNS导向文件里只有写进入的114.114.114.114,而不会自动分配dns

nmcli命令

命令nmcli需在NetworkManager服务开启的状体下执行

在这里插入图片描述

(1)nmcli device show 显示所有设备
在这里插入图片描述
(2)nmcli device connect eth0连接eth0
在这里插入图片描述

(3)nmcli device disconnect eth0 关闭eth0网卡
在这里插入图片描述

(4)nmcli device show eth0 查看网卡信息

(5)nmcli device status eth0 查看网卡服务借口信息
在这里插入图片描述

创建网卡
在这里插入图片描述
nmcli connection show 显示连接的网卡
在这里插入图片描述

nmcli connection down westos 断开连接
在这里插入图片描述

nmcli connection add 创建网卡

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值