From Dynamic to Static: A Beginner’s Guide to Setting a Static IP Address on Ubuntu

  Linux, Ubuntu

Welcome, Linux newcomers! If you’ve just dived into the wonderful world of Ubuntu, you might be wondering how to get a bit more control over your network settings. One common task is switching your IP address from dynamic (DHCP) to static. Why would you want to do this? Well, a static IP address ensures that your Ubuntu machine always has the same address on your local network. This is super handy for things like:

  • Hosting a server: If you’re running a web server, game server, or any other service, a static IP makes it easy for other devices to find it.
  • Network sharing: Consistent IP addresses simplify setting up network shares and printers.
  • Remote access: If you need to SSH into your Ubuntu machine reliably, a static IP is your friend.

By default, Ubuntu (like most operating systems) uses DHCP (Dynamic Host Configuration Protocol). This means your router automatically assigns an IP address to your machine from a pool of available addresses. While convenient, this address can change, which isn’t ideal for the scenarios mentioned above.

In this guide, we’ll walk you through every step to configure a static IP address on the latest version of Ubuntu. As of May 2025, the latest LTS (Long Term Support) version is Ubuntu 24.04 LTS “Noble Numbat”, and these instructions will be tailored for it. Ubuntu typically uses Netplan for network configuration, so that’s what we’ll be focusing on.

Let’s get started!

Prerequisites

  • An Ubuntu machine (we’re using Ubuntu 24.04 LTS as our reference).
  • Access to a user account with sudo privileges (to make administrative changes).
  • Basic understanding of the command line (don’t worry, we’ll guide you!).

Step 1: Gathering Your Current Network Information

Before we can set a static IP, we need to know a few things about your current network configuration. We’ll need:

  1. Your network interface name: This is the identifier for your network card (e.g., eth0, ens33, wlp2s0).
  2. Your current IP address (if assigned by DHCP): This will give you an idea of the network range.
  3. Your gateway address: This is usually your router’s IP address.
  4. Your DNS server addresses: These servers translate domain names (like google.com) into IP addresses.

Open a terminal window. You can usually do this by pressing Ctrl+Alt+T.

First, let’s find your network interface name and current IP address.

ip addr show

You’ll see output similar to this (the specifics will vary based on your system):

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:12:34:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.105/24 brd 192.168.1.255 scope global dynamic noprefixroute ens33
       valid_lft 85678sec preferred_lft 85678sec
    inet6 fe80::20c:29ff:fe12:3456/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

In this example:

  • The interface name is ens33. Yours might be different (e.g., eth0, enp0s3). Note this down.
  • The current DHCP-assigned IP address is 192.168.1.105/24. The /24 part is the CIDR notation for the subnet mask (which usually means 255.255.255.0).

Next, let’s find your gateway address:

ip route

The output will look something like this:

default via 192.168.1.1 dev ens33 proto dhcp src 192.168.1.105 metric 100
192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.105 metric 100

The important line is the one starting with default via. Here, the gateway address is 192.168.1.1. Note this down.

Finally, let’s find your DNS server addresses. These are often the same as your gateway, or they might be provided by your ISP or a third-party service like Google DNS or Cloudflare DNS.

resolvectl status

or

cat /etc/resolv.conf

The resolvectl status command usually gives more comprehensive information, especially on modern Ubuntu systems. Look for “DNS Servers” under your active network interface.

Global
       Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
resolv.conf mode: stub

Link 2 (ens33)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 192.168.1.1
       DNS Servers: 192.168.1.1 8.8.8.8
        DNS Domain: ~.

In this example, the DNS servers are 192.168.1.1 (often your router) and 8.8.8.8 (Google’s public DNS). It’s good to have at least one, preferably two, DNS server addresses. Note these down.

Important Considerations for Choosing Your Static IP:

  • Network Range: The static IP you choose must be within the same network range as your router. In our example, the network is 192.168.1.x.
  • Avoid Conflicts: The IP address must not be already in use by another device on your network.
  • Outside DHCP Range: It’s best practice to choose an IP address that is outside the range of IPs your router assigns via DHCP. For example, if your router’s DHCP pool is 192.168.1.100 to 192.168.1.200, you could choose 192.168.1.50. You might need to check your router’s configuration page to find its DHCP range.

For this guide, let’s assume we want to set the following static IP configuration:

  • Static IP Address: 192.168.1.50
  • Subnet Mask: 24 (which translates to 255.255.255.0)
  • Gateway Address: 192.168.1.1 (from our ip route command)
  • DNS Servers: 192.168.1.1 and 8.8.8.8 (from resolvectl status)

Step 2: Locating and Editing the Netplan Configuration File

Ubuntu uses Netplan to manage network interfaces. Netplan configuration files are written in YAML format and are typically located in the /etc/netplan/ directory.

Navigate to this directory:

cd /etc/netplan

Now, list the files in this directory:

ls

You should see a YAML file, often named something like 01-network-manager-all.yaml, 50-cloud-init.yaml, or 00-installer-config.yaml. The exact name can vary.

We need to edit this file. It’s crucial to back up the original configuration file before making any changes. If something goes wrong, you can easily revert.

sudo cp your-config-file.yaml your-config-file.yaml.bak

Replace your-config-file.yaml with the actual name of your configuration file. For example, if it’s 00-installer-config.yaml:

sudo cp 00-installer-config.yaml 00-installer-config.yaml.bak

Now, open the configuration file for editing using a command-line text editor like nano.

sudo nano your-config-file.yaml

Again, replace your-config-file.yaml with the actual filename.

Step 3: Configuring the Static IP Address

You will see the current Netplan configuration. If your system is configured to use DHCP for your Ethernet interface (let’s assume it’s ens33 as per our example), it might look something like this:

YAML

# This is the network config written by 'subiquity'
network:
  ethernets:
    ens33:
      dhcp4: true
  version: 2

Or, if NetworkManager is handling things (common on desktop installs), it might look like:

YAML

network:
  version: 2
  renderer: NetworkManager

If your file mentions renderer: NetworkManager, it means NetworkManager is controlling the connections, often through a graphical interface. While you can define connections in Netplan and have NetworkManager use them, for a server or a more direct approach, you might specify renderer: networkd. For desktop users, it’s often easier to use the GUI if NetworkManager is the renderer. However, we’ll proceed with a networkd or a generic Netplan configuration that works for servers.

We need to modify this to set a static IP. Here’s how you would change the configuration for the ens33 interface to use our chosen static IP settings:

Carefully replace the existing content or modify it to look like this:

YAML

network:
  version: 2
  renderer: networkd # Or keep NetworkManager if you prefer, but networkd is common for static server configs
  ethernets:
    ens33: # Replace ens33 with YOUR actual interface name
      dhcp4: no # Disable DHCP for IPv4
      addresses:
        - 192.168.1.50/24 # Your chosen static IP and subnet mask
      routes:
        - to: default
          via: 192.168.1.1 # Your gateway address
      nameservers:
        addresses: [192.168.1.1, 8.8.8.8] # Your DNS servers

Understanding the YAML Configuration:

  • network:: This is the top-level key for Netplan configurations.
  • version: 2: Specifies the Netplan configuration format version.
  • renderer: networkd: This line tells Netplan to use systemd-networkd as the backend to manage networking. If it was NetworkManager, changes here might be overridden by NetworkManager unless configured appropriately. For server environments, networkd is common for static IPs.
  • ethernets:: Defines configurations for Ethernet interfaces.
  • ens33:: This is the name of your network interface. Make sure you use the correct interface name you found in Step 1.
  • dhcp4: no: This disables DHCP for IPv4. If you also want to disable DHCP for IPv6, you would add dhcp6: no.
  • addresses:: This section lists the static IP addresses.
    • - 192.168.1.50/24: This is your desired static IP address and the subnet mask in CIDR notation. /24 corresponds to 255.255.255.0.
  • routes:: This section defines static routes.
    • to: default: Specifies this is the default route.
    • via: 192.168.1.1: Sets your gateway address.
  • nameservers:: Configures DNS.
    • addresses: [192.168.1.1, 8.8.8.8]: Lists your primary and secondary DNS server addresses.

Important Notes on YAML:

  • Indentation is crucial! YAML uses spaces (not tabs) for indentation. The structure (nesting) of the configuration depends entirely on correct indentation. Typically, two spaces are used for each level of indentation.
  • Double-check everything: Typos in interface names, IP addresses, or keywords will cause errors.

Once you’ve made the changes, save the file and exit nano.

  • In nano, press Ctrl+X.
  • It will ask if you want to save the modified buffer. Press Y for Yes.
  • It will then confirm the file name. Press Enter.

Step 4: Applying the Netplan Configuration

Now that you’ve saved your new network configuration, you need to apply it. Netplan has a command for this.

First, it’s a good idea to test the configuration for any syntax errors:

sudo netplan try

This command will attempt to apply the configuration. If there are syntax errors, it will report them and automatically revert to the previous working configuration after a timeout (usually 120 seconds) if you don’t confirm. If the syntax is correct and the new configuration could work, it will apply it temporarily and ask you to press Enter to confirm and keep the new settings. If you lose connectivity, don’t press Enter; it will revert automatically.

If sudo netplan try reports “Configuration accepted” and you are happy with the (temporary) application, you can then make it permanent by pressing Enter.

If you are confident or if netplan try completed successfully and you confirmed, you can use the apply command:

sudo netplan apply

This command will apply the configuration permanently. If there are errors, it should tell you.

Step 5: Verifying the New Static IP Address

After applying the changes, your network connection might briefly disconnect and then reconnect. Let’s verify that your Ubuntu machine is now using the static IP address you configured.

Check your IP address again:

ip addr show dev ens33 # Replace ens33 with your interface name

You should see your new static IP address listed:

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:12:34:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.50/24 brd 192.168.1.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe12:3456/64 scope link
       valid_lft forever preferred_lft forever

Notice that dynamic is no longer present next to the IP address.

Verify your gateway (default route):

ip route

You should see your configured gateway:

default via 192.168.1.1 dev ens33 proto static
192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.50

Verify your DNS settings:

resolvectl status ens33 # Replace ens33 with your interface name

or

cat /etc/resolv.conf

You should see the DNS servers you specified in your Netplan configuration. With systemd-networkd as the renderer, /etc/resolv.conf is often a symbolic link to /run/systemd/resolve/stub-resolv.conf or /run/systemd/resolve/resolv.conf, and it should reflect the nameservers pushed by systemd-networkd.

Link 2 (ens33)
    Current Scopes: DNS
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
Current DNS Server: 192.168.1.1
       DNS Servers: 192.168.1.1 8.8.8.8
        DNS Domain: ~.

Finally, test your internet connectivity:

ping -c 4 google.com

This will send 4 packets to google.com. If you get replies, your static IP configuration is working, and you have internet access!

PING google.com (142.250.190.78) 56(84) bytes of data.
64 bytes from lga34s32-in-f14.1e100.net (142.250.190.78): icmp_seq=1 ttl=118 time=10.5 ms
64 bytes from lga34s32-in-f14.1e100.net (142.250.190.78): icmp_seq=2 ttl=118 time=11.2 ms
64 bytes from lga34s32-in-f14.1e100.net (142.250.190.78): icmp_seq=3 ttl=118 time=10.8 ms
64 bytes from lga34s32-in-f14.1e100.net (142.250.190.78): icmp_seq=4 ttl=118 time=12.1 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 10.528/11.152/12.102/0.585 ms

Troubleshooting Tips

  • No Network Connection:
    • Double-check your Netplan YAML file for typos and incorrect indentation. YAML is very sensitive.
    • Ensure the IP address, gateway, and DNS server addresses are correct for your network.
    • Make sure you used the correct network interface name.
    • Revert to your backup configuration if needed: Bashsudo cp /etc/netplan/your-config-file.yaml.bak /etc/netplan/your-config-file.yaml sudo netplan apply
  • IP Address Conflict: If another device on your network is using the static IP you chose, you’ll have issues. Try a different IP address (outside your router’s DHCP range).
  • netplan apply Fails: Read the error messages carefully. They often point to the line in the YAML file that has a problem.
  • Renderer Issues: If your system uses NetworkManager as the renderer in /etc/netplan/your-config-file.yaml, and you’ve edited the file directly as shown for networkd, NetworkManager might override these settings or not apply them as expected.
    • You can tell Netplan to let NetworkManager manage the connection and then configure the static IP through NetworkManager’s tools (either GUI or nmcli).
    • Alternatively, if you want Netplan to control it via systemd-networkd, ensure renderer: networkd is set. Be aware this might stop NetworkManager from managing that specific interface. For servers, networkd is generally preferred for static configurations. For desktops, many users prefer using NetworkManager.
    If you want to use NetworkManager (often the default on Ubuntu Desktop), you can also configure a static IP using the GUI:
    1. Click on the network icon in the top bar.
    2. Go to “Wired Settings” (or “Wi-Fi Settings”).
    3. Click the cog icon next to your active connection.
    4. Go to the “IPv4” tab.
    5. Change “Method” from “Automatic (DHCP)” to “Manual”.
    6. Enter your desired IP Address, Netmask (e.g., 255.255.255.0), and Gateway.
    7. Add your DNS server(s) in the “DNS” field, separated by commas.
    8. Click “Apply”. You might need to toggle the connection off and on.
    • [Screenshot of Ubuntu Network Settings GUI showing IPv4 Manual configuration.]

Conclusion

Congratulations! You’ve successfully switched your Ubuntu machine from a dynamic DHCP IP address to a static IP address using Netplan. This will provide a stable and predictable network identity for your machine, making it much easier to manage for server duties or consistent remote access.

Remember, the key steps are:

  1. Gathering your current network information.
  2. Carefully editing the Netplan YAML configuration file.
  3. Applying and verifying the changes.

While working with configuration files in the terminal might seem daunting at first, it’s a powerful way to manage your Linux system. With practice, you’ll become more comfortable and confident. Welcome to the world of Linux networking!

LEAVE A COMMENT