iis服务器助手广告
返回顶部
首页 > 资讯 > 服务器 >ubuntu 22.04版本修改服务器名、ip,dns信息的操作方法
  • 581
分享到

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

服务器ubuntutcp/ip 2023-12-22 21:12:56 581人浏览 八月长安
摘要

总结 1、ubuntu修改服务器名重启后生效的方法是直接修改/etc/hostname文件 ubuntu 22.04操作系统配置ip和dns信息,一般只需要使用netplan命令行工具来配置就行,在/etc/netplan/在目录下创建一个

总结
1、ubuntu修改服务器名重启后生效的方法是直接修改/etc/hostname文件

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

个人不建议使用/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

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.

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.yamlnetwork:  ethernets:    ens160:      dhcp4: true  version: 2
root@FRSBachDEV3:~# vim /etc/netplan/00-installer-config.yamlnetwork:  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 applyroot@FRSBachDEV3:~# cat /etc/resolv.confnameserver 127.0.0.53options edns0 trust-adsearch dai.netdai.com netdai.comroot@FRSBachDEV3:~# ifconfigens160: flags=4163  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        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 0lo: flags=73  mtu 65536        inet 127.0.0.1  netmask 255.0.0.0        inet6 ::1  prefixlen 128  scopeid 0x10        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 defaultdefault via 172.22.136.1 dev ens160 proto staticroot@FRSBachDEV3:~# nmcli dev show|grep GATEWAYIP4.GATEWAY:172.22.136.1IP6.GATEWAY:--IP4.GATEWAY:--IP6.GATEWAY:--

查看dns

root@FRSBachDEV3:~# resolvectl statusGlobal       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupportedresolv.conf mode: stubLink 2 (ens160)    Current Scopes: DNS         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupportedCurrent 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信息

/etc/systemd/resolved.conf中dns为172.22.136.2root@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS=DNS=172.22.136.2/etc/resolv.conf来自软链接/run/systemd/resolve/stub-resolv.confroot@FRSBachDEV3:~# ll /etc/resolv.conflrwxrwxrwx 1 root root 39 Aug  9  2022 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.confroot@FRSBachDEV3:~# cat /run/systemd/resolve/stub-resolv.confnameserver 127.0.0.53options edns0 trust-adsearch 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.53root@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.confnameserver 172.22.136.2nameserver 172.22.10.66nameserver 172.22.10.67search 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.53root@FRSBachDEV3:~# resolvectl statusGlobal         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported  resolv.conf mode: stubCurrent DNS Server: 172.22.136.2       DNS Servers: 172.22.136.2Link 2 (ens160)    Current Scopes: DNS         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupportedCurrent 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.3root@FRSBachDEV3:~# ll /etc/resolv.conf-rw-r--r-- 1 root root 946 Oct 12 10:49 /etc/resolv.confroot@FRSBachDEV3:~# cat /etc/resolv.confnameserver 172.22.136.3options edns0 trust-adsearch 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.3root@FRSBachDEV3:~# systemctl restart systemd-resolvedroot@FRSBachDEV3:~# resolvectl statusGlobal       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupportedresolv.conf mode: foreign     DNS Servers: 172.22.136.2Link 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.3root@FRSBachDEV3:~# vim /etc/systemd/resolved.confroot@FRSBachDEV3:~# cat /etc/systemd/resolved.conf |grep DNS#DNSroot@FRSBachDEV3:~# systemctl restart systemd-resolvedroot@FRSBachDEV3:~# resolvectl statusGlobal         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported  resolv.conf mode: foreignCurrent DNS Server: 172.22.136.3       DNS Servers: 172.22.136.3        DNS Domain: dai.netdai.com netdai.comLink 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.comroot@FRSBachDEV3:~# cat /run/systemd/resolve/resolv.confnameserver 172.22.136.3nameserver 172.22.10.66nameserver 172.22.10.67search dai.netdai.com netdai.com

来源地址:https://blog.csdn.net/lusklusklusk/article/details/133798861

--结束END--

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

本文链接: https://www.lsjlt.com/news/551497.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • ubuntu 22.04版本修改服务器名、ip,dns信息的操作方法
    总结 1、ubuntu修改服务器名重启后生效的方法是直接修改/etc/hostname文件 ubuntu 22.04操作系统配置ip和dns信息,一般只需要使用netplan命令行工具来配置就行,在/etc/netplan/在目录下创建一个...
    99+
    2023-12-22
    服务器 ubuntu tcp/ip
  • 修改DNS服务器的方法
    本篇内容介绍了“修改DNS服务器的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!什么是DNS DNS 是指:域名服务器(Domain N...
    99+
    2023-06-07
  • 在Linux操作系统下修改IP、DNS和路由配置的方法
    这篇文章主要介绍“在Linux操作系统下修改IP、DNS和路由配置的方法”,在日常操作中,相信很多人在在Linux操作系统下修改IP、DNS和路由配置的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”在Li...
    99+
    2023-06-13
  • windows命令行修改ip地址和dns服务器地址的方法
    每个星期都要周转于实验室与宿舍之间。实验室该死的公共路由器锁定IP。完事每次动态IP又总是错误地址,只能一次一次的手动修改。然后我又那么懒不想打开图形化界面去操作,索性学了一手。 命令格式:netsh in 命令解释: ...
    99+
    2023-06-04
    windows命令行 ip地址 dns服务器 dns 地址 服务器 ip 命令行
  • 国际版阿里云服务器改密码的操作方法
    在使用国际版阿里云服务器时,有时需要修改服务器的登录密码以增强服务器的安全性。本文将介绍如何在国际版阿里云服务器上修改密码,并提供详细的步骤和示例。步骤一:登录阿里云控制台首先,在浏览器中打开阿里云官方网站,点击右上角的"登录"按钮,输入...
    99+
    2024-01-20
    国际版 阿里 操作方法
  • 阿里云服务器修改操作系统设置方法
    阿里云服务器修改操作系统设置是一个很常见的操作,以下是一些可能有用的步骤: 在阿里云客户端上登录并进行身份验证(即确认您在阿里云服务器上的账号和密码): 在操作系统设置窗口中,选择“修改系统设置”并点击“保存设置”: 在保存设置的对话框...
    99+
    2023-10-27
    阿里 系统设置 操作
  • 亚马逊云服务器IP地址的修改方法
    1. 登录亚马逊云控制台 首先,打开您的浏览器,访问亚马逊云服务(AWS)的控制台。输入您的用户名和密码,然后点击登录。 2. 导航到EC2服务 在控制台的顶部菜单栏中,找到并点击“服务”按钮。在下拉菜单中,选择“EC2”服务。 3. 选...
    99+
    2023-10-27
    亚马逊 地址 服务器
  • 阿里云服务器修改操作系统设置方法在哪
    一、创建新的RDS实例 1.登录阿里云控制台,进入RDS实例管理页面。 2.点击“新建实例”按钮,弹出新建RDS实例的页面。 3.输入RDS实例的名称、配置文件、用户名和密码等信息,点击“创建”按钮。 4.在创建RDS实例的页面中,选择“...
    99+
    2023-10-28
    阿里 系统设置 操作
  • 阿里云服务器登陆名称的修改方法
    阿里云服务器的登录名称是每个用户在使用阿里云服务器时都需要了解和掌握的知识点之一。本文将详细介绍如何修改阿里云服务器的登录名称。 阿里云服务器的登录名称是用户在使用阿里云服务器时的唯一标识,如果需要修改登录名称,可以按照以下步骤进行操作。一...
    99+
    2023-11-07
    阿里 名称 服务器
  • 泛域名绑定服务器的操作方法是什么
    泛域名绑定服务器是指将多个子域名绑定到一个主域名上,使得访问这些子域名时都指向同一个服务器。具体操作方法如下:1. 在DNS管理控制...
    99+
    2023-06-12
    泛域名绑定 域名
  • 云服务器数据备份到本地服务器上的操作方法
    1. 确定备份需求和目标 在进行云服务器数据备份到本地服务器的操作之前,首先需要明确备份的需求和目标。确定需要备份的数据类型、备份频率、备份的目的地等。 2. 选择备份工具 选择适合的备份工具是进行数据备份的关键一步。根据你的需求和服务器...
    99+
    2023-10-27
    数据备份 操作方法 器上
  • 阿里云服务器更换IP地址的方法及手机端操作指南
    本文将详细介绍如何在阿里云服务器上更换IP地址,并提供详细的手机端操作指南。通过阅读本文,读者将了解更换IP地址的步骤、注意事项以及常见问题的解决方法。 随着互联网技术的发展,企业对于服务器的安全性和稳定性要求越来越高。在阿里云服务器上更换...
    99+
    2024-01-25
    阿里 操作指南 地址
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作