ubuntu 22.04版本修改服务器名、ip,dns信息的利用方法

打印 上一主题 下一主题

主题 495|帖子 495|积分 1485

总结
1、ubuntu修改服务器名重启后生效的方法是直接修改/etc/hostname文件
2、ubuntu 22.04利用体系设置ip和dns信息,一般只必要使用netplan命令行工具来设置就行,在/etc/netplan/在目录下创建一个yaml文件就可以实现ip和dns的设置,固然如果/etc/netplan下有多个yaml文件,则全部/etc/netplan/*.yaml文件都将被netplan命令行使用,拜见官方文档https://ubuntu.com/server/docs/network-configuration和https://manpages.ubuntu.com/manpages/jammy/man5/netplan.5.html
3、个人不建议使用/etc/resolve.conf来设置ubuntu 22.04利用体系的dns信息,由于太复杂了,这个文件是被systemd-resolved服务托管。ubuntu利用体系/etc/resolve.conf中默认使用的是 nameserver 127.0.0.53回环地点,/etc/resolve.conf文中有这么一句话This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8). Do not edit。说明这个文件是被systemd-resolved这个服务托管的。通过 netstat -tnpl| grep systemd-resolved 查察到这个服务是监听在 53 号端口上。为啥要用127.0.0.53作为回环地点而不是127.0.0.1,由于127网段都是回环地点(127.0.0.1 - 127.255.255.254),不过通常用大家只喜欢用127.0.0.1来表示而已,53表示dns端口
3.1、新安装的ubuntu 22.04情况不做其他改动的情况下,/etc/resolv.conf是/run/systemd/resolve/stub-resolv.conf的软链接,如果只是手工更改/etc/resolv.conf中的内容,重启会后发现/etc/resolv.conf中的内容又恢复成原样了,比如表明/etc/resolv.conf文件中的nameserver 127.0.0.53这一行,重启体系后被表明掉的nameserver 127.0.0.53这一行又返来了,如果不想重启后/etc/resolv.conf中的内容恢复成原样,则执行systemctl stop systemd-resolved和systemctl disable systemd-resolved,或把/etc/resolv.conf对应的软链接删除,再手工建立一个/etc/resolv.conf
3.2、使用/etc/netplan/00-installer-config.yaml设置DNS 172.22.10.66并执行netplan apply使之生效,再修改/etc/systemd/resolved.conf内容里的DNS为172.22.136.2,然后执行systemctl restart systemd-resolved,发现/etc/resolv.conf中的内容没变还是127.0.0.53,再执行resolvectl status可以看到/etc/systemd/resolved.conf中的DNS 172.22.136.2存在,/etc/netplan/00-installer-config.yaml中的DNS 172.22.10.66也在,就是没有/etc/resolv.conf中的127.0.0.53。重启利用体系后/etc/resolv.conf中的内容没变还是127.0.0.53,重启利用体系后执行resolvectl status还是只有/etc/systemd/resolved.conf中的DNS 172.22.136.2和/etc/netplan/00-installer-config.yaml中的DNS 172.22.10.66,没有/etc/resolv.conf中的127.0.0.53
4、ubuntu官方不建议使用/etc/resolve.conf来设置ubuntu 的dns信息,ubuntu对于/etc/resolv.conf的官方描述:If you require DNS for your temporary network configuration, you can add DNS server IP addresses in the file /etc/resolv.conf. In general, editing /etc/resolv.conf directly is not recommended, but this is a temporary and non-persistent configuration.
5、ubuntu 22.04查察dns信息的命令是resolvectl status,该命令可以看到全局dns信息和某个网卡的dns信息,全局DNS服务器信息一般来自设置文件/etc/systemd/resolved.conf,仅在/etc/resolv.conf不是一个指向/run/systemd/resolve/stub-resolv.conf, /usr/lib/systemd/resolv.conf, /run/systemd/resolve/resolv.conf 之一的软连接的情况下,且/etc/systemd/resolved.conf中dns被表明掉的情况下,全局DNS服务器才会读取自/etc/resolv.conf,以是这就是不建议大家使用/etc/resolve.conf来设置ubuntu 的dns信息的原因。
修改服务器名
修改服务器名为FRSBachDEV3,重启后也生效的方法
vi /etc/hostname
FRSBachDEV3
备注:如果/etc/hostname文件中的内容写成hostname FRSBachDEV3,那么重启后服务器名就酿成了hostnameFRSBachDEV3
修改IP
https://ubuntu.com/server/docs/network-configuration
https://manpages.ubuntu.com/manpages/jammy/man5/netplan.5.html
netplan是一个命令行工具,用于ubuntu上设置网络,全部/etc/netplan/*.yaml文件都将被使用
  1. root@FRSBachDEV3:~# cat /etc/netplan/00-installer-config.yaml
  2. network:
  3.   ethernets:
  4.     ens160:
  5.       dhcp4: true
  6.   version: 2
复制代码
  1. root@FRSBachDEV3:~# vim /etc/netplan/00-installer-config.yaml
  2. network:
  3.   ethernets:
  4.     ens160:
  5.       dhcp4: false
  6.       addresses: [172.22.136.147/22]
  7.       routes:
  8.         - to: default
  9.           via: 172.22.136.1
  10.       nameservers:
  11.         search: [dai.netdai.com,netdai.com]
  12.         addresses: [172.22.10.66,172.22.10.67]
  13.   version: 2
复制代码
备注:
dhcp4中的4表示ipv4
gateway4中的4表示ipv4,不过gateway4已经被废弃,设置了gateway4再执行netplan apply的话会报错** (generate:1426): WARNING **: 09:54:13.918: gateway4 has been deprecated, use default routes instead.See the ‘Default routes’ section of the documentation for more details.
routes项填写网关地点
addresses项填写ip地点
nameservers项填写dns地点或搜索域,其中addresses子项是dns地点列表,search子项是搜索域名列表
version: 2这一项表示YAML版本是2,curtin,MaaS等目前使用的YAML的版本是1
  1. root@FRSBachDEV3:~# netplan apply
  2. root@FRSBachDEV3:~# cat /etc/resolv.conf
  3. nameserver 127.0.0.53
  4. options edns0 trust-ad
  5. search dai.netdai.com netdai.com
  6. root@FRSBachDEV3:~# ifconfig
  7. ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  8.         inet 172.22.136.147  netmask 255.255.252.0  broadcast 172.22.139.255
  9.         inet6 fe80::250:56ff:fe94:522d  prefixlen 64  scopeid 0x20<link>
  10.         ether 00:50:56:94:52:2d  txqueuelen 1000  (Ethernet)
  11.         RX packets 45071  bytes 3394009 (3.3 MB)
  12.         RX errors 0  dropped 40  overruns 0  frame 0
  13.         TX packets 765  bytes 78732 (78.7 KB)
  14.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  15. lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  16.         inet 127.0.0.1  netmask 255.0.0.0
  17.         inet6 ::1  prefixlen 128  scopeid 0x10<host>
  18.         loop  txqueuelen 1000  (Local Loopback)
  19.         RX packets 2331  bytes 168025 (168.0 KB)
  20.         RX errors 0  dropped 0  overruns 0  frame 0
  21.         TX packets 2331  bytes 168025 (168.0 KB)
  22.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
复制代码
查察网关
  1. root@FRSBachDEV3:~# ip route | grep default
  2. default via 172.22.136.1 dev ens160 proto static
  3. root@FRSBachDEV3:~# nmcli dev show|grep GATEWAY
  4. IP4.GATEWAY:                            172.22.136.1
  5. IP6.GATEWAY:                            --
  6. IP4.GATEWAY:                            --
  7. IP6.GATEWAY:                            --
复制代码
查察dns
  1. root@FRSBachDEV3:~# resolvectl status
  2. Global
  3.        Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  4. resolv.conf mode: stub
  5. Link 2 (ens160)
  6.     Current Scopes: DNS
  7.          Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  8. Current DNS Server: 172.22.10.66
  9.        DNS Servers: 172.22.10.66 172.22.10.67
  10.         DNS Domain: dai.netdai.com netdai.com
复制代码
实验:怎样才会使用到/etc/resolve.conf中的dns信息
  1. /etc/systemd/resolved.conf中dns为172.22.136.2
  2. root@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS=
  3. DNS=172.22.136.2
  4. /etc/resolv.conf来自软链接/run/systemd/resolve/stub-resolv.conf
  5. root@FRSBachDEV3:~# ll /etc/resolv.conf
  6. lrwxrwxrwx 1 root root 39 Aug  9  2022 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
  7. root@FRSBachDEV3:~# cat /run/systemd/resolve/stub-resolv.conf
  8. nameserver 127.0.0.53
  9. options edns0 trust-ad
  10. search dai.netdai.com netdai.com
  11. /run/systemd/resolve/resolv.conf有来自/etc/systemd/resolved.conf中dns 172.22.136.2,也有来自/etc/netplan/00-installer-config.yaml中dns 172.22.10.66 172.22.10.67,就是没有来自/etc/resolv.conf的127.0.0.53
  12. root@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.conf
  13. nameserver 172.22.136.2
  14. nameserver 172.22.10.66
  15. nameserver 172.22.10.67
  16. search dai.netdai.com netdai.com
  17. /etc/systemd/resolved.conf中dns 172.22.136.2和/etc/netplan/00-installer-config.yaml中dns 172.22.10.66 172.22.10.67,就是没有来自/etc/resolv.conf的127.0.0.53
  18. root@FRSBachDEV3:~# resolvectl status
  19. Global
  20.          Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  21.   resolv.conf mode: stub
  22. Current DNS Server: 172.22.136.2
  23.        DNS Servers: 172.22.136.2
  24. Link 2 (ens160)
  25.     Current Scopes: DNS
  26.          Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  27. Current DNS Server: 172.22.10.66
  28.        DNS Servers: 172.22.10.66 172.22.10.67
  29.         DNS Domain: dai.netdai.com netdai.com
  30. 删除/etc/resolv.conf来自软链接,并手工建立/etc/resolv.conf文件,其中dns为172.22.136.3
  31. root@FRSBachDEV3:~# ll /etc/resolv.conf
  32. -rw-r--r-- 1 root root 946 Oct 12 10:49 /etc/resolv.conf
  33. root@FRSBachDEV3:~# cat /etc/resolv.conf
  34. nameserver 172.22.136.3
  35. options edns0 trust-ad
  36. search dai.netdai.com netdai.com
  37. 保留/etc/systemd/resolved.conf中dns 172.22.136.2的情况下,重启systemd-resolved,发现还是用的/etc/systemd/resolved.conf中dns 172.22.136.2而没有使用手工建立/etc/resolv.conf文件的dns 172.22.136.3
  38. root@FRSBachDEV3:~# systemctl restart systemd-resolved
  39. root@FRSBachDEV3:~# resolvectl status
  40. Global
  41.        Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  42. resolv.conf mode: foreign
  43.      DNS Servers: 172.22.136.2
  44. Link 2 (ens160)
  45. Current Scopes: DNS
  46.      Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  47.    DNS Servers: 172.22.10.66 172.22.10.67
  48.     DNS Domain: dai.netdai.com netdai.com
  49. 注释掉/etc/systemd/resolved.conf中dns再重启systemd-resolved,发现这个时候才真正用上了手工建立/etc/resolv.conf文件的dns 172.22.136.3
  50. root@FRSBachDEV3:~# vim /etc/systemd/resolved.conf
  51. root@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS
  52. #DNS
  53. root@FRSBachDEV3:~# systemctl restart systemd-resolved
  54. root@FRSBachDEV3:~# resolvectl status
  55. Global
  56.          Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  57.   resolv.conf mode: foreign
  58. Current DNS Server: 172.22.136.3
  59.        DNS Servers: 172.22.136.3
  60.         DNS Domain: dai.netdai.com netdai.com
  61. Link 2 (ens160)
  62. Current Scopes: DNS
  63.      Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  64.    DNS Servers: 172.22.10.66 172.22.10.67
  65.     DNS Domain: dai.netdai.com netdai.com
  66. root@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.conf
  67. nameserver 172.22.136.3
  68. nameserver 172.22.10.66
  69. nameserver 172.22.10.67
  70. search dai.netdai.com netdai.com
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦见你的名字

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表