Method 1: Using /etc/network/interfaces File
First, identify your network interface name:
bash
ip a
# Common names: eth0, ens33, enp0s5, wlp1s0
Step 1: Edit the interfaces file:
bash
sudo nano /etc/network/interfaces
Step 2: Configure static IP. Change from DHCP to:
bash
# Static IP Configuration
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
Step 1: Edit the interfaces file:
bash
sudo nano /etc/network/interfaces
Step 2: Configure DHCP:
bash
# DHCP Configuration (Dynamic)
auto eth0
iface eth0 inet dhcp
⚠️ Warning: Do NOT run these commands over SSH, as you'll lose connection!
bash
# Bring interface DOWN
sudo ifdown eth0
# Bring interface UP
sudo ifup eth0
# Verify new IP
ip a show eth0
bash
# Restart entire networking service
sudo systemctl restart networking
# Check status
sudo systemctl status networking
# Verify IP address
ip addr show
bash
sudo ip link set eth0 down
sudo ip link set eth0 up
sudo ip link show eth0
bash
# For systemd-managed interfaces
sudo systemctl restart ifup@eth0.service
For better organization, you can separate interface configs:
Step 1: Create interface-specific file:
bash
sudo nano /etc/network/interfaces.d/eth0
Step 2: Add configuration:
bash
# Static IP
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
Step 3: Modify main interfaces file (ensure source line exists):
bash
source /etc/network/interfaces.d/*
# Loopback
auto lo
iface lo inet loopback
# Reference to external config
auto eth0
bash
# Check IP assignment
ip addr show eth0
# Check for active DHCP client
ps aux | grep dhclient
# View DHCP leases (if DHCP)
cat /var/lib/dhcp/dhclient.eth0.leases
# Test connectivity
ping -c 4 8.8.8.8
ping -c 4 google.com