ubuntu 配置静态IP地址和DNS

netplan简介

参考:
https://netplan.io/
https://netplan.io/examples
https://github.com/canonical/netplan

Ubuntu使用 Netplan 管理网络,Netplan是一个用于在 linux 系统上轻松配置网络的实用程序。只需创建所需网络接口的 YAML 描述以及每个应配置的功能,根据此描述,Netplan 将为您选择的渲染器工具生成所有必要的配置。

在这里插入图片描述

Netplan 读取由管理员、安装人员、云映像实例化或其他操作系统部署编写的/etc/netplan/*.yaml网络配置。在早期启动期间,Netplan 会在/run生成后端特定的配置文件,以将设备的控制权移交给特定的网络守护进程。

Netplan 目前可与这些受支持的渲染器一起使用

  • NetworkManager
  • Systemd-networkd

在这里插入图片描述

配置静态或动态IP地址

以 ubuntu server LTS 22.04为例:

1、修改网络配置文件

配置静态IP地址示例,其中routes.via为网关地址

cat > /etc/netplan/00-installer-config.yaml <<EOF
network:
    version: 2
    renderer: networkd
    ethernets:
        ens33:
            addresses:
                - 192.168.92.4/24
            nameservers:
                search: [mydomain, otherdomain]
                addresses: [114.114.114.114, 8.8.8.8]
            routes:
                - to: default
                  via: 192.168.92.2
EOF

配置DHCP动态IP示例:

cat > /etc/netplan/00-installer-config.yaml <<EOF
network:
    version: 2
    renderer: networkd
    ethernets:
        enp3s0:
            dhcp4: true
EOF

2、测试配置文件

sudo netplan try

3、应用配置文件

sudo netplan apply

如果遇到错误可以执行debug

netplan --debug apply

4、验证 IP 地址

root@ubuntu:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:14:45:93 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.92.4/24 brd 192.168.92.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe14:4593/64 scope link 
       valid_lft forever preferred_lft forever

查看网关配置

root@ubuntu:~# ip route
default via 192.168.92.2 dev ens33 proto static 
192.168.92.0/24 dev ens33 proto kernel scope link src 192.168.92.4 

查看dns配置

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

Link 2 (ens33)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 114.114.114.114
       DNS Servers: 114.114.114.114 8.8.8.8
        DNS Domain: mydomain otherdomain

配置DNS

查看/etc/resolv.conf 文件,其中nameserver默认为 127.0.0.5,修改dns地址直接配置netplan文件即可,不要直接修改/etc/resolv.conf配置文件

root@ubuntu:~# cat /etc/resolv.conf 
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad
search .

禁用本地 dns 53端口监听

默认/etc/resolv.conf软链接到/run/systemd/resolve/stub-resolv.conf,该文件由systemd-resolved管理:

root@ubuntu:~# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Apr 21 09:00 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

/run/systemd/resolve/* 生成的配置文件是由/etc/systemd/resolved.conf管理,该/run/systemd/resolve/目录下有两个resolv.conf文件:

root@ubuntu:~# ll /run/systemd/resolve/
total 8
drwxr-xr-x  2 systemd-resolve systemd-resolve 100 Apr 26 17:31 ./
drwxr-xr-x 24 root            root            580 Apr 26 17:31 ../
srw-rw-rw-  1 systemd-resolve systemd-resolve   0 Apr 26 17:31 io.systemd.Resolve=
-rw-r--r--  1 systemd-resolve systemd-resolve 809 Apr 26 17:31 resolv.conf
-rw-r--r--  1 systemd-resolve systemd-resolve 920 Apr 26 17:31 stub-resolv.conf

查看/run/systemd/resolve/resolv.conf文件,与netplan文件中配置的nameserver一致,修改netplan配置中的nameserver该文件将会自动同步更新:

root@ubuntu:/run/systemd/resolve# cat resolv.conf 
# This is /run/systemd/resolve/resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 114.114.114.114
nameserver 8.8.8.8
search .

查看systemd-resolved服务,请勿禁用该服务,否则将导致外网解析失败。

systemctl status systemd-resolved

systemd-resolved服务默认监听53端口,如果直接启用其他dns服务器会报端口冲突错误:“listen tcp 0.0.0.0:53: bind: address already in use” .

root@bastion:~# lsof -i :53
COMMAND    PID            USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd-r 1687 systemd-resolve   12u  IPv4  45468      0t0  UDP localhost:domain 
systemd-r 1687 systemd-resolve   13u  IPv4  45469      0t0  TCP localhost:domain (LISTEN)

可以通过编辑 /etc/systemd/resolved.conf,修改DNSStubListener参数禁用53端口监听

root@bastion:~# vim /etc/systemd/resolved.conf
......
[Resolve]
DNSStubListener=no

或者通过创建配置文件修改

mkdir -p /etc/systemd/resolved.conf.d/
cat >/etc/systemd/resolved.conf.d/98-disable-127-53.conf << EOF
[Resolve]
DNSStubListener=no
EOF

重启systemd-resolved服务

systemctl daemon-reload
systemctl restart systemd-resolved.service
systemctl status -l systemd-resolved.service --no-pager

查看53端口已经取消占用

root@bastion:~# lsof -i :53

在禁用systemd-resolved端口监听后,查看/etc/resolv.conf 中的变化

root@ubuntu:~# cat /etc/resolv.conf 
# This is /run/systemd/resolve/resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 114.114.114.114
nameserver 8.8.8.8
search .

查看stub-resolv.conf文件变化,作为软连接重新指向了resolv.conf

root@ubuntu:~# ll /run/systemd/resolve/
total 4
drwxr-xr-x  2 systemd-resolve systemd-resolve 100 Apr 26 18:37 ./
drwxr-xr-x 25 root            root            600 Apr 26 18:37 ../
srw-rw-rw-  1 systemd-resolve systemd-resolve   0 Apr 26 18:37 io.systemd.Resolve=
-rw-r--r--  1 systemd-resolve systemd-resolve 809 Apr 26 18:00 resolv.conf
lrwxrwxrwx  1 systemd-resolve systemd-resolve  11 Apr 26 18:37 stub-resolv.conf -> resolv.conf

参考:https://dyrnq.com/ubuntu-update-etc-resolve-conf/

Ubuntu系统中配置静态IP地址有两种方法。第一种方法是通过命令行进行配置。首先,您可以使用ifconfig命令查看当前分配给虚拟机的IP地址。然后,使用ip route命令查看网关的IP地址。接下来,使用nano或vi编辑器打开/etc/network/interfaces文件,并添加以下内容: auto [网卡名称] iface [网卡名称 inet static address [静态IP地址] netmask [子网掩码] gateway [网关IP地址] 将替换为您的网络接口的名称,替换为您想要设置的静态IP地址,替换为您的子网掩码,替换为您的网关IP地址。保存并关闭文件后,通过运行sudo service networking restart命令来使更改生效。 第二种方法是通过netplan进行配置。首先,您可以使用ip addr命令查看虚拟机分配的IP地址。然后,使用ip route命令查看网关的IP地址。接下来,使用nano或vi编辑器打开/etc/netplan/50-cloud-init.yaml文件,并添加以下内容: network: version: 2 ethernets: [网卡名称]: dhcp4: no addresses: [静态IP地址/子网掩码] gateway4: [网关IP地址] nameservers: addresses: [DNS服务器IP地址] 将替换为您的网络接口名称,替换为您想要设置的静态IP地址和子网掩码,替换为您的网关IP地址,替换为您的DNS服务器IP地址。保存并关闭文件后,运行sudo netplan apply命令使更改生效。 请注意,以上方法适用于Ubuntu 18.04系统。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Ubuntu设置静态IP地址的几种方法](https://blog.csdn.net/fun_tion/article/details/126750615)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Ubuntu配置静态IP的方法](https://blog.csdn.net/qq_25919879/article/details/129284762)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

willops

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值