Linux online course

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 generationPrimary configuration mechanismConfiguration locationSafe apply commandNotes
Older Ubuntu or explicit ifupdown installationifupdown/etc/network/interfacesifdown and ifupTaking down an active interface can terminate SSH.
Modern Ubuntu ServerNetplan with systemd-networkd/etc/netplan/*.yamlnetplan try, then netplan applyUse YAML with exact indentation.
Modern Ubuntu DesktopNetplan with NetworkManager/etc/netplan/*.yaml or NetworkManager-managed settingsnetplan try, then netplan applyThe 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/interfaces

The 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.

SettingExamplePurposeWhere to obtain it
Interface nameens33Selects the network device to configure.ip -br address or ip link show
IPv4 address192.168.198.160Identifies the Ubuntu host on the local network.Network administrator or address plan
Prefix or subnet mask/24 or 255.255.255.0Defines the network and host portions of the address.Network administrator or current DHCP settings
Default gateway192.168.198.2Router used for destinations outside the local subnet.ip route show default or router configuration
DNS servers192.168.198.2, 1.1.1.1Resolve 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 prefixSubnet maskTypical private-network use
/24255.255.255.0Small LAN with 254 usable IPv4 host addresses
/16255.255.0.0Large private network
/8255.0.0.0Large 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 default

Look 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 eth0

Replace 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.backup

Preserve 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.1
  • auto eth0 starts the interface during normal boot.
  • iface eth0 inet static declares an IPv4 static configuration for eth0.
  • address sets the host's IPv4 address.
  • netmask sets the IPv4 subnet mask. Some configurations can use a prefix instead.
  • gateway sets the router for the default route.
  • dns-nameservers lists 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 eth0

Where 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 status

Configure 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.backup

The 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.1

This 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 generate

When possible, use the recoverable test mode:

sudo netplan try

netplan 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 apply

Validate Address, Routing, Connectivity, and DNS

CheckCommandExpected resultWhat failure suggests
Interface addressip address show dev ens33Expected address with the expected prefix is present.Wrong interface, invalid configuration, or failed activation.
Default routeip route show defaultDefault route uses the intended gateway and interface.Missing or incorrect gateway or route.
Local gatewayping -c 4 192.168.198.2Replies from the local router.Wrong subnet, gateway, interface, cable, virtual switch, or LAN isolation.
External IPping -c 4 1.1.1.1Replies from an external IP address.Routing, firewall, upstream, or Internet connectivity problem. DNS is not involved.
Hostname resolutiongetent hosts example.comAn address is returned for the hostname.Missing, unreachable, or incorrectly selected DNS resolver.
Resolver stateresolvectl statusExpected 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 try with 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.2

Common 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.