Here’s how to temporarily set a static IP address on a Linux system using command-line tools. These changes will reset after reboot (for permanent setups, edit your network configuration files).
Contents
Method 1: Using the ip
Command (Modern Systems)
Step 1: Check Current Network Interface
ip addr show
Identify your active interface (e.g., eth0
, enp0s3
, or wlan0
for Wi-Fi).
Step 2: Assign a Static IP Address
sudo ip addr add <IP-Address>/<Subnet-Mask> dev <interface>
Example:
sudo ip addr add 192.168.1.100/24 dev eth0
Step 3: Set Default Gateway
sudo ip route add default via <Gateway-IP> dev <interface>
Example:
sudo ip route add default via 192.168.1.1 dev eth0
Step 4: Configure DNS (Optional)
Temporarily add DNS servers to /etc/resolv.conf
(may reset on reboot):
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.conf # Google DNS
echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf
Method 2: Using ifconfig
& route
(Legacy Systems)
Step 1: Assign IP Address
sudo ifconfig <interface> <IP-Address> netmask <Subnet-Mask>
Example:
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
Step 2: Set Default Gateway
sudo route add default gw <Gateway-IP> <interface>
Example:
sudo route add default gw 192.168.1.1 eth0
Method 3: Using nmcli
(NetworkManager Systems)
sudo nmcli con mod <Connection-Name> ipv4.addresses <IP-Address>/<Subnet-Mask>
sudo nmcli con mod <Connection-Name> ipv4.gateway <Gateway-IP>
sudo nmcli con mod <Connection-Name> ipv4.dns "8.8.8.8,8.8.4.4"
sudo nmcli con up <Connection-Name>
Example:
sudo nmcli con mod "Wired Connection 1" ipv4.addresses 192.168.1.100/24
sudo nmcli con mod "Wired Connection 1" ipv4.gateway 192.168.1.1
sudo nmcli con up "Wired Connection 1"
Verify the Configuration
ip addr show <interface> # Check IP assignment
ip route show # Verify gateway
ping google.com # Test internet connectivity
Revert to DHCP (Dynamic IP)
To undo the static IP and return to DHCP:
sudo dhclient <interface>
Example:
sudo dhclient eth0
Important Notes
- ⚠️ Temporary Changes: These settings do not survive reboots. For permanent setups, edit
/etc/network/interfaces
(Debian) or/etc/sysconfig/network-scripts/ifcfg-<interface>
(RHEL/CentOS). - 🔧 Interface Names: Use
ip link show
to confirm your interface name. - 🔄 NetworkManager: If your system uses NetworkManager, avoid mixing
ip
/ifconfig
withnmcli
to prevent conflicts.