Linux Networking Basics: Configuring IP and DNS
Master Linux networking basics: learn how to configure IP and DNS settings step-by-step. A clear, practical guide for Linux and DevOps beginners to set up networking and troubleshoot common issues.
Short introduction
Networking is one of the first skills every Linux user should learn — it lets you connect machines, services, and people. This guide covers the essentials of configuring IP addresses and DNS on common Linux systems, with practical examples you can run today.
Understanding network interfaces and basic commands
Before changing anything, identify your network interfaces and current settings. The modern tool is ip; older systems may still use ifconfig.
Example: list interfaces and addresses
# show all interfaces and their IPs
ip addr show
# show link state and MTU
ip link show
# show the default route
ip route show
Quick explanations:
- ip addr show — shows addresses (IPv4/IPv6) assigned to each interface.
- ip link show — shows whether an interface is up or down.
- ip route show — shows routing table including default gateway.
Bring an interface up/down:
# bring interface eth0 up
sudo ip link set dev eth0 up
# bring it down
sudo ip link set dev eth0 down
Verify connectivity:
# test gateway connectivity and DNS separately
ping -c 3 8.8.8.8 # checks basic IP-level connectivity
ping -c 3 google.com # checks DNS resolution + connectivity
Configuring a static IP (Netplan example for Ubuntu)
On modern Ubuntu (18.04+), Netplan is used to declare network configuration in YAML and hand it to NetworkManager or systemd-networkd.
Example netplan file (/etc/netplan/01-netcfg.yaml):
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
addresses:
- 192.168.1.50/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
Apply the configuration:
sudo netplan try # tests config and reverts on failure
sudo netplan apply # apply permanently
Alternative: Debian/older Ubuntu using /etc/network/interfaces:
auto eth0
iface eth0 inet static
address 192.168.1.50
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 1.1.1.1
Then:
sudo ifdown eth0 && sudo ifup eth0
# or restart networking service
sudo systemctl restart networking
Notes:
- Always backup configs before editing.
- Ensure correct netmask (CIDR or dotted notation) and gateway.
Using DHCP to get an IP address
DHCP is the most common way to get IP configuration automatically.
Using dhclient manually:
# request DHCP lease on interface enp0s3
sudo dhclient -v enp0s3
# release a DHCP lease
sudo dhclient -r enp0s3
If your distro uses NetworkManager, you can use nmcli:
# show device status
nmcli device status
# request DHCP on interface
sudo nmcli device reapply enp0s3
Systemd-networkd-managed interface (example .network file in /etc/systemd/network/10-dhcp.network):
[Match]
Name=enp0s3
[Network]
DHCP=yes
Then:
sudo systemctl restart systemd-networkd
Verify DHCP-assigned address:
ip addr show enp0s3
ip route show
Configuring and testing DNS
DNS translates hostnames to IPs. On many Linux systems /etc/resolv.conf is the file the resolver library consults, but it may be managed by NetworkManager, systemd-resolved, or other services.
Check current DNS info:
# view /etc/resolv.conf
cat /etc/resolv.conf
# if using systemd-resolved
resolvectl status
# or
systemd-resolve --status
Temporarily test with dig or nslookup:
# use system resolver
dig google.com +short
# or specify a DNS server directly
dig @8.8.8.8 google.com +short
# nslookup example
nslookup example.com 1.1.1.1
If you need to set DNS manually (Netplan example already used nameservers), for NetworkManager you can run:
# set DNS for connection "Wired connection 1"
sudo nmcli connection modify "Wired connection 1" ipv4.dns "8.8.8.8 1.1.1.1"
sudo nmcli connection up "Wired connection 1"
For systems using systemd-resolved, you can set DNS with:
# set DNS for link
sudo resolvectl dns enp0s3 8.8.8.8 1.1.1.1
# set domain search (optional)
sudo resolvectl domain enp0s3 example.com
Important: Don't edit /etc/resolv.conf by hand if a manager rewrites it; adjust the manager's config instead.
Troubleshooting and verification
When something doesn't work, isolate where the failure is happening (link, IP, route, DNS).
Check link and IP:
ip link show enp0s3
ip addr show enp0s3
Check routing:
ip route show
# test default gateway
ip route get 8.8.8.8
Check DNS resolution separately:
# IP-level ping (bypasses DNS)
ping -c 3 8.8.8.8
# hostname ping (tests DNS)
ping -c 3 www.example.com
# use dig to compare system resolver vs specific server
dig +short @8.8.8.8 www.example.com
dig +short www.example.com
Capture packets for deeper debugging (requires root):
# show DNS queries on interface
sudo tcpdump -n -i enp0s3 port 53
If routes are missing, add one temporarily:
sudo ip route add default via 192.168.1.1 dev enp0s3
Log inspection:
# system logs may contain network manager messages
sudo journalctl -u NetworkManager --since "15 minutes ago"
sudo journalctl -u systemd-networkd --since "15 minutes ago"
Common Pitfalls
- Overwriting a managed file: Editing /etc/resolv.conf or /etc/network/interfaces while NetworkManager or systemd-resolved is managing those files leads to your changes being overwritten. Change the manager's configuration instead.
- Wrong netmask/gateway: A typo in netmask or gateway will make the host unreachable on the LAN. Double-check CIDR (/24) vs dotted netmask formats (255.255.255.0).
- Multiple network managers: Having NetworkManager, systemd-networkd, and ifupdown all trying to configure interfaces will cause conflicts. Use one manager per interface and disable others for that interface.
Next Steps
- Learn the ip suite: practice ip addr, ip route, ip link to become comfortable managing interfaces and routes.
- Automate configs: create persistent Netplan or systemd-networkd files for repeatable setups, especially for servers.
- Monitor and secure: learn tcpdump for traffic monitoring and iptables/nftables for firewalling to protect networked services.
👉 Explore more IT books and guides at dargslan.com.