Setting a specific IP address on your Linux server ensures consistent network connectivity. If you’re wondering how to change ip address linux, you’ve come to the right place. This guide will walk you through every method, from simple commands to permanent configuration files.
Whether you’re using Ubuntu, Debian, CentOS, or Fedora, the process is straightforward once you understand the basics. Let’s get started with the quickest way to make changes.
Understanding Ip Addresses In Linux
Your Linux machine uses an IP address to communicate on a network. There are two main types: dynamic (assigned by DHCP) and static (manually set). Changing your IP address can help with network troubleshooting, security, or setting up services like web servers.
Before making changes, you need to know your current network interface name. Common names include eth0, enp0s3, wlan0, or ens33. You can find this using the ip link or ifconfig command.
Checking Your Current Ip Configuration
Open your terminal and type:
ip addr show
This shows all network interfaces and their assigned IPs. Look for an interface with an inet line, like 192.168.1.10/24. Write down the interface name—you’ll need it later.
How To Change Ip Address Linux Temporarily
This method changes your IP address until you reboot. It’s perfect for testing or quick fixes. Use the ip command, which is available on most modern distributions.
Using The Ip Command
First, remove the current IP address from your interface. Replace eth0 with your actual interface name:
sudo ip addr del 192.168.1.10/24 dev eth0
Then add the new IP address:
sudo ip addr add 192.168.1.20/24 dev eth0
Verify the change:
ip addr show eth0
You should see the new IP listed. This change is immediate but won’t survive a reboot.
Setting A Default Gateway
If you need internet access, set a default gateway:
sudo ip route add default via 192.168.1.1
Replace 192.168.1.1 with your router’s IP. Check your routing table with ip route show.
Using Ifconfig (Older Method)
Some older systems still use ifconfig. Install net-tools if needed:
sudo apt install net-tools # Debian/Ubuntu
Then change the IP:
sudo ifconfig eth0 192.168.1.20 netmask 255.255.255.0
Add a default gateway:
sudo route add default gw 192.168.1.1
This method works but is less flexible than the ip command.
How To Change Ip Address Linux Permanently
For a lasting change, you need to edit configuration files. The exact file depends on your Linux distribution and network manager.
Ubuntu And Debian (Netplan)
Modern Ubuntu uses Netplan. Configuration files are in /etc/netplan/. Look for a .yaml file like 01-netcfg.yaml.
Edit the file with sudo:
sudo nano /etc/netplan/01-netcfg.yaml
Change the configuration to set a static IP. Here’s an example:
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.20/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
Apply the changes:
sudo netplan apply
Check with ip addr show. If you get errors, verify your YAML indentation—spaces matter!
Older Ubuntu With /Etc/network/interfaces
For older versions or systems without Netplan, edit the interfaces file:
sudo nano /etc/network/interfaces
Change the section for your interface:
auto eth0
iface eth0 inet static
address 192.168.1.20
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
Or reboot to confirm persistence.
CentOS, RHEL, Fedora (NetworkManager)
These distributions use NetworkManager. Edit the connection file in /etc/sysconfig/network-scripts/. Find the file named ifcfg-eth0 (or your interface).
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
Set the following parameters:
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.1.20
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
Or use nmcli to reload:
sudo nmcli connection reload
Using Nmcli For NetworkManager
You can also change IP settings via command line with nmcli. First, list connections:
nmcli connection show
Modify the connection:
sudo nmcli connection modify eth0 ipv4.addresses 192.168.1.20/24
sudo nmcli connection modify eth0 ipv4.gateway 192.168.1.1
sudo nmcli connection modify eth0 ipv4.dns "8.8.8.8 8.8.4.4"
sudo nmcli connection modify eth0 ipv4.method manual
Bring the connection down and up:
sudo nmcli connection down eth0
sudo nmcli connection up eth0
Changing Ip Address Via Dhcp
If you want a new IP from your router, release and renew the DHCP lease.
Using Dhclient
Release the current lease:
sudo dhclient -r eth0
Request a new one:
sudo dhclient eth0
This often gives you a different IP, depending on your router’s settings.
Using Nmcli For Dhcp
Switch back to DHCP with nmcli:
sudo nmcli connection modify eth0 ipv4.method auto
sudo nmcli connection down eth0
sudo nmcli connection up eth0
Troubleshooting Common Issues
Sometimes things go wrong. Here are fixes for typical problems.
No Internet After Change
Check your gateway and DNS settings. Use ping 8.8.8.8 to test internet. If that works but domain names don’t, check DNS in /etc/resolv.conf.
Interface Name Changed
If you see a different interface name after reboot, it might be due to predictable network interface names. Use ip link to find the current name.
Permission Denied
All network changes require root privileges. Use sudo before every command.
Configuration File Errors
YAML files are sensitive to spaces. Use a YAML validator if you get errors. For interfaces files, ensure no typos in keywords.
How To Change Ip Address Linux For Wifi
Wireless interfaces work similarly. Replace eth0 with wlan0 or your wifi interface name.
Using Iwconfig And Ifconfig
For temporary changes on wifi:
sudo ifconfig wlan0 192.168.1.30 netmask 255.255.255.0
Then set the gateway as before.
Permanent Wifi Configuration
In Netplan, add the wifi interface under the ethernets section or create a separate wifi block. For NetworkManager, use nmcli with the wifi connection name.
Automating Ip Changes With Scripts
If you frequently change IPs, write a simple bash script.
#!/bin/bash
sudo ip addr flush dev eth0
sudo ip addr add 192.168.1.20/24 dev eth0
sudo ip route add default via 192.168.1.1
Save as change_ip.sh, make executable with chmod +x change_ip.sh, and run with sudo.
Security Considerations
Changing your IP address can affect firewall rules. If you use iptables or ufw, update rules to allow traffic on the new IP. Also, ensure SSH access if you’re remote—test with a new terminal session before closing the current one.
Frequently Asked Questions
How Do I Change My IP Address In Linux Using The Terminal?
Use the ip addr add command for temporary changes, or edit configuration files in /etc/netplan or /etc/sysconfig/network-scripts for permanent ones.
Can I Change My IP Address Without Rebooting?
Yes, using the ip or ifconfig command changes it immediately without a reboot. Permanent changes via config files usually require a network restart.
What Is The Difference Between Static And Dynamic IP In Linux?
A static IP is manually set and doesn’t change, while a dynamic IP is assigned by DHCP and can change over time. Static IPs are better for servers.
How To Change IP Address In Linux For Multiple Interfaces?
Repeat the same steps for each interface (e.g., eth0, eth1). Use the correct interface name in each command or config file.
Why Does My IP Change Back After Reboot?
You made a temporary change. To make it permanent, edit the network configuration files as described in the permanent methods section.
Final Thoughts On Changing Ip Address In Linux
Now you know multiple ways to change your IP address on Linux. Start with the temporary method for quick tests, then move to permanent configuration for production systems. Always double-check your settings with ip addr show and test connectivity.
Remember to document your changes, especially if you manage multiple servers. With practice, this process becomes second nature. If you run into issues, revisit the troubleshooting tips above.
Your Linux system is now configured with the IP address you need. Whether for security, networking, or service setup, you have the skills to manage it effectively. Keep experimenting and learning—Linux gives you full control over your network.