Version: Dec 13, 2024
Setting a Static IP Address on Ubuntu Server
1. Identify Network Interface Details
- Use the command:
ip addr
- Identify the network interface name (e.g.,
eth0
,ens33
,wlan0
) and current IP configuration.
2. Access Netplan Configuration
- Ubuntu uses Netplan for network configuration. Locate the configuration file, typically in
/etc/netplan/
. Common filenames are01-netcfg.yaml
or50-cloud-init.yaml
. - Use:
ls /etc/netplan/
3. Back Up the Original Configuration
- Always back up the file before making changes:
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak
4. Edit the Configuration File
-
Open the configuration file with a text editor:
sudo nano /etc/netplan/01-netcfg.yaml
-
Replace or add the following configuration, substituting with your desired IP, gateway, and DNS:
network: version: 2 ethernets: eth0: # Replace with your interface name addresses: - 192.168.1.100/24 # Replace with your desired IP and subnet mask routes: - to: 0.0.0.0/0 via: 192.168.1.1 # Replace with your gateway nameservers: addresses: - 8.8.8.8 # Google DNS - 8.8.4.4
5. Apply Configuration
- Apply the changes using:
sudo netplan generate sudo netplan apply
6. Verify Configuration
- Confirm the changes using:
ip addr
- Test connectivity by pinging the gateway or an external IP:
ping 8.8.8.8
7. Troubleshooting
- If the network does not come up after applying the changes:
- Revert to the backup file:
sudo cp /etc/netplan/01-netcfg.yaml.bak /etc/netplan/01-netcfg.yaml sudo netplan apply
- Check Netplan syntax before applying changes:
sudo netplan try
- Verify the correct interface name and ensure no conflicting DHCP settings.
- Revert to the backup file:
8. Optional: Disable DHCP (if needed)
-
If your network was using DHCP and you're switching to a static IP, ensure that DHCP is disabled for the interface.
dhcp4: false
9. Restart Network Services
- If issues persist, restart the network service:
sudo systemctl restart systemd-networkd