Setting a Static IP in Ubuntu – A Comprehensive Tutorial for Developers
As a developer, having a static IP address assigned to your Ubuntu machine offers some key benefits:
- Consistent access: You can rely on the same IP for connecting to local development servers or infrastructure.
- Remote access: Set up VPNs, port forwarding, etc. to access devices outside your network.
- Portability: Move your device across networks without needing to adjust IP-dependent configs.
While DHCP is more convenient, static IPs give your Ubuntu devices a consistent identity on the network. This allows you to optimize connectivity for coding, testing, and deployment.
In this comprehensive guide, we will cover all the concepts around static IP assignment in Ubuntu.
IP Addressing Refresher
Before learning how to configure a static IP, let‘s review some core concepts around IP addressing and subnetting.
IP Addresses
Every device connected to a TCP/IP network requires an IP address configured to identify it and enable routing of traffic to/from the device.
An IP address like 192.168.1.105
consists of two parts:
- Network ID: The first part of the IP address, identifying the network that the device belongs to. In this example,
192.168.1
. - Host ID: The remainder of the address, identifying the specific device on the network. Here,
105
.
Subnet Masks
Subnet masks help distinguish the network ID from the host ID.
For example, the subnet mask 255.255.255.0
means the first 3 octets represent the network ID. So in our example IP, the network ID is 192.168.1
, with 105
being the host ID.
Subnet masks convert to CIDR notation as well. Our example mask corresponds to /24
, meaning the first 24 bits are the network identifier.
DHCP and Static IPs
By default, most home routers will hand out IP addresses automatically using DHCP (Dynamic Host Configuration Protocol). The router‘s DHCP server manages these dynamic IP assignments.
With a static IP, you manually specify an address and skip the dynamic assignment process. This locks your device to a specific identifier on the network.
Now that we‘ve reviewed some core concepts, let‘s look at practically configuring a static IP on Ubuntu.
Prerequisites
Before setting a static IP on Ubuntu, you will need:
- Ubuntu 18.04 or newer (for netplan support)
- An available IP address outside your router‘s DHCP allocation range
- Your network‘s subnet mask
- Your router/gateway IP address
Older Ubuntu versions used ifconfig
for network configuration which had different syntax. We will focus on netplan here which is the new standard.
Method 1: Configuring a Static IP with Netplan (Command Line)
netplan is a cross-distribution command line network configuration utility introduced in Ubuntu 17.10. It provides consistent network config across various Linux environments.
netplan configuration formats are backend-agnostic and translated into whatever format the renderer tool requires. The default renderer on Ubuntu is systemd-networkd.
Let‘s look at using netplan to set a static IP on Ubuntu Server or Desktop.
1. Check the Current IP Address
First identify your current dynamic IP address info:
$ ip addr show eth0
Make note of the interface name, IP address, subnet mask, and default gateway details.
2. Create a Netplan YAML File
By convention, netplan configuration files are saved under /etc/netplan
with a .yaml
extension.
Create a file such as 01-static-ip.yaml
:
sudo nano /etc/netplan/01-static-ip.yaml
3. Define the Netplan Config
Start by declaring the netplan version and renderer:
network:
version: 2
renderer: networkd
The renderer
tells netplan which back-end tool to use to apply the configurations. Ubuntu uses systemd-networkd by default.
Then specify your network interface:
ethernets:
eth0:
Next disable DHCP and define your static IP address and subnet mask:
dhcp4: no
addresses:
- 192.168.1.25/24
Specify your network‘s gateway IP address:
gateway4: 192.168.1.1
And add DNS nameservers if needed:
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
4. Apply the Netplan Config
First test that your YAML config file is valid:
sudo netplan try
If it is valid, go ahead and apply it:
sudo netplan apply
This will configure your NIC with the static IP address details from the YAML file.
Verify your IP address is now static with ip addr show
.
Method 2: Using Ubuntu Desktop Network Settings
For Ubuntu desktop, an alternative method is to set the static IP address directly in the GUI network settings:
- Go to Settings -> Network
- Select your network connection
- Click the gear to edit connection details
- Under the IPv4 tab, change the method to Manual
- Enter your desired static IP address, subnet mask, and default gateway
- Click Apply
The settings will override any DHCP IP assignment.
However, this approach only modifies the /etc/NetworkManager/system-connections/
files temporarily. Rebooting will reset it back to DHCP unless the connection is re-saved after setting static.
So for Ubuntu desktop, using netplan configs is still the most robust approach.
Netplan vs Ifconfig for Network Configuration
Prior to netplan, ifconfig was commonly used to view and adjust network interfaces and IP assignment in Ubuntu and other Linux distributions.
However, ifconfig has some drawbacks compared to netplan:
- No persistent config: Changes made via ifconfig are not saved on reboot
- Limited capabilities: Cannot configure many modern settings around VLANs, bonds, bridges, etc
- Non-standardized: Differs significantly across distributions
Netplan was introduced to address these gaps by providing durable, distribution-agnostic network configuration.
So while legacy ifconfig commands may still work, netplan is generally preferred for consistency and reliability of static IP assignment.
DevOps Use Cases for Static IPs
As highlighted earlier, static IPs offer key benefits for developers when working with local tooling and infrastructure:
Local Development Environments
When operating developer environments like JavaScript/Python/Ruby stacks, databases, caching layers, etc locally, static IPs provide:
- Reliable access to containers, VM IPs that you depend on
- Portability across networks
- Time savings from not needing to adjust config files
Here is an example docker-compose setup using static IPs:
version: ‘3‘
services:
cache:
image: redis
networks:
static-network:
ipv4_address: 172.20.128.2
db:
image: postgres
networks:
static-network:
ipv4_address: 172.20.128.3
app:
build: .
networks:
static-network:
ipv4_address: 172.20.128.4
...
networks:
static-network:
driver: bridge
ipam:
config:
- subnet: 172.20.128.0/24
Remote Accessibility
Static home/office IPs also make it easier to get remote access for:
- Web dev testing from mobile devices
- Debugging infrastructure through VPNs
- Operating headless Raspberry Pis
Rather than dealing with dynamic DNS services to handle shifting home IPs, a static solves the problem.
Avoiding IP Conflicts
With proliferations of devices on modern home networks – computers, mobile phones, TVs, assistants, IoT devices – IP conflicts can occur causing hard-to-diagnose network issues.
Locking critical infrastructure to static IPs protects availability and removes headaches.
Network Security Compliance
For highly secured environments, static IP assignment may be required as part of compliance regulations or to tighten control.
This prevents unwanted devices being inadvertently granted access into sensitive network segments.
Common Problems When Setting Static IPs
While static IPs provide benefits in many network scenarios, you may also face potential pitfalls, such as:
Running Out of DHCP IP Addresses
If you statically allocate too many IPs, you can prematurely exhaust the DHCP IP range. Most home routers provide DHCP pools of 100-200 addresses by default.
Carefully plan subnets with DHCP vs static device ratios when allocating larger blocks.
Subnetting Misconfigurations
Choosing IP addresses outside of the proper subnet range for your network can cause loss of connectivity.
Double check your gateway/router and subnet mask combinations match when picking a static IP.
Duplicate IP Assignment
Accidentally assigning a static IP already allocated by DHCP will create an IP conflict leading to strange behavior.
Scan your DHCP reservations before selecting static IP addresses.
DNS Issues
While static IPs provide network layer benefits, don‘t forget to optimize DNS as well for application access:
- Use custom local DNS domains for development environments
- Configure DNS search suffixes for lab domains
- Enable DNS host/service name resolution as needed
Tightly integrating both IP and DNS configuration ensures reliability all the way up the stack.
Closing Recommendations When Setting Static IPs
Here are some final tips for effectively leveraging static IPs:
- Audit your network topology before carving out static IP segments
- Document all static assignments in an IP address management plan
- Set static IPs outside DHCP pools with a sufficient buffer
- Use hostnames mapped in your local DNS server for ease of maintenance
- Implement secondary IP failover if higher resiliency is needed
I hope this guide gives you a very thorough overview of working with static IPs on Ubuntu! Let me know if you have any other questions.