Ubuntu 17.10下通过Netplan修改网卡IP、MAC及名称求助
Hey Zoltan, sorry to hear you’ve been grinding on this for hours—YAML’s indentation quirks and network config gotchas are no joke. Let’s walk through a reliable, step-by-step setup that should stick after reboot. We’ll focus on Netplan (common on Ubuntu 18.04+) since you mentioned YAML files, but I’ll throw in notes for other distros too.
1. First, Identify Your Current Network Hardware
Before writing any config, let’s get clear on what we’re working with. Run these commands in your terminal:
ip link show: Lists all network interfaces. Note the current name (likeenp0s3) and its original MAC address (looks likeaa:bb:cc:dd:ee:ff).ip route show: Find your default gateway (usually something like192.168.1.1).cat /etc/resolv.conf: Grab your DNS servers (e.g.,8.8.8.8or your router’s IP).
2. Write a Valid Netplan YAML Config
Netplan lives in /etc/netplan/—you’ll probably have a file like 00-installer-config.yaml or 01-netcfg.yaml. Replace its contents with this template, adjusting the values to match your network:
network: version: 2 ethernets: # We'll use the original MAC to target the interface (avoids name changes) enp0s3: # You can keep this, or use a custom name later match: macaddress: aa:bb:cc:dd:ee:ff # Original MAC from ip link show set-name: my-custom-nic # Your desired persistent interface name addresses: - 192.168.1.100/24 # Static IP + subnet mask (e.g., /24 = 255.255.255.0) gateway4: 192.168.1.1 # Your default gateway nameservers: addresses: [8.8.8.8, 1.1.1.1] # DNS servers macaddress: aa:bb:cc:dd:ee:ff # Your custom MAC address (must be unique on the network!)
Critical YAML Rules to Avoid Failures:
- Use 2 spaces for indentation—tabs will break the config.
- Don’t add extra spaces after colons (e.g.,
macaddress: aa:bb:ccis good;macaddress : aa:bb:ccis bad). - Make sure the custom MAC is valid (6 pairs of hex digits, no duplicates on your local network).
3. Lock in the Persistent Interface Name (Udev Rule)
Even with Netplan’s set-name, sometimes the system reverts to random names on reboot. To fix this, create a udev rule:
- Create a file:
sudo nano /etc/udev/rules.d/70-persistent-net.rules - Add this line, replacing the values with your hardware info:
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="aa:bb:cc:dd:ee:ff", NAME="my-custom-nic"ATTR{address}: The original MAC of your network card (not the custom one you’re setting).NAME: Your desired permanent interface name.
4. Apply the Config and Test
Run these commands to activate your changes without rebooting first:
sudo netplan generate: Checks for YAML syntax errors (if this fails, fix the indentation/typos first!).sudo netplan apply: Applies the config immediately.- Verify with
ip link showandip a: Check that the interface name, MAC, and static IP are all correct.
5. Reboot and Confirm
Finally, reboot your system with sudo reboot, then re-run ip link show and ip a to make sure everything sticks. If it still doesn’t work:
- Check
/var/log/syslogfor Netplan/udev errors (look for lines withnetplanorudev). - Make sure your router allows static IPs (some require reserving the IP in the router’s admin panel).
- If you’re on a non-Ubuntu distro (like CentOS/RHEL), you’ll use NetworkManager instead—just let me know and I can walk you through that setup.
内容的提问来源于stack exchange,提问作者Zoltan Szecsei

