Configure a Static IP Address in Ubuntu
Learn how to configure, apply, and verify a persistent static IPv4 address, subnet, gateway, and DNS settings in Ubuntu using Netplan or ifupdown.
Static and Dynamic IP Addresses
An IPv4 address is a 32-bit network address commonly written in dotted-decimal form, such as 192.168.198.160. A network interface is the physical, virtual, or logical device that connects a host to a network. Typical interface names include eth0, enp0s3, and ens33.
DHCP, the Dynamic Host Configuration Protocol, leases network settings to a host. The lease can include an IP address, subnet information, a default gateway, and DNS servers. A DHCP-assigned address can change when the lease expires, the device reconnects, or the DHCP server changes its allocation.
A static IP address is manually configured and remains consistent until an administrator changes it. Static addresses are useful for servers, remote administration, network appliances, and hosts that receive port-forwarded traffic. They are also useful when other systems must reliably connect to a known address.
Choose the Correct Ubuntu Networking Method
Ubuntu networking differs by release and installation type. Many current Ubuntu installations use Netplan, a YAML-based configuration system. Netplan commonly uses NetworkManager as its backend on desktop installations and systemd-networkd on servers. Older Ubuntu systems, and systems explicitly configured with ifupdown, use /etc/network/interfaces.
Identify the active network stack before editing a file. Do not configure the same interface through competing tools, because one service can overwrite or ignore another service's settings.
| Environment or Ubuntu generation | Primary configuration mechanism | Configuration location | Safe apply command | Notes |
|---|---|---|---|---|
| Older Ubuntu or explicit ifupdown installation | ifupdown | /etc/network/interfaces | ifdown and ifup | Taking down an active interface can terminate SSH. |
| Modern Ubuntu Server | Netplan with systemd-networkd | /etc/netplan/*.yaml | netplan try, then netplan apply | Use YAML with exact indentation. |
| Modern Ubuntu Desktop | Netplan with NetworkManager | /etc/netplan/*.yaml or NetworkManager-managed settings | netplan try, then netplan apply | The renderer may be NetworkManager. |
Useful inspection commands include:
systemctl is-active NetworkManager
systemctl is-active systemd-networkd
ls -l /etc/netplan/
ls -l /etc/network/interfacesThe presence of Netplan files does not by itself prove which backend is active. Inspect the configuration and service state, then use one management method for the interface.
Collect the Network Values
Before changing anything, record the values required for the local network. The static address must belong to the same subnet as the gateway, and it must not already be assigned to another device.
| Setting | Example | Purpose | Where to obtain it |
|---|---|---|---|
| Interface name | ens33 | Selects the network device to configure. | ip -br address or ip link show |
| IPv4 address | 192.168.198.160 | Identifies the Ubuntu host on the local network. | Network administrator or address plan |
| Prefix or subnet mask | /24 or 255.255.255.0 | Defines the network and host portions of the address. | Network administrator or current DHCP settings |
| Default gateway | 192.168.198.2 | Router used for destinations outside the local subnet. | ip route show default or router configuration |
| DNS servers | 192.168.198.2, 1.1.1.1 | Resolve hostnames into IP addresses. | Network administrator or approved resolver service |
A subnet mask such as 255.255.255.0 has the same meaning as the CIDR prefix length /24. The prefix length is the number of network bits.
| CIDR prefix | Subnet mask | Typical private-network use |
|---|---|---|
/24 | 255.255.255.0 | Small LAN with 254 usable IPv4 host addresses |
/16 | 255.255.0.0 | Large private network |
/8 | 255.0.0.0 | Large address space, less common for a single LAN |
Check the DHCP scope on the router or DHCP server. Choose an unused address outside that pool, or create a DHCP reservation. Assigning an address that another device receives later can cause intermittent connectivity and duplicate-address conflicts.
Identify the Interface and Current Configuration
ip address show
ip -br address
ip route show
ip route show defaultLook for the interface that has a link and an existing IPv4 address. Names such as eth0, enp0s3, and ens33 are normal predictable interface names; your system may use a different name.
The default route usually looks like default via 192.168.198.2 dev ens33. This reveals both the current default gateway and the interface carrying ordinary outbound traffic.
ifconfig is a legacy inspection utility. It may not be installed on newer Ubuntu systems and is generally replaced by the ip command. If needed, it is provided by the net-tools package.
Test a Temporary Static Address
A temporary change is useful for testing whether an address and route work before editing persistent configuration. It disappears after reboot and may be replaced by the active network manager.
sudo ip address add 192.168.198.160/24 dev eth0
sudo ip route replace default via 192.168.198.2 dev eth0Replace the interface, address, prefix, and gateway with values for your network. Do not add a second conflicting address or route to a production interface. Applying these commands to the interface carrying your SSH connection can interrupt or terminate the session.
Configure a Persistent Address with ifupdown
ifupdown reads interface definitions from /etc/network/interfaces. Back up the file before editing it:
sudo cp -a /etc/network/interfaces /etc/network/interfaces.backupPreserve existing settings unless you intentionally replace them. The file may contain include or source directives that load additional configuration.
For the example network, edit the file and add or adjust the interface stanza:
auto eth0
iface eth0 inet static
address 192.168.198.160
netmask 255.255.255.0
gateway 192.168.198.2
dns-nameservers 192.168.198.2 1.1.1.1auto eth0starts the interface during normal boot.iface eth0 inet staticdeclares an IPv4 static configuration foreth0.addresssets the host's IPv4 address.netmasksets the IPv4 subnet mask. Some configurations can use a prefix instead.gatewaysets the router for the default route.dns-nameserverslists optional DNS resolvers.
The address 192.168.198.160, mask 255.255.255.0, and gateway 192.168.198.2 are examples. Substitute values appropriate to your LAN.
Apply and Verify ifupdown Settings
On a system managed by ifupdown, reactivate the interface:
sudo ifdown eth0
sudo ifup eth0Where the applicable service is used, restarting the networking service may also apply the file, but it can disrupt every managed interface. Confirm the service and configuration method before restarting it.
ip address show dev eth0
ip route show default
ping -c 4 192.168.198.2
ping -c 4 1.1.1.1
getent hosts example.com
resolvectl statusConfigure a Persistent Address with Netplan
Netplan reads YAML files under /etc/netplan/. Back up the directory or the file you will edit:
sudo cp -a /etc/netplan/01-network-manager-all.yaml /etc/netplan/01-network-manager-all.yaml.backupThe source filename may differ. List the directory first and back up the actual file you plan to modify.
Create or edit a file such as /etc/netplan/01-static-ip.yaml:
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: false
addresses:
- 192.168.198.160/24
routes:
- to: default
via: 192.168.198.2
nameservers:
addresses:
- 192.168.198.2
- 1.1.1.1This example uses systemd-networkd. On a desktop installation, use renderer: NetworkManager when that is the active backend. Replace ens33 with the actual interface name.
YAML uses indentation to define structure. Use spaces rather than tabs, keep child keys indented beneath their parent, and ensure the interface name is spelled exactly as shown by ip link show. The routes form shown above is the preferred default-route syntax on current Netplan versions. Older versions may accept a gateway property instead, so syntax can vary by installed Netplan version.
Validate and Apply Netplan
Generate backend configuration first:
sudo netplan generateWhen possible, use the recoverable test mode:
sudo netplan trynetplan try applies the configuration temporarily and can roll it back if you do not confirm it. This is safer for remote changes, but a console or out-of-band recovery path is still recommended. After confirming the result, apply the configuration normally:
sudo netplan applyValidate Address, Routing, Connectivity, and DNS
| Check | Command | Expected result | What failure suggests |
|---|---|---|---|
| Interface address | ip address show dev ens33 | Expected address with the expected prefix is present. | Wrong interface, invalid configuration, or failed activation. |
| Default route | ip route show default | Default route uses the intended gateway and interface. | Missing or incorrect gateway or route. |
| Local gateway | ping -c 4 192.168.198.2 | Replies from the local router. | Wrong subnet, gateway, interface, cable, virtual switch, or LAN isolation. |
| External IP | ping -c 4 1.1.1.1 | Replies from an external IP address. | Routing, firewall, upstream, or Internet connectivity problem. DNS is not involved. |
| Hostname resolution | getent hosts example.com | An address is returned for the hostname. | Missing, unreachable, or incorrectly selected DNS resolver. |
| Resolver state | resolvectl status | Expected DNS servers and link configuration are shown. | Resolver service or backend settings differ from the configuration. |
Test in order: inspect the address, inspect the default route, reach the local gateway, test an external IP, and then test a hostname. This sequence separates local link and routing failures from DNS failures.
Safe Change-Management Practices
- Back up configuration files before editing them.
- Write down the current address, route, DNS settings, and active backend.
- Confirm that the chosen address is unused and outside the DHCP pool, or create a DHCP reservation.
- Use console access, an out-of-band console, or
netplan trywith a rollback plan for remote systems. - Plan how to restore the old configuration before changing the interface carrying your administrative connection.
- Do not maintain competing persistent definitions for the same interface in ifupdown, Netplan, and another network manager.
Troubleshooting
Network access is lost after applying the configuration
Check the address and route from a console:
ip address show
ip route show default
ping -c 4 192.168.198.2Common causes include an incorrect address, prefix, subnet mask, gateway, route, or interface name. Restore the backup configuration if necessary and reactivate the interface using the correct backend.
The static address works, but hostnames do not resolve
First test an IP address such as 1.1.1.1. If that works, inspect DNS with resolvectl status, verify that nameservers are configured, and confirm that those resolver addresses are reachable. The active NetworkManager, systemd-networkd, or ifupdown settings may differ from the file you edited.
The network works until reboot
An ip or ifconfig command changes the running system only. Determine whether the host uses Netplan, ifupdown, or NetworkManager, then place the settings in that system's persistent configuration. Another network-management service may otherwise overwrite them during boot.
The address conflicts with another device
The address may be inside the DHCP pool, already assigned, or absent from the network's address-allocation plan. Check DHCP scope and lease records, choose an unused address outside the pool, or configure a DHCP reservation.
Netplan rejects the YAML
Run sudo netplan generate to expose parsing errors. Check spaces and indentation, verify the interface name with ip link show, and confirm that the route syntax is supported by the installed Netplan version. Use netplan try for recoverable testing.
ifdown or ifup is unavailable
The host may use Netplan, NetworkManager, or systemd-networkd, or the ifupdown package may not be installed. Identify the active backend and use its configuration and activation commands instead of combining management frameworks.
Key Points
- A static address persists only when it is represented in the active persistent network configuration.
- The address, prefix or subnet mask, and gateway must describe the same local network.
- The default route identifies the gateway used for destinations without a more specific route.
- Successful IP connectivity does not prove that DNS works; test an external IP and a hostname separately.
- Always protect remote access with a backup and a recovery plan before changing networking.