How to modify server name, ip, dns information in ubuntu 22.04 version

Summary
1. The way to modify the server name in Ubuntu to take effect after restarting is to directly modify the /etc/hostname file

2. To configure ip and dns information in the Ubuntu 22.04 operating system, you generally only need to use the netplan command line tool to configure it. Create a yaml file in the /etc/netplan/ directory to configure ip and dns. Of course, if /etc If there are multiple yaml files under /netplan, all /etc/netplan/*.yaml files will be used by the netplan command line. See the official documentation https://ubuntu.com/server/docs/network-configuration and https:/ /manpages.ubuntu.com/manpages/jammy/man5/netplan.5.html

3. I personally do not recommend using /etc/resolve.conf to configure the dns information of the ubuntu 22.04 operating system because it is too complicated. This file is hosted by the systemd-resolved service. The nameserver 127.0.0.53 loopback address is used by default in /etc/resolve.conf of the ubuntu operating system. There is a sentence in /etc/resolve.conf This is /run/systemd/resolve/stub-resolv.conf managed by man: systemd-resolved(8). Do not edit. Indicates that this file is hosted by the systemd-resolved service. Through netstat -tnpl | grep systemd-resolved, we can see that this service is listening on port 53. Why should we use 127.0.0.53 as the loopback address instead of 127.0.0.1? Because the 127 network segments are all loopback addresses (127.0.0.1 - 127.255.255.254), but usually people only like to use 127.0.0.1 to express it, and 53 represents dns port
3.1. In the newly installed ubuntu 22.04 environment without making other changes, /etc/resolv.conf is a soft link to /run/systemd/resolve/stub-resolv.conf. If you just manually change the contents of /etc/resolv.conf, you will find that the contents of /etc/resolv.conf have returned to their original state after restarting the session. For example, comment the nameserver 127.0.0.53 line in the /etc/resolv.conf file. , the line nameserver 127.0.0.53 that was commented out after restarting the system is back. If you do not want the contents in /etc/resolv.conf to be restored to the original after restarting, execute systemctl stop systemd-resolved and systemctl disable systemd-resolved, or Delete the soft link corresponding to /etc/resolv.conf, and then manually create a /etc/resolv.conf
3.2. Use /etc/netplan/00-installer-config.yaml to configure DNS 172.22 .10.66 and execute netplan apply to make it effective, then modify the DNS in the content of /etc/systemd/resolved.conf to 172.22.136.2, and then execute systemctl restart systemd-resolved, and find that the content in /etc/resolv.conf has not changed. 127.0.0.53, and then execute resolvectl status, you can see that DNS 172.22.136.2 in /etc/systemd/resolved.conf exists, and DNS 172.22.10.66 in /etc/netplan/00-installer-config.yaml also exists, but there is no 127.0.0.53 in /etc/resolv.conf. After restarting the operating system, the content in /etc/resolv.conf has not changed and is still 127.0.0.53. After restarting the operating system, executing resolvectl status still only contains DNS 172.22.136.2 and /etc/netplan/00 in /etc/systemd/resolved.conf. - DNS 172.22.10.66 in installer-config.yaml, no 127.0.0.53 in /etc/resolv.conf

4. Ubuntu officially does not recommend using /etc/resolve.conf to configure Ubuntu’s DNS information. Ubuntu’s official description of /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. The command to view dns information in ubuntu 22.04 is resolvectl status. This command can see the global dns information and the dns information of a certain network card. The global DNS server information generally comes from the configuration file /etc/systemd/resolved.conf, only in /etc When /resolv.conf is not a soft link pointing to one of /run/systemd/resolve/stub-resolv.conf, /usr/lib/systemd/resolv.conf, /run/systemd/resolve/resolv.conf, And only when dns in /etc/systemd/resolved.conf is commented out, the global DNS server will read from /etc/resolv.conf, so it is not recommended that you use /etc/resolve.conf to configure ubuntu. DNS information.

Modify the server name
Modify the server name to FRSBachDEV3 and it will take effect after restarting
vi /etc/hostname
FRSBachDEV3
Note: If the content in the /etc/hostname file is written as hostname FRSBachDEV3, then the server name will become hostnameFRSBachDEV3 after restarting.

Modify IP
https://ubuntu.com/server/docs/network-configuration
https://manpages.ubuntu.com/ manpages/jammy/man5/netplan.5.html
netplan is a command line tool used to configure the network on ubuntu. All /etc/netplan/*.yaml files will be used a>

root@FRSBachDEV3:~# cat /etc/netplan/00-installer-config.yaml
network:
  ethernets:
    ens160:
      dhcp4: true
  version: 2
root@FRSBachDEV3:~# vim /etc/netplan/00-installer-config.yaml
network:
  ethernets:
    ens160:
      dhcp4: false
      addresses: [172.22.136.147/22]
      routes:
        - to: default
          via: 172.22.136.1
      nameservers:
        search: [dai.netdai.com,netdai.com]
        addresses: [172.22.10.66,172.22.10.67]
  version: 2

Remarks:
The 4 in dhcp4 means ipv4
The 4 in gateway4 means ipv4, but gateway4 has been abandoned. Configure gateway4 and then execute netplan An error will be reported when applying ** (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.
Fill in the gateway address for routes
Fill in ip address for addresses
Fill in dns address or search domain for nameservers. The addresses sub-item is the dns address list, and the search sub-item is the search domain name list
version: 2 indicates that the YAML version is 2, and the YAML version currently used by curtin, MaaS, etc. is 1< /span>

root@FRSBachDEV3:~# netplan apply

root@FRSBachDEV3:~# cat /etc/resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad
search dai.netdai.com netdai.com

root@FRSBachDEV3:~# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.136.147  netmask 255.255.252.0  broadcast 172.22.139.255
        inet6 fe80::250:56ff:fe94:522d  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:94:52:2d  txqueuelen 1000  (Ethernet)
        RX packets 45071  bytes 3394009 (3.3 MB)
        RX errors 0  dropped 40  overruns 0  frame 0
        TX packets 765  bytes 78732 (78.7 KB)
        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 2331  bytes 168025 (168.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2331  bytes 168025 (168.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

View gateway

root@FRSBachDEV3:~# ip route | grep default
default via 172.22.136.1 dev ens160 proto static

root@FRSBachDEV3:~# nmcli dev show|grep GATEWAY
IP4.GATEWAY:                            172.22.136.1
IP6.GATEWAY:                            --
IP4.GATEWAY:                            --
IP6.GATEWAY:                            --

View dns

root@FRSBachDEV3:~# resolvectl status
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (ens160)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 172.22.10.66
       DNS Servers: 172.22.10.66 172.22.10.67
        DNS Domain: dai.netdai.com netdai.com

Experiment: How to use the dns information in /etc/resolve.conf

/etc/systemd/resolved.conf中dns为172.22.136.2
root@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS=
DNS=172.22.136.2

/etc/resolv.conf来自软链接/run/systemd/resolve/stub-resolv.conf
root@FRSBachDEV3:~# ll /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Aug  9  2022 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

root@FRSBachDEV3:~# cat /run/systemd/resolve/stub-resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad
search dai.netdai.com netdai.com

/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
root@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.conf
nameserver 172.22.136.2
nameserver 172.22.10.66
nameserver 172.22.10.67
search dai.netdai.com netdai.com

/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
root@FRSBachDEV3:~# resolvectl status
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: stub
Current DNS Server: 172.22.136.2
       DNS Servers: 172.22.136.2

Link 2 (ens160)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 172.22.10.66
       DNS Servers: 172.22.10.66 172.22.10.67
        DNS Domain: dai.netdai.com netdai.com

删除/etc/resolv.conf来自软链接,并手工建立/etc/resolv.conf文件,其中dns为172.22.136.3
root@FRSBachDEV3:~# ll /etc/resolv.conf
-rw-r--r-- 1 root root 946 Oct 12 10:49 /etc/resolv.conf

root@FRSBachDEV3:~# cat /etc/resolv.conf
nameserver 172.22.136.3
options edns0 trust-ad
search dai.netdai.com netdai.com

保留/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
root@FRSBachDEV3:~# systemctl restart systemd-resolved
root@FRSBachDEV3:~# resolvectl status
Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: foreign
     DNS Servers: 172.22.136.2

Link 2 (ens160)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 172.22.10.66 172.22.10.67
    DNS Domain: dai.netdai.com netdai.com

注释掉/etc/systemd/resolved.conf中dns再重启systemd-resolved,发现这个时候才真正用上了手工建立/etc/resolv.conf文件的dns 172.22.136.3
root@FRSBachDEV3:~# vim /etc/systemd/resolved.conf
root@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS
#DNS

root@FRSBachDEV3:~# systemctl restart systemd-resolved
root@FRSBachDEV3:~# resolvectl status
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: foreign
Current DNS Server: 172.22.136.3
       DNS Servers: 172.22.136.3
        DNS Domain: dai.netdai.com netdai.com

Link 2 (ens160)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 172.22.10.66 172.22.10.67
    DNS Domain: dai.netdai.com netdai.com

root@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.conf
nameserver 172.22.136.3
nameserver 172.22.10.66
nameserver 172.22.10.67
search dai.netdai.com netdai.com

Guess you like

Origin blog.csdn.net/lusklusklusk/article/details/133798861