Assigning a static IP address in Linux requires editing network configuration files or using the terminal. If you’re wondering how to set ip address in linux, you’ve come to the right place. This guide walks you through multiple methods, from command-line tools to GUI settings, so you can choose what works best for your system. Whether you’re using Ubuntu, CentOS, or Debian, the steps are clear and practical.
Network configuration can feel daunting, but it doesn’t have to be. With a few commands or file edits, you’ll have a static IP up and running. Let’s start with the basics and then dive into distro-specific instructions.
Understanding IP Address Configuration In Linux
An IP address identifies your device on a network. By default, most Linux systems use DHCP to get a dynamic IP from a router. But sometimes you need a static IP—for servers, SSH access, or consistent connectivity. The process varies slightly depending on your Linux distribution and network manager.
You can set an IP address using:
- Command-line tools like
iporifconfig - Network configuration files (e.g.,
/etc/network/interfacesor/etc/netplan/*.yaml) - GUI network settings
Each method has its pros and cons. We’ll cover all three so you can pick the one that fits your workflow.
How To Set Ip Address In Linux
This section covers the core steps for setting a static IP. We’ll use both temporary and permanent methods. Temporary changes are great for testing, while permanent edits survive reboots.
Method 1: Using The Ip Command (Temporary)
The ip command is modern and widely available. It replaces the older ifconfig. To assign a static IP temporarily, follow these steps:
- Open a terminal.
- Check your network interface name with
ip link show. Common names areeth0,enp0s3, orwlan0. - Assign the IP address:
sudo ip addr add 192.168.1.100/24 dev eth0 - Bring the interface up if needed:
sudo ip link set eth0 up
This change disappears after a reboot. To make it permanent, you’ll need to edit config files or use a network manager.
Method 2: Editing Network Configuration Files (Permanent)
Different distros use different files. Here are the most common ones:
For Debian/Ubuntu (Netplan)
Ubuntu 18.04 and later use Netplan. Configuration files are in /etc/netplan/. Look for a .yaml file (e.g., 01-netcfg.yaml).
- Backup the file:
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak - Edit it with nano or vim:
sudo nano /etc/netplan/01-netcfg.yaml - Add or modify the configuration. Example for a static IP:
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
- Apply the changes:
sudo netplan apply
If you get an error, check the YAML indentation. Spaces matter—no tabs allowed.
For Older Ubuntu/Debian (Interfaces File)
If your system uses /etc/network/interfaces, edit it directly:
- Open the file:
sudo nano /etc/network/interfaces - Add these lines for a static IP:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
- Restart networking:
sudo systemctl restart networking
For CentOS/RHEL/Fedora
These distros use /etc/sysconfig/network-scripts/ifcfg-eth0. Edit the file for your interface:
- Open the file:
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0 - Set these parameters:
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes
- Restart the network service:
sudo systemctl restart network
Method 3: Using NetworkManager (GUI And CLI)
Most desktop Linux distros use NetworkManager. You can set a static IP via GUI or command line.
GUI Method (GNOME/KDE)
- Go to Settings > Network.
- Click the gear icon next to your connection.
- Select the IPv4 tab.
- Change from Automatic (DHCP) to Manual.
- Enter your IP, netmask, gateway, and DNS.
- Click Apply and reconnect.
CLI Method (Nmcli)
For headless servers, use nmcli:
- List connections:
nmcli con show - Modify the connection:
sudo nmcli con mod "Wired connection 1" ipv4.addresses 192.168.1.100/24 - Set the gateway:
sudo nmcli con mod "Wired connection 1" ipv4.gateway 192.168.1.1 - Set DNS:
sudo nmcli con mod "Wired connection 1" ipv4.dns "8.8.8.8 8.8.4.4" - Change method to manual:
sudo nmcli con mod "Wired connection 1" ipv4.method manual - Restart the connection:
sudo nmcli con down "Wired connection 1" && sudo nmcli con up "Wired connection 1"
Verifying Your New IP Address
After setting the IP, confirm it’s working:
- Use
ip addr showorifconfigto see the assigned IP. - Ping your gateway:
ping 192.168.1.1 - Check internet access:
ping google.com
If something fails, double-check your configuration files for typos or incorrect subnet masks.
Common Issues And Troubleshooting
Even with careful steps, things can go wrong. Here are frequent problems and fixes:
Network Service Won’t Restart
If systemctl restart networking fails, look for syntax errors in your config files. Use journalctl -xe to see error logs.
No Internet After Setting Static IP
This usually means the gateway or DNS is wrong. Verify the gateway IP matches your router’s address. Test DNS with nslookup google.com.
IP Address Conflicts
If another device uses the same IP, you’ll get an error. Use a unique address outside your DHCP range. Check your router’s DHCP settings to avoid overlaps.
Netplan Apply Fails
Netplan is picky about YAML formatting. Use sudo netplan try to test before applying. If it fails, revert to the backup file.
Setting IP Address On Different Linux Distros
Let’s look at specific examples for popular distributions.
Ubuntu 20.04 And Later
Ubuntu uses Netplan by default. Follow the Netplan steps above. For older versions (pre-18.04), use the interfaces file method.
Debian 10 And 11
Debian still supports /etc/network/interfaces. Edit that file and restart networking. You can also install Netplan if you prefer.
CentOS 7 And 8
CentOS 7 uses the ifcfg files. CentOS 8 also supports NetworkManager. Use nmcli or edit the config files directly.
Fedora
Fedora relies on NetworkManager. The nmcli method works well. You can also use the GUI if you have a desktop environment.
Arch Linux
Arch uses systemd-networkd or Netplan. Create a .network file in /etc/systemd/network/. Example:
[Match]
Name=eth0
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
Then enable and start the service: sudo systemctl enable --now systemd-networkd
Advanced Configuration: Multiple IPs And VLANs
Sometimes you need more than one IP on a single interface. Here’s how:
Adding A Secondary IP
Use the ip command with a different address:
sudo ip addr add 192.168.1.101/24 dev eth0
To make it permanent, add another address line in Netplan or an additional iface eth0 inet static block in the interfaces file.
Setting Up VLANs
VLANs let you segment network traffic. First, install vlan package:
sudo apt install vlan (Debian/Ubuntu) or sudo yum install vlan (CentOS)
Then create a VLAN interface:
sudo ip link add link eth0 name eth0.10 type vlan id 10
Assign an IP to it: sudo ip addr add 192.168.10.100/24 dev eth0.10
For permanent config, add the VLAN in your network files.
Automating IP Configuration With Scripts
If you manage multiple machines, scripting saves time. Here’s a simple bash script to set a static IP using Netplan:
#!/bin/bash
INTERFACE="eth0"
IP="192.168.1.100/24"
GATEWAY="192.168.1.1"
DNS="8.8.8.8,8.8.4.4"
cat > /etc/netplan/01-netcfg.yaml << EOF
network:
version: 2
renderer: networkd
ethernets:
$INTERFACE:
dhcp4: no
addresses:
- $IP
gateway4: $GATEWAY
nameservers:
addresses: [$DNS]
EOF
netplan apply
Save it as set_static_ip.sh, make it executable (chmod +x set_static_ip.sh), and run it with sudo.
Security Considerations
Static IPs can be more predictable for attackers. Consider these tips:
- Use a firewall (like
ufworiptables) to restrict access. - Change default SSH ports if exposing the machine.
- Keep your system updated.
- Use strong passwords or SSH keys.
Frequently Asked Questions
What Is The Difference Between Static And Dynamic IP?
A static IP stays the same, while a dynamic IP changes periodically. Static is better for servers and remote access.
Can I Set An IP Address Without Sudo?
No, setting IP addresses requires root privileges. Use sudo or log in as root.
How Do I Find My Current IP Address In Linux?
Use ip addr show or hostname -I. The latter shows only the IP, not the interface details.
Why Does My Static IP Reset After Reboot?
You likely used a temporary command like ip addr add. Make the change permanent by editing config files or using NetworkManager.
What Is The Default Gateway In Linux?
The default gateway is the router's IP address, usually 192.168.1.1 or 10.0.0.1. Check with ip route show default.
Final Tips For Success
Setting a static IP in Linux is straightforward once you know your distribution's tools. Always backup config files before editing. Test changes with a reboot to ensure they persist. If you're new to Linux, start with the GUI method or nmcli for simplicity.
Remember that network interfaces names can vary. Use ip link show to confirm yours. And if you get stuck, the man pages and community forums are your best freinds.
With this guide, you now have the knowledge to configure IP addresses on any Linux system. Practice on a virtual machine first if you're unsure. Once you master it, you'll appreciate the control and reliability of a static setup.