In typical network setups, your router uses DHCP (Dynamic Host Configuration Protocol) to automatically assign IP addresses to devices. While convenient for everyday use, this dynamic allocation can be problematic when you need a consistent IP address for your Ubuntu server. Setting a static IP address ensures your server retains the same IP every time it connects to the network.
This guide will walk you through two straightforward methods to configure a static IP address on your Ubuntu server. We’ll cover both the command-line interface (CLI) for those who prefer a hands-on approach and the graphical user interface (GUI) for ease of use.
Static IP addresses are crucial in various server applications, including:
- Port Forwarding: Essential for directing network traffic to specific services running on your server, like web servers or game servers.
- Server Hosting: When hosting services such as FTP servers, web servers (like Apache or Nginx), media servers (like Plex or Emby), a static IP ensures consistent accessibility for users.
- Remote Access: For reliable SSH access or remote desktop connections to your Ubuntu server.
Prerequisites:
Before you begin, ensure you have the following:
- An Ubuntu server installation (GUI is recommended for the GUI method).
sudo
privileges to modify system network configurations.- Basic understanding of network concepts like IP addresses, subnet masks, and gateways.
Method 1: Setting a Static IP Using the Command Line in Ubuntu Server
The command line offers a powerful and direct way to configure your Ubuntu server’s network settings. This method is particularly useful for server environments where a GUI might not be available.
Step 1: Access the Terminal
Open your terminal. You can usually do this via SSH or directly on the server console.
Step 2: Gather Current Network Information
Before making changes, it’s vital to understand your current network configuration. This information will be used to configure the static IP correctly.
Use the ip a
command to display network adapter details and IP information.
ip a
The output will list your network interfaces. Look for the active interface connected to your network. Common interface names are eth0
, eth1
, enp0s3
, or ens33
.
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:b1:fc:0a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 85794sec preferred_lft 85794sec
inet6 fe80::a00:27ff:feb1:fc0a/64 scope link
valid_lft forever preferred_lft forever
In this example, eth0
is the network interface, and the current dynamic IP address is 192.168.1.100
with a /24
subnet mask (which is equivalent to a subnet mask of 255.255.255.0
).
Key pieces of information to note:
- Network Interface Name: (e.g.,
eth0
,ens33
). - Current IP Address: (e.g.,
192.168.1.100
). - Subnet Mask: (e.g.,
/24
or255.255.255.0
).
You’ll also need to determine your gateway and DNS server addresses. Often, the gateway is your router’s IP address. You can usually find this information in your router’s configuration or by using the route -n
command:
route -n
Look for the “Gateway” entry associated with the 0.0.0.0
destination. This is typically your default gateway.
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
In this case, the gateway is 192.168.1.1
. For DNS servers, you can often use public DNS servers like Google’s (8.8.8.8
and 8.8.4.4
) or Cloudflare’s (1.1.1.1
and 1.0.0.1
).
Step 3: Configure Static IP in Netplan
Ubuntu uses Netplan as its default network configuration tool. Netplan uses YAML configuration files located in /etc/netplan/
.
-
Navigate to the Netplan directory:
cd /etc/netplan/
-
List files in the directory:
ls
You’ll likely see a
.yaml
file, such as01-netcfg.yaml
or similar. If no file exists, you can create one. -
Open the Netplan configuration file for editing:
Use
sudo nano
(or your preferred text editor) to edit the YAML file. Replace01-netcfg.yaml
with the actual filename if it’s different.sudo nano 01-netcfg.yaml
-
Edit the configuration:
The default Netplan configuration might look something like this:
network: version: 2 renderer: networkd ethernets: eth0: dhcp4: yes
Modify this file to configure a static IP. Here’s an example configuration:
network: version: 2 renderer: networkd ethernets: eth0: dhcp4: no addresses: [192.168.1.150/24] # Replace with your desired static IP and subnet gateway4: 192.168.1.1 # Replace with your gateway IP nameservers: addresses: [8.8.8.8, 8.8.4.4] # Replace with your DNS servers
Explanation of the configuration:
network:
: Top-level section for network configuration.version: 2
: Netplan configuration version.renderer: networkd
: Specifiesnetworkd
as the network management renderer.ethernets:
: Section for Ethernet interface configurations.eth0:
: Replaceeth0
with your network interface name identified in Step 2.dhcp4: no
: Disables DHCP for IPv4, ensuring a static IP.addresses: [192.168.1.150/24]
: Sets the static IP address to192.168.1.150
with a/24
subnet mask. Replace192.168.1.150
with your chosen static IP address within your network’s range. Choose an IP address that is outside the DHCP range of your router to avoid conflicts.gateway4: 192.168.1.1
: Specifies the IPv4 gateway address. Replace192.168.1.1
with your router’s IP address.nameservers:
: Section for DNS server configuration.addresses: [8.8.8.8, 8.8.4.4]
: Sets the DNS server addresses. Here, we are using Google’s public DNS servers. Replace these with your preferred DNS servers if needed.
Important: Ensure correct YAML syntax, especially indentation. Spaces are significant in YAML.
-
Save the file and exit the editor (in
nano
, pressCtrl+X
, thenY
, thenEnter
).
Step 4: Apply and Test the Netplan Configuration
-
Apply the Netplan configuration:
sudo netplan apply
This command applies the new network settings. If there are syntax errors in your YAML file,
netplan apply
will report them. -
Verify the static IP:
Use the
ip a
command again to check if the static IP has been correctly applied to your network interface.ip a
You should see your configured static IP address listed for your network interface.
-
Test network connectivity:
Ping a public website (like Google) to ensure your server can connect to the internet:
ping google.com
If the ping is successful, your static IP configuration is working correctly.
Manually setting a static IP using Ubuntu Desktop.
Method 2: Setting a Static IP Using the Ubuntu GUI
For Ubuntu Desktop users, the GUI provides a user-friendly way to configure a static IP address.
-
Open System Settings:
Click on the system menu (usually in the top right corner) and select “Settings”.
-
Navigate to Network Settings:
In the Settings window, find and click on either “Network” (for wired connections) or “Wi-Fi” (for wireless connections) depending on your network interface.
-
Access Interface Settings:
Identify your active network interface (e.g., Wired, Wi-Fi). Click the “gear” icon next to the interface name to open its settings.
-
Configure IPv4 Settings:
In the interface settings window, go to the “IPv4” tab.
- Select “Manual”: Change the “IPv4 Method” from “Automatic (DHCP)” to “Manual”.
- Enter Static IP Details:
- Address: Enter your desired static IP address. Make sure it’s within your network’s range and outside the DHCP range of your router.
- Netmask: Enter your network’s subnet mask (e.g.,
255.255.255.0
). - Gateway: Enter your router’s IP address (gateway).
- DNS Servers: In the “DNS” section, enter your preferred DNS server addresses, separated by commas. You can use public DNS servers like
8.8.8.8, 8.8.4.4
.
-
Apply Changes:
Click the “Apply” button in the bottom right corner of the network settings window.
-
Restart Network (Optional but Recommended):
Sometimes, restarting the network connection can ensure the changes are applied correctly. You can toggle the network interface off and then back on in the Network settings window, or simply reboot your system.
-
Verify Static IP:
Open a terminal and use the
ip a
command to verify that the static IP address has been successfully configured.
ip a
You can also check your internet connectivity by opening a web browser or using the ping google.com
command in the terminal.
Static IP address successfully configured via GUI.
Conclusion
This guide has shown you two effective methods to configure a static IP address on your Ubuntu server: using the command line with Netplan and using the graphical user interface. Choosing the right method depends on your environment and preference. The command-line method is ideal for server environments and those comfortable with the terminal, while the GUI method offers a simpler approach for desktop users.
Setting a static IP is a fundamental step in server administration, ensuring reliable access and service availability. By following these steps, you can confidently configure your Ubuntu server with a static IP address that meets your networking needs.