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/

### 设置静态IP地址Ubuntu 对于希望在Ubuntu服务器上配置静态IP地址的情况,操作过程依赖于所使用的Ubuntu版本以及网络管理工具。下面提供适用于不同情况下的指导。 #### 对于Ubuntu Server 16.04 LTS 使用netplan 配置文件编辑方式 Netplan是一个声明式的网络配置工具,在较新的Ubuntu发行版中被采用作为默认的网络配置方法。为了给Ubuntu Server 16.04设置静态IP地址,需修改`/etc/netplan/*.yaml`中的相应配置文件[^1]: ```bash network: version: 2 ethernets: ens33: # 这里应替换为实际网卡名称 dhcp4: no addresses: - 192.168.1.100/24 # 替换成想要分配的具体IPv4地址及子网掩码长度 gateway4: 192.168.1.1 # 默认网关地址 nameservers: addresses: [8.8.8.8, 8.8.4.4] # DNS服务器列表 ``` 完成上述更改之后保存退出,并通过命令应用新配置: ```bash sudo netplan apply ``` #### 修改云初始化配置禁用动态获取网络配置(针对某些特定环境) 如果是在基于云端部署的情况下遇到问题,则可能需要进一步调整云初始化行为来确保不会覆盖本地所做的任何改动。这可以通过创建或编辑位于路径`/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg`下的文件实现[^4]: ```yaml network: {config: disabled} ``` 此步骤并非总是必需,具体取决于目标主机的实际运行状况及其所在的工作环境中是否存在类似的自动化机制影响到网络参数设定流程。 #### 更新DNS服务器信息 当指定静态IP的同时也应当考虑更新系统的域名解析服务指向。按照指示将`name server xxx.xxx.xxx.xxx`里的占位符替换成有效的DNS服务器地址即可满足需求[^3]。 以上就是在Ubuntu操作系统内实施固定IP地址的主要途径之一;值得注意的是各版本间可能存在细微差异因此建议参照官方文档获得最准确的操作指南。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

willops

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

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

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

打赏作者

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

抵扣说明:

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

余额充值