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

这篇具有很好参考价值的文章主要介绍了ubuntu 22.04版本修改服务器名、ip,dns信息的操作方法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

总结
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文件都将被使用

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

备注:
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

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

查看网关

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:                            --

查看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

实验:如何才会使用到/etc/resolve.conf中的dns信息文章来源地址https://www.toymoban.com/news/detail-745822.html

/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

到了这里,关于ubuntu 22.04版本修改服务器名、ip,dns信息的操作方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Ubuntu 22.04 安装配置时间同步服务器

    参数解释: 1.server 127.127.1.0 #local clock 这个参数指定了一个本地时钟源。127.127.1.0 ,通常用于表示本地计算机的时钟。这个参数告诉NTP守护进程,如果无法从其他NTP服务器获取时间,或者作为备份时钟源,应该使用本地计算机的时钟作为时间源。 2.fudge 127.127.1.0 stratum 10 这个参

    2024年04月28日
    浏览(49)
  • ubuntu 22.04 服务器网卡无IP地址

    ssh连接服务器连接不上,提示如下; 连接显示器 ,ip addr ls 命令查看IP地址,有网卡但没有IP地址 用于通过 DHCP 协议获取网络配置信息并为名为 enp10s0 的网络接口分配 IP 地址,enp10s0替换为本机网络接口名称 但是一旦重启 ,又没了IP地址 。目前为止暂未找到解决办法 偶然的机

    2024年02月22日
    浏览(41)
  • ubuntu22.04 服务器 SSH 密钥登录失败

    SSH密钥登录,是将SSH公钥写入服务端的 ~/.ssh/authorized_keys 文件中。 今天装了ubuntu22.04的系统,按照以往操作,在服务端配置了SSH公钥之后,发现竟然无法登录。 首先查看OpenSSH版本: 查看 /var/log/auth.log 文件,发现有如下错误信息: 通过错误信息来看,填入 authorized_keys 文件的

    2024年01月18日
    浏览(62)
  • Ubuntu22.04 安装深度学习服务器全纪录

    制作启动盘 参考链接:https://blog.csdn.net/lyx_ok/article/details/129308753 安装 Ubuntu 22.04 将U盘插到服务器上,开机按F11键(具体什么键跟主板型号有关)选择启动项进入临时的 Ubuntu 系统,在图形界面中选择 Install Ubuntu ,所有配置都可以使用默认的,改一下用户名和密码即可。 进入

    2024年02月16日
    浏览(39)
  • 【Ubuntu】ubuntu22.04使用VNC链接服务器远程桌面

    本地主要需要一个VNC客户端,用来远程连接服务器端的VNC(在不安装Web版本VNC情况下)。VNC客户端下载地址: VNC客户端下载 在远程服务器控制台中安装Xfce桌面(这个桌面环境比较轻量化,博主用的88块钱的腾讯云服务器) 注意更新软件源 在远程服务器控制台中安装 tightvn

    2024年02月03日
    浏览(45)
  • Ubuntu服务器安装配置slurm (Ubuntu 22.04 LTS)

    Slurm 全称 S imple L inux U tility for R esource M anagement。通常被用于大型Linux服务器 (超算) 上,作为任务管理系统。本文详细讲述如何在 Ubuntu 22.04 LTS 上安装slurm,并进行简单的配置。 其实网上相关的教程已经非常多,但在旧版本的Ubuntu上安装slurm时,通常需要安装一个名为slurm-ll

    2024年02月07日
    浏览(49)
  • 在 ubuntu 22.04 上配置界面服务器 xrdp

    安装 vnc 服务器 和 xrdp 服务器 配置 ~/.xsession

    2024年02月07日
    浏览(38)
  • 在 ubuntu 22.04 上配置界面服务器 vnc

    xrdp服务器的安装 1.安装服务器 查看配置 2.设置密码 3.更改 ~/.vnc/xstartup 4.重新启动 5.连接

    2024年02月08日
    浏览(35)
  • 服务器Ubuntu 22.04 64位安装 MySQL5.7

    官网地址 这里选择下载5.7.29的ubuntu版本注意是amd64不是arm64,或者执行下面命令 解压下载下来的包 ls一下看看有什么 我们先安装这个 然后再装下一个 喜闻乐见的报错,说我们没有安装红圈里的两个,我们去安装 好嘛,server依赖client,client又缺少libtinfo5,那就不能先装clien

    2024年02月06日
    浏览(37)
  • 基于Ubuntu22.04的Samba服务器搭建教程(新手保姆级教程)

    Samba 是在 Linux 和 UNIX 系统上实现SMB 协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型

    2024年04月08日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包