How To Change Ip In Linux – Using Terminal Command Methods

Changing your IP address in Linux requires just a few terminal commands that give you full control over your network configuration. If you’re wondering how to change ip in linux, you’ve come to the right place. This guide will walk you through every method, from temporary changes to permanent fixes, using both command-line tools and GUI options.

Whether you need a new IP for privacy, troubleshooting, or network testing, Linux makes it simple. You don’t need to be a sysadmin to follow along. Let’s get started with the basics.

Why You Might Need To Change Your IP In Linux

There are several reasons you might want to change your IP address. Maybe you’re setting up a static IP for a server. Or perhaps you’re troubleshooting a network conflict. Sometimes you just want to avoid IP-based restrictions.

Whatever your reason, Linux gives you multiple ways to do it. You can use the ip command, the older ifconfig, or network manager tools. Each method has its own advantages.

How To Change Ip In Linux

Now let’s dive into the main methods. We’ll cover both temporary and permanent changes, so you can choose what works best for your situation.

Method 1: Using The IP Command (Modern Way)

The ip command is the standard tool in modern Linux distributions. It’s part of the iproute2 package, which is installed by default on most systems.

First, check your current IP address:

ip addr show

This shows all network interfaces and their IPs. Note your interface name (like eth0, enp3s0, or wlp2s0).

To change the IP temporarily:

sudo ip addr add 192.168.1.100/24 dev eth0

This adds a new IP to the interface. To remove the old one:

sudo ip addr del 192.168.1.10/24 dev eth0

You can also flush all IPs and set a new one:

sudo ip addr flush dev eth0
sudo ip addr add 192.168.1.100/24 dev eth0

Don’t forget to set the default gateway:

sudo ip route add default via 192.168.1.1

Method 2: Using Ifconfig (Legacy Way)

Some older systems still use ifconfig. It’s part of the net-tools package. If it’s not installed, you can add it with:

sudo apt install net-tools   # Debian/Ubuntu
sudo yum install net-tools   # RHEL/CentOS

To change your IP:

sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up

This sets a new IP and activates the interface. To add a second IP:

sudo ifconfig eth0:0 192.168.1.200 netmask 255.255.255.0 up

Note that ifconfig changes are also temporary. They revert after a reboot.

Method 3: Using Network Manager (GUI And CLI)

Most desktop Linux distros use NetworkManager. You can change IPs through the GUI or the nmcli command.

For GUI users:

  1. Open Settings or Network Connections
  2. Select your connection (Wi-Fi or Ethernet)
  3. Click the gear icon or “Edit”
  4. Go to the IPv4 or IPv6 tab
  5. Change from Automatic (DHCP) to Manual
  6. Enter your desired IP, netmask, and gateway
  7. Click Apply and reconnect

For CLI users with nmcli:

nmcli con show   # List connections
sudo nmcli con mod "YourConnectionName" ipv4.addresses 192.168.1.100/24
sudo nmcli con mod "YourConnectionName" ipv4.gateway 192.168.1.1
sudo nmcli con mod "YourConnectionName" ipv4.method manual
sudo nmcli con down "YourConnectionName"
sudo nmcli con up "YourConnectionName"

This method makes the change permanent across reboots.

Method 4: Editing Network Configuration Files

For permanent changes without NetworkManager, you edit configuration files directly. The location varies by distribution.

On Debian/Ubuntu Systems

Edit /etc/network/interfaces:

sudo nano /etc/network/interfaces

Add or modify the interface section:

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

Then restart networking:

sudo systemctl restart networking

On RHEL/CentOS/Fedora Systems

Edit the interface file in /etc/sysconfig/network-scripts/:

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

Set these values:

DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
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

Then restart the network service:

sudo systemctl restart network

On Arch Linux

Use netctl or systemd-networkd. For systemd-networkd, create a file in /etc/systemd/network/:

sudo nano /etc/systemd/network/20-wired.network

Add:

[Match]
Name=eth0

[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8

Then enable and start:

sudo systemctl enable systemd-networkd
sudo systemctl start systemd-networkd

Changing IP For Specific Use Cases

Changing IP For A VPN Or Proxy

If you’re using a VPN, your IP changes automatically when you connect. But you can also set a static IP for the VPN interface.

For OpenVPN, edit your config file:

sudo nano /etc/openvpn/client.conf

Add:

ifconfig 10.8.0.2 255.255.255.0

For WireGuard, edit the config:

[Interface]
Address = 10.0.0.2/24
PrivateKey = yourPrivateKey

Changing IP For Docker Containers

Docker containers get IPs from a bridge network. To change a container’s IP:

docker network create --subnet=172.20.0.0/16 mynetwork
docker run --net mynetwork --ip 172.20.0.10 myimage

Or for existing containers:

docker network connect --ip 172.20.0.11 mynetwork mycontainer

Changing IP For Virtual Machines

For VMs using VirtualBox or KVM, you can set a static IP inside the VM using the methods above. Or configure the host’s virtual network.

For VirtualBox, use host-only or bridged networking. Then set the IP inside the guest OS.

Troubleshooting Common Issues

Sometimes things don’t work as expected. Here are common problems and fixes.

IP Change Not Taking Effect

Make sure you’re using the correct interface name. Check with ip link show. Also, ensure you have proper permissions (use sudo).

If using NetworkManager, restart it:

sudo systemctl restart NetworkManager

Lost Network Connectivity

If you lose connection after changing IP, you might have set the wrong gateway or subnet. Reboot or use a live USB to fix the config file.

You can also reset the interface:

sudo ip link set eth0 down
sudo ip link set eth0 up

Duplicate IP Address

If another device has the same IP, you’ll get conflicts. Use a unique IP in your subnet. You can check with:

arp-scan --local

Automating IP Changes With Scripts

You can write a bash script to change IPs quickly. Here’s a simple example:

#!/bin/bash
INTERFACE="eth0"
NEW_IP="192.168.1.100"
GATEWAY="192.168.1.1"

sudo ip addr flush dev $INTERFACE
sudo ip addr add $NEW_IP/24 dev $INTERFACE
sudo ip route add default via $GATEWAY
echo "IP changed to $NEW_IP on $INTERFACE"

Save it as changeip.sh, make it executable (chmod +x changeip.sh), and run it with sudo.

For more advanced automation, use cron or systemd timers to change IPs at specific times.

Security Considerations When Changing IP

Changing your IP can affect security. If you set a static IP, make sure it’s not already in use. Also, configure firewall rules to match your new IP.

For servers, update DNS records and any whitelists. For home networks, ensure your router’s DHCP range doesn’t include your static IP.

Using a VPN or proxy adds a layer of privacy, but remember that your ISP still sees your connection to the VPN server.

Frequently Asked Questions

How do I change my IP address in Linux permanently?

Edit the network configuration file for your distribution (like /etc/network/interfaces on Debian) or use NetworkManager with nmcli to set a manual IP.

Can I change my IP without sudo?

No, changing network settings requires root privileges. You must use sudo or run commands as root.

Will changing my IP affect other services?

Yes, if you’re running servers or services that rely on the old IP, you’ll need to update their configurations. Also, active connections may drop.

How do I change my IP back to DHCP?

Use sudo dhclient eth0 or set the method back to “automatic” in NetworkManager. For config files, change BOOTPROTO to dhcp.

What’s the difference between public and private IP changes?

Private IPs (like 192.168.x.x) are for local networks. Public IPs are assigned by your ISP. You can only change your public IP by contacting your ISP or using a VPN.

Conclusion

Now you know multiple ways to change your IP address in Linux. From the quick ip command to permanent config file edits, you have full control. Start with the method that feels easiest for you.

Remember to always double-check your subnet and gateway settings. A small mistake can disconnect you from the network. But with the steps above, you’ll be fine.

Practice on a test machine if you’re unsure. And keep this guide bookmarked for future reference. Changing IPs in Linux is a valuable skill that gets easier with practice.