Ubuntu20.04 设置静态ip
Ubuntu 从 17.10 开始,已放弃在 /etc/network/interfaces 里固定 IP 的配置,interfaces 文件不复存在,即使配置也不会生效,而是改成 netplan 方式 ,配置写在 /etc/netplan/01-netcfg.yaml ,
50-cloud-init.yaml
或者类似名称的 yaml 文件里。
1. 查看网卡设备号
ip address
2. 修改 YAML 文件
sudo vim /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
eth0: # 配置网卡的名称
dhcp4: no # 禁用dhcp,如果需要启动则写yes
optional: true
addresses: # 配置静态IP地址和掩码
- 192.168.31.173/24
gateway4: 192.168.16.1 # 设置网关地址
nameservers:
addresses: [202.102.152.3, 114.114.114.114] # 设置主、备DNS,多个DNS服务地址需要用英文逗号隔开
version: 2
renderer: networkd #指定后端采用systemd-networkd或者NetworkManager,可不填写则默认使用systemd-workd
根据自己的需要配置好之后保存文件
3. 应用配置
sudo netplan apply
4. 确认IP地址已经更改
ip addr show
连接主机...
连接主机成功
Last login: Wed Jul 24 12:42:56 2024 from 192.168.31.172
root@ubuntu:~# cd /etc/netplan/
root@ubuntu:/etc/netplan# sudo netplan apply
root@ubuntu:/etc/netplan# ip addr show
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether dc:a6:32:c5:41:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.173/24 brd 192.168.31.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::dea6:32ff:fec5:4101/64 scope link
valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether dc:a6:32:c5:41:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.137.208/24 brd 192.168.137.255 scope global dynamic noprefixroute wlan0
valid_lft 604416sec preferred_lft 604416sec
inet6 fe80::21a0:fa53:db59:10cf/64 scope link noprefixroute
valid_lft forever preferred_lft forever
root@ubuntu:/etc/netplan#
确保你的Ubuntu系统没有通过DHCP获取IP地址,并且网络接口配置正确。如果你有多个网络接口,请确保只为你想要配置静态IP的接口设置静态地址。
原文地址:https://blog.csdn.net/qq_25231683/article/details/140660263
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!