Set Static IP Address on Ubuntu
This comprehensive guide explains how to configure a static IP address on Ubuntu using the netplan
configuration system. Setting a static IP is essential for servers, network devices, or any system that requires a consistent network address for reliable connectivity.
Overview
Static IP addresses provide several advantages over dynamic IP assignment:
- Consistent Network Identity: Same IP address after reboots and network changes
- Server Applications: Essential for hosting services that clients need to find
- Network Management: Easier to manage firewall rules and network policies
- Remote Access: Reliable SSH and remote desktop connections
- Port Forwarding: Stable configuration for router port forwarding rules
Prerequisites
- Ubuntu system with network connectivity
- Administrative (sudo) privileges
- Basic understanding of network concepts (IP, subnet, gateway)
- Access to your network's configuration details
Understanding Netplan
Ubuntu uses netplan as the default network configuration system since Ubuntu 18.04. Netplan:
- Uses YAML configuration files for network settings
- Supports both NetworkManager and systemd-networkd as backends
- Provides a unified configuration approach across Ubuntu systems
- Validates configuration before applying changes
1. Gather Network Information
Before configuring static IP, collect necessary network information:
1.1 Identify Network Interface
# List all network interfaces
ip link show
# Alternative method
ls /sys/class/net/
Common interface names:
- Ethernet:
eth0
,enp0s3
,enp5s0
- Wireless:
wlan0
,wlp2s0
- Virtual:
vboxnet0
,docker0
1.2 Check Current Network Configuration
# Display current IP configuration
ip addr show
# Show routing table
ip route show
# Check DNS configuration
systemd-resolve --status
1.3 Identify Network Details
Gather the following information:
- Current IP Address: Your current dynamic IP
- Subnet Mask: Typically
/24
(255.255.255.0) - Gateway IP: Router's IP address
- DNS Servers: Primary and secondary DNS servers
# Find gateway (router IP)
ip route | grep default
# Check DNS servers
cat /etc/resolv.conf
2. Locate Netplan Configuration
2.1 Find Configuration Files
# Navigate to netplan directory
cd /etc/netplan/
# List configuration files
ls -la
Common filenames:
01-netcfg.yaml
00-installer-config.yaml
50-cloud-init.yaml
2.2 Backup Existing Configuration
# Create backup of current configuration
sudo cp /etc/netplan/*.yaml /etc/netplan/backup-$(date +%Y%m%d).yaml
# Alternative: backup entire directory
sudo cp -r /etc/netplan /etc/netplan-backup-$(date +%Y%m%d)
Always backup your network configuration before making changes. A misconfigured network can render your system inaccessible remotely.
3. Configure Static IP
3.1 Edit Configuration File
Open the netplan configuration file:
# Replace <filename> with your actual configuration file
sudo nano /etc/netplan/<filename>.yaml
# Example for common filename
sudo nano /etc/netplan/01-netcfg.yaml
3.2 Basic Static IP Configuration
Replace the file contents with a static IP configuration:
network:
version: 2
renderer: networkd
ethernets:
enp5s0: # Replace with your interface name
dhcp4: no
dhcp6: no
addresses: [192.168.1.100/24] # Static IP with subnet mask
routes:
- to: default
via: 192.168.1.1 # Gateway (router) IP
nameservers:
addresses: [8.8.8.8, 8.8.4.4] # DNS servers
3.3 Advanced Configuration Example
For more complex network setups:
network:
version: 2
renderer: networkd
ethernets:
enp5s0:
dhcp4: no
dhcp6: no
addresses:
- 192.168.1.100/24
- 10.0.0.100/16 # Additional IP address
routes:
- to: default
via: 192.168.1.1
- to: 10.0.0.0/16
via: 192.168.1.254 # Specific route
nameservers:
addresses: [8.8.8.8, 8.8.4.4, 1.1.1.1]
search: [local.domain, company.com] # DNS search domains
optional: true # Don't wait for this interface during boot
3.4 Configuration Parameters Explained
Parameter | Description | Example |
---|---|---|
dhcp4: no | Disable IPv4 DHCP | Required for static IP |
dhcp6: no | Disable IPv6 DHCP | Optional, recommended |
addresses | Static IP addresses | [192.168.1.100/24] |
routes | Network routing rules | Gateway and specific routes |
nameservers | DNS server configuration | Public or local DNS servers |
optional | Interface is optional for boot | true or false |
4. Validate and Apply Configuration
4.1 Validate Configuration Syntax
# Check YAML syntax and netplan configuration
sudo netplan try
# Alternative: generate configuration without applying
sudo netplan generate
The netplan try
command:
- Applies configuration temporarily
- Automatically reverts after 120 seconds if not confirmed
- Provides safety against network lockout
4.2 Apply Configuration Permanently
# Apply the configuration permanently
sudo netplan apply
If using netplan try
, you'll be prompted to confirm:
Do you want to keep these settings?
Press ENTER before the timeout to accept the new configuration
Changes will revert in 120 seconds
Press ENTER to confirm and make changes permanent.
5. Verify Configuration
5.1 Check IP Address Assignment
# Display current IP addresses
ip addr show
# Check specific interface
ip addr show enp5s0
Expected output should show your static IP:
2: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP
inet 192.168.1.100/24 brd 192.168.1.255 scope global enp5s0
5.2 Test Network Connectivity
# Test gateway connectivity
ping -c 4 192.168.1.1
# Test internet connectivity
ping -c 4 8.8.8.8
# Test DNS resolution
ping -c 4 google.com
# Check routing table
ip route show
5.3 Verify DNS Configuration
# Check DNS resolution
nslookup google.com
# Alternative DNS test
dig google.com
# Check systemd-resolved status
systemd-resolve --status
6. Troubleshooting
6.1 Common Issues and Solutions
Configuration Not Applied
# Check netplan status
sudo netplan --debug apply
# Check systemd-networkd status
sudo systemctl status systemd-networkd
# Restart networking service
sudo systemctl restart systemd-networkd
YAML Syntax Errors
# Validate YAML syntax
sudo netplan try
# Check for common issues:
# - Incorrect indentation (use spaces, not tabs)
# - Missing colons after keys
# - Improper list formatting
Network Interface Not Found
# List all available interfaces
ip link show
# Check if interface is enabled
sudo ip link set enp5s0 up
# Check kernel messages for hardware issues
dmesg | grep -i network
IP Address Conflicts
# Scan network for IP conflicts
sudo nmap -sn 192.168.1.0/24
# Check ARP table for duplicates
arp -a
# Use different IP address in same subnet
6.2 Revert to DHCP
If you need to revert to automatic IP assignment:
# Edit netplan configuration
sudo nano /etc/netplan/01-netcfg.yaml
Change configuration to:
network:
version: 2
renderer: networkd
ethernets:
enp5s0:
dhcp4: yes
dhcp6: yes
Apply changes:
sudo netplan apply
6.3 Emergency Recovery
If you lose network connectivity:
-
Physical Access Method:
# Restore from backup
sudo cp /etc/netplan/backup-*.yaml /etc/netplan/01-netcfg.yaml
sudo netplan apply -
Single User Mode:
- Boot into recovery mode
- Mount filesystem as read-write
- Edit netplan configuration
- Restart networking
7. Advanced Configurations
7.1 Multiple Network Interfaces
network:
version: 2
renderer: networkd
ethernets:
enp5s0: # Primary interface
dhcp4: no
addresses: [192.168.1.100/24]
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
enp6s0: # Secondary interface
dhcp4: no
addresses: [10.0.0.100/24]
routes:
- to: 10.0.0.0/8
via: 10.0.0.1
7.2 VLAN Configuration
network:
version: 2
renderer: networkd
ethernets:
enp5s0:
dhcp4: no
dhcp6: no
vlans:
vlan100:
id: 100
link: enp5s0
addresses: [192.168.100.10/24]
routes:
- to: default
via: 192.168.100.1
7.3 Bridge Configuration
network:
version: 2
renderer: networkd
ethernets:
enp5s0:
dhcp4: no
dhcp6: no
bridges:
br0:
interfaces: [enp5s0]
dhcp4: no
addresses: [192.168.1.100/24]
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
8. Best Practices
8.1 IP Address Planning
-
Use Private IP Ranges:
192.168.0.0/16
(192.168.0.1 - 192.168.255.254)10.0.0.0/8
(10.0.0.1 - 10.255.255.254)172.16.0.0/12
(172.16.0.1 - 172.31.255.254)
-
Avoid Conflicts:
- Check existing DHCP range
- Document assigned static IPs
- Use higher IP ranges for static assignments
-
Network Segmentation:
- Separate static IPs by function
- Use different subnets for different services
8.2 Security Considerations
# Disable unused network interfaces
sudo ip link set down <interface_name>
# Configure firewall for static IP
sudo ufw allow from 192.168.1.0/24
sudo ufw deny from 0.0.0.0/0
8.3 Documentation and Monitoring
-
Document Configuration:
- Keep network diagram updated
- Document IP assignments
- Note configuration changes
-
Monitor Network Status:
# Create monitoring script
#!/bin/bash
echo "Network Status Check - $(date)"
echo "================================"
ip addr show
echo "================================"
ip route show
echo "================================"
ping -c 1 8.8.8.8 >/dev/null && echo "Internet: OK" || echo "Internet: FAIL"
9. Integration with Other Services
9.1 SSH Server Configuration
For systems with static IP and SSH:
# Edit SSH configuration
sudo nano /etc/ssh/sshd_config
# Add specific interface binding
ListenAddress 192.168.1.100
# Restart SSH service
sudo systemctl restart ssh
9.2 Web Server Configuration
Configure web servers to bind to static IP:
# Apache virtual host
<VirtualHost 192.168.1.100:80>
ServerName myserver.local
DocumentRoot /var/www/html
</VirtualHost>
# Nginx server block
server {
listen 192.168.1.100:80;
server_name myserver.local;
root /var/www/html;
}
10. Automation and Scripting
10.1 Automated Configuration Script
#!/bin/bash
# Static IP configuration script
INTERFACE="enp5s0"
STATIC_IP="192.168.1.100/24"
GATEWAY="192.168.1.1"
DNS_SERVERS="8.8.8.8,8.8.4.4"
# Create netplan configuration
cat > /etc/netplan/01-static-ip.yaml << EOF
network:
version: 2
renderer: networkd
ethernets:
${INTERFACE}:
dhcp4: no
dhcp6: no
addresses: [${STATIC_IP}]
routes:
- to: default
via: ${GATEWAY}
nameservers:
addresses: [${DNS_SERVERS}]
EOF
# Apply configuration
netplan apply
echo "Static IP ${STATIC_IP} configured for ${INTERFACE}"
10.2 Configuration Validation Script
#!/bin/bash
# Network configuration validation
echo "Validating network configuration..."
# Check interface status
if ip link show $INTERFACE &>/dev/null; then
echo "✓ Interface $INTERFACE exists"
else
echo "✗ Interface $INTERFACE not found"
exit 1
fi
# Check IP assignment
if ip addr show $INTERFACE | grep -q "inet $STATIC_IP"; then
echo "✓ Static IP assigned correctly"
else
echo "✗ Static IP not assigned"
fi
# Check gateway connectivity
if ping -c 1 $GATEWAY &>/dev/null; then
echo "✓ Gateway reachable"
else
echo "✗ Gateway unreachable"
fi
# Check internet connectivity
if ping -c 1 8.8.8.8 &>/dev/null; then
echo "✓ Internet connectivity"
else
echo "✗ No internet connectivity"
fi
Resources and References
- Ubuntu Netplan Documentation
- Ubuntu Server Guide - Network Configuration
- Netplan Examples
- systemd-networkd Documentation
- Always test configuration with
netplan try
before permanent application - Keep backup of working configuration files
- Document your network setup for future reference
- Use consistent IP addressing scheme across your network
- Consider using configuration management tools for multiple servers
Static IP addresses are more visible on the network. Ensure proper firewall configuration and security hardening for systems with static IPs.