• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

TinyGrab

Your Trusted Source for Tech, Finance & Brand Advice

  • Personal Finance
  • Tech & Social
  • Brands
  • Terms of Use
  • Privacy Policy
  • Get In Touch
  • About Us
Home » How to set a static IP address on Ubuntu Server 22.04?

How to set a static IP address on Ubuntu Server 22.04?

March 27, 2025 by TinyGrab Team Leave a Comment

Table of Contents

Toggle
  • Configuring a Static IP on Ubuntu Server 22.04: A Deep Dive
    • Step-by-Step Guide to Static IP Configuration
    • Frequently Asked Questions (FAQs)
      • 1. What is Netplan and why is it used in Ubuntu Server?
      • 2. How do I find my gateway IP address?
      • 3. What DNS servers should I use?
      • 4. What is a subnet mask and why is it important?
      • 5. How do I revert back to DHCP after setting a static IP?
      • 6. What if I get a “Cannot read Netplan configuration” error?
      • 7. My server is not connecting to the internet after setting a static IP. What should I do?
      • 8. How do I configure a static IPv6 address?
      • 9. Can I use NetworkManager instead of systemd-networkd with Netplan?
      • 10. How do I set a static IP address using the command line without editing YAML files?
      • 11. What happens if there are multiple Netplan configuration files?
      • 12. How do I configure multiple IP addresses on a single interface?

Configuring a Static IP on Ubuntu Server 22.04: A Deep Dive

Setting a static IP address on your Ubuntu Server 22.04 instance is crucial for maintaining consistent network connectivity, especially when hosting services like websites, databases, or game servers. The process involves modifying the network configuration file to specify the IP address, gateway, DNS servers, and subnet mask. We will use Netplan, the network configuration tool used by default in Ubuntu 22.04, to achieve this.

Step-by-Step Guide to Static IP Configuration

Here’s a detailed breakdown of the procedure:

  1. Identify Your Network Interface: First, determine the name of the network interface you want to configure. Use the command ip addr or ifconfig -a (if net-tools is installed). Look for an interface that’s “UP” and has a MAC address; common names are eth0, ens3, or enp0s3. Take careful note of this name, as you’ll need it in the next steps.

  2. Locate the Netplan Configuration File: Netplan configuration files are typically located in /etc/netplan/. List the contents of the directory using ls /etc/netplan/. You will likely find a file named something like 01-network-manager-all.yaml or 50-cloud-init.yaml. The specific filename might vary.

  3. Back Up the Existing Configuration: Before making any changes, it’s always a good idea to back up the original configuration file. Use the sudo cp command: sudo cp /etc/netplan/YOUR_FILE.yaml /etc/netplan/YOUR_FILE.yaml.bak (Replace YOUR_FILE.yaml with the actual filename). This allows you to easily revert to the original configuration if something goes wrong.

  4. Edit the Netplan Configuration File: Open the Netplan configuration file using a text editor like nano or vim: sudo nano /etc/netplan/YOUR_FILE.yaml. You’ll need root privileges. The file will likely contain YAML code. The structure will look something like this:

network:   version: 2   renderer: networkd   ethernets:     ens3:  # Replace with your interface name       dhcp4: yes 
  1. Modify the Configuration for Static IP: Replace the existing content with the following, adapting it to your specific network details:
network:   version: 2   renderer: networkd   ethernets:     ens3: # Replace with your interface name       dhcp4: no       addresses: [192.168.1.10/24] # Replace with your desired IP address and subnet mask       gateway4: 192.168.1.1 # Replace with your gateway IP address       nameservers:           addresses: [8.8.8.8, 8.8.4.4] # Replace with your desired DNS server IP addresses 
  • ens3: Replace this with the actual name of your network interface identified in Step 1.
  • dhcp4: no: Disables DHCP for IPv4 on this interface.
  • addresses: [192.168.1.10/24]: Specifies the static IP address and subnet mask. /24 represents a subnet mask of 255.255.255.0. Change the IP address to one that is available within your network’s IP range and not already in use.
  • gateway4: 192.168.1.1: Specifies the IP address of your network’s gateway (usually your router).
  • nameservers: addresses: [8.8.8.8, 8.8.4.4]: Specifies the IP addresses of your DNS servers. In this example, we’re using Google’s public DNS servers, but you can replace them with your preferred DNS servers or your ISP’s DNS servers.
  1. Validate the YAML Syntax: Ensure the YAML syntax is correct. Incorrect syntax can cause network configuration to fail. You can use a YAML validator online or run sudo netplan try (mentioned later) to check the syntax. Pay close attention to indentation. YAML is sensitive to indentation; use spaces, not tabs.

  2. Apply the Changes: Save the changes to the file and then apply the new configuration using the command: sudo netplan apply. This command will attempt to apply the new configuration.

  3. Troubleshooting with netplan try: If you encounter issues, use sudo netplan try. This command applies the new configuration in a temporary way. If the configuration fails, it will automatically revert to the previous working configuration after a timeout period (usually 2 minutes), preventing you from losing network connectivity. You’ll also see error messages that can help you diagnose the problem.

  4. Verify the Configuration: After applying the changes, verify that the static IP address has been correctly assigned using the command ip addr or ifconfig -a. Check that the IP address, subnet mask, gateway, and DNS servers are configured as expected.

  5. Reboot (Optional): In some cases, a reboot might be necessary for the changes to fully take effect. However, netplan apply should usually be sufficient. If you encounter persistent issues, try rebooting the server with sudo reboot.

Frequently Asked Questions (FAQs)

1. What is Netplan and why is it used in Ubuntu Server?

Netplan is a network configuration utility introduced in Ubuntu 17.10 and later versions, including Ubuntu Server 22.04. It uses YAML configuration files to define network interfaces and their settings. Netplan acts as a front-end, generating the necessary configuration files for back-end network management daemons like NetworkManager or systemd-networkd. It simplifies network configuration by providing a consistent and declarative approach.

2. How do I find my gateway IP address?

The gateway IP address is usually the IP address of your router. You can often find it by checking your router’s configuration page (typically accessed through a web browser using an address like 192.168.1.1 or 192.168.0.1). Alternatively, you can use the command ip route on a system within the same network. The line starting with “default via” will show the gateway IP address.

3. What DNS servers should I use?

You can use your ISP’s DNS servers, public DNS servers like Google DNS (8.8.8.8 and 8.8.4.4) or Cloudflare DNS (1.1.1.1 and 1.0.0.1), or a private DNS server on your network. Using a public DNS server can often improve browsing speed and reliability.

4. What is a subnet mask and why is it important?

The subnet mask defines the network portion of an IP address. It’s used to determine which devices are on the same network segment. A common subnet mask is 255.255.255.0 (represented as /24), which means that the first 24 bits of the IP address define the network, and the remaining 8 bits define the host within that network. An incorrect subnet mask can prevent devices from communicating with each other.

5. How do I revert back to DHCP after setting a static IP?

To revert back to DHCP, edit the Netplan configuration file and change dhcp4: no to dhcp4: yes. Remove the lines for addresses, gateway4, and nameservers. Then, apply the changes using sudo netplan apply.

6. What if I get a “Cannot read Netplan configuration” error?

This error usually indicates a syntax error in your Netplan configuration file. Double-check the YAML syntax, especially the indentation. Use sudo netplan try to identify the specific error.

7. My server is not connecting to the internet after setting a static IP. What should I do?

First, verify that you’ve configured the correct IP address, subnet mask, gateway, and DNS servers. Check your network connectivity by pinging the gateway IP address (ping YOUR_GATEWAY_IP) and a public DNS server (ping 8.8.8.8). If you can ping the gateway but not the DNS server, there might be an issue with your DNS configuration. If you can’t ping the gateway, there might be a problem with your IP address, subnet mask, or gateway configuration.

8. How do I configure a static IPv6 address?

To configure a static IPv6 address, add an addresses entry under the ethernets section with the IPv6 address and prefix length. Also, specify the IPv6 gateway using gateway6. For example:

network:   version: 2   renderer: networkd   ethernets:     ens3:       dhcp4: no       dhcp6: no       addresses: [2001:db8::1/64]       gateway6: 2001:db8::1       nameservers:           addresses: [2001:4860:4860::8888, 2001:4860:4860::8844] 

9. Can I use NetworkManager instead of systemd-networkd with Netplan?

Yes, you can specify renderer: NetworkManager in your Netplan configuration file. However, systemd-networkd is the default renderer in Ubuntu Server 22.04 and is generally recommended for server environments. NetworkManager is more commonly used in desktop environments.

10. How do I set a static IP address using the command line without editing YAML files?

While editing the YAML file is the standard Netplan approach, you can use nmcli (NetworkManager Command Line Interface) if NetworkManager is used as the renderer. However, for server environments and consistency, modifying the Netplan YAML file is preferred.

11. What happens if there are multiple Netplan configuration files?

Netplan reads all the .yaml files in the /etc/netplan/ directory. The files are processed in lexicographical order based on their filenames. If there are conflicting configurations, the settings in the file with the later lexicographical order will take precedence. It’s generally best to have only one Netplan configuration file to avoid confusion.

12. How do I configure multiple IP addresses on a single interface?

You can configure multiple IP addresses by adding multiple entries in the addresses list under the ethernets section. For example:

network:   version: 2   renderer: networkd   ethernets:     ens3:       dhcp4: no       addresses: [192.168.1.10/24, 192.168.1.11/24]       gateway4: 192.168.1.1       nameservers:           addresses: [8.8.8.8, 8.8.4.4] 

Remember to replace the example IP addresses with your desired addresses. Ensure that these addresses are within the same subnet and don’t conflict with other devices on your network.

Filed Under: Tech & Social

Previous Post: « Are Emeralds More Expensive Than Diamonds?
Next Post: How do you say “Google” in French? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

NICE TO MEET YOU!

Welcome to TinyGrab! We are your trusted source of information, providing frequently asked questions (FAQs), guides, and helpful tips about technology, finance, and popular US brands. Learn more.

Copyright © 2025 · Tiny Grab