How to configure networking: static and DHCP
Topic: Servers linux
Summary
Set a static IP or use DHCP on Linux using netplan (Ubuntu), nmcli (NetworkManager), or /etc/network/interfaces. Verify with ip addr and ping; ensure DNS and default route work so the server is reachable and can reach the internet.
Intent: How-to
Quick answer
- Netplan (Ubuntu): edit /etc/netplan/*.yaml with addresses, gateway, nameservers; netplan apply. NetworkManager: nmcli con add/modify.
- Verify: ip addr show; ip route; ping gateway and 8.8.8.8; resolve names (getent hosts example.com).
- If you lose access after change, use console to revert or fix typo in IP/gateway; keep a backup of the working config.
Prerequisites
Steps
-
Identify interface and current config
ip link; ip addr show; note interface name (e.g. eth0, ens18) and current IP; back up current netplan or interfaces file.
-
Set static IP (netplan example)
Edit /etc/netplan/50-cloud-init.yaml or new file: addresses, gateway4 or routes, nameservers; run netplan apply; do not use tabs, use spaces in YAML.
-
Or use DHCP
Netplan: use dhcp4: true and optionally dhcp6; NetworkManager: nmcli con add type ethernet con-name NAME ifname IF ipv4.method auto.
-
Verify connectivity
ip addr; ip route; ping -c 2 GATEWAY; ping -c 2 8.8.8.8; getent hosts google.com; curl -I https://example.com if curl installed.
Summary
You will configure a network interface with a static IP or DHCP using netplan or NetworkManager, and confirm connectivity and DNS. Use this when deploying a server or changing its IP.
Prerequisites
- Root or sudo; console or existing SSH so you can fix config if you lose network.
- Know the desired IP, netmask, gateway, and DNS (or confirm DHCP is correct).
Steps
Step 1: Identify interface and current config
ip link show
ip addr show
cat /etc/netplan/*.yaml
Back up: sudo cp /etc/netplan/50-*.yaml /root/netplan.bak
Step 2: Set static IP (netplan example)
# /etc/netplan/99-static.yaml
network:
version: 2
ethernets:
ens18:
addresses: [192.168.1.10/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
sudo netplan apply
Step 3: Or use DHCP
In netplan set dhcp4: true for the interface; then netplan apply.
Step 4: Verify connectivity
ip addr show ens18
ip route
ping -c 2 192.168.1.1
ping -c 2 8.8.8.8
getent hosts google.com
Verification
- Interface has the expected IP; default route and DNS work; you can ping gateway and internet and resolve names.
Troubleshooting
No IP after netplan apply — Check YAML syntax (no tabs); correct interface name; run netplan apply --debug. Use console if SSH was lost.
No connectivity — Wrong gateway or mask; firewall on host or router; check ip route and ping gateway.