Ubuntu 22.04 静态 IP 配置
This guide explains how to configure a static IP address on Ubuntu 22.04 using Netplan.
1. Check Current Network Status
Section titled “1. Check Current Network Status”Before making changes, identify your network interface name and current configuration.
# Check IP address and subnet maskip addr
# Check default gatewayip route
# Check DNS serversresolvectl status2. Configure Netplan
Section titled “2. Configure Netplan”Netplan configuration files are located in /etc/netplan/. The default file is usually named 00-installer-config.yaml or similar.
-
Backup the existing configuration:
Terminal window sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak -
Edit the configuration file:
Terminal window sudo vim /etc/netplan/00-installer-config.yamlDefault Configuration (DHCP):
# This is the network config written by 'subiquity'network:ethernets:ens160:dhcp4: trueversion: 2Static IP Configuration:
Modify the file to look like this (replace
ens160, IP addresses, and gateway with your own values):# This is the network config written by 'subiquity'network:ethernets:ens160:dhcp4: falseaddresses:- 192.168.1.2/24routes:- to: defaultvia: 192.168.1.1nameservers:addresses: [1.1.1.1, 8.8.8.8]version: 2[!IMPORTANT] YAML is indentation-sensitive. Ensure you use spaces (not tabs) and align the keys correctly.
3. Apply Configuration
Section titled “3. Apply Configuration”Apply the changes using Netplan.
sudo netplan applyIf there are no errors, verify the new IP address:
ip addr show ens160