Setting a Static IP
Every Linux distro configures networking differently, and some of them do it two different ways at once. Here’s how to figure out which one you’ve got and how to set a static IP on each without losing your SSH session.
Consider just not
Section titled “Consider just not”On a home or office LAN, a DHCP reservation on the router is often the better answer. The server stays on DHCP, the router always hands it the same address, and there’s one place to see every reserved IP. You don’t have to edit anything on the server, so there’s nothing to get wrong on it.
Static IPs on the box itself make sense for routers, DNS servers, DHCP servers (obviously), or networks where you don’t control the router.
Gather your info first
Section titled “Gather your info first”ip -br a # interface names and current addressesip r # current default gateway ("default via ...")resolvectl status # current DNS servers (or: cat /etc/resolv.conf)Write down:
- Interface name, e.g.
ens18,enp3s0,eth0. Notlo. - Address + prefix, e.g.
192.168.1.50/24./24= netmask255.255.255.0. - Gateway, e.g.
192.168.1.1 - DNS servers, e.g. your router, or
1.1.1.1,9.9.9.9
Make sure the new IP isn’t in the DHCP pool, or the router will eventually hand it to someone’s phone and you’ll get an IP conflict that comes and goes. Those are the worst kind.
Which one do I have?
Section titled “Which one do I have?”ls /etc/netplan/ 2>/dev/null # files here -> Netplan (Ubuntu)systemctl is-active NetworkManager # "active" -> NetworkManager (RHEL, Fedora, desktops)cat /etc/network/interfaces 2>/dev/null # config here -> ifupdown (Debian server)Ubuntu: Netplan
Section titled “Ubuntu: Netplan”Netplan files are YAML in /etc/netplan/. There’s usually one already, like 50-cloud-init.yaml or 00-installer-config.yaml. Back it up, then edit it (or replace it):
sudo cp /etc/netplan/50-cloud-init.yaml /root/netplan-backup.yamlsudo nano /etc/netplan/50-cloud-init.yamlnetwork: version: 2 ethernets: ens18: dhcp4: false addresses: - 192.168.1.50/24 routes: - to: default via: 192.168.1.1 nameservers: addresses: [192.168.1.1, 1.1.1.1]- Spaces, not tabs. YAML will hurt you over this.
gateway4:is deprecated. Useroutes: - to: defaultas above. Old tutorials still usegateway4, which is how you can tell they’re old.- Netplan wants these files private:
sudo chmod 600 /etc/netplan/*.yaml, or it’ll complain every time.
Apply it with rollback:
sudo netplan tryThis applies the config and waits 120 seconds for you to press Enter. If you don’t, it reverts to the old config. The problem: if the IP changes, your SSH session dies, and you can’t press Enter. So run it inside tmux:
tmux new -s netsudo netplan tryYour session drops. SSH in on the new IP, tmux attach -t net, and press Enter. Can’t reach the new IP? Wait two minutes, it reverts, and you SSH back in on the old IP and figure out what you typo’d.
sudo netplan apply applies it immediately with no safety net. Use it once you’re sure.
RHEL / Rocky / Alma / Fedora: NetworkManager
Section titled “RHEL / Rocky / Alma / Fedora: NetworkManager”Use nmcli. Don’t hand-edit the files. First, find the connection name (not the same as the device name, of course):
nmcli con show# NAME UUID TYPE DEVICE# Wired connection 1 ... ethernet enp3s0sudo nmcli con mod "Wired connection 1" \ ipv4.method manual \ ipv4.addresses 192.168.1.50/24 \ ipv4.gateway 192.168.1.1 \ ipv4.dns "192.168.1.1 1.1.1.1"Apply (this drops your session if the IP changes):
sudo nmcli con up "Wired connection 1"Check:
nmcli con show "Wired connection 1" | grep ipv4ip -br aTo go back to DHCP: sudo nmcli con mod "Wired connection 1" ipv4.method auto ipv4.addresses "" ipv4.gateway "", then con up again.
No try with rollback here, so use the shutdown -r +10 trick from the top of this page. nmcli changes are persistent, though, so a reboot won’t undo them. Console access is your real safety net on these boxes. If you only have SSH, consider adding the new IP alongside the old one first (+ipv4.addresses), confirming you can reach it, then removing the old one.
There’s also nmtui, a text menu, if you’d rather not type all that. No shame in it. I use it.
Debian (server): ifupdown
Section titled “Debian (server): ifupdown”/etc/network/interfaces:
auto loiface lo inet loopback
auto ens18iface ens18 inet static address 192.168.1.50/24 gateway 192.168.1.1DNS is separate. Without the resolvconf package, dns-nameservers lines do nothing. Just edit /etc/resolv.conf:
nameserver 192.168.1.1nameserver 1.1.1.1Apply:
sudo ifdown ens18 && sudo ifup ens18Run that as one line. If you run ifdown alone over SSH, you just cut off the session you’d need to run ifup. The && makes sure both run, even after your connection drops. Even safer, run it inside tmux or screen, so it finishes even if the SSH session dies mid-command.
Check it worked (and survives a reboot)
Section titled “Check it worked (and survives a reboot)”ip -br a # new address on the interfaceip r # default via your gatewayping -c 3 192.168.1.1 # gateway reachableping -c 3 1.1.1.1 # internet reachable (routing works)ping -c 3 example.com # DNS worksIf 1.1.1.1 pings but example.com doesn’t, it’s DNS. If neither works, it’s the gateway or the prefix. Then reboot once while you can still get to the console, and check again. A config that only works until reboot will wait for the worst possible moment to show it.
Add it the same way: addresses: takes IPv6 too in Netplan, and nmcli has ipv6.method/ipv6.addresses. On most networks, SLAAC handles IPv6 by itself and you can leave it alone. “Leave it alone” is underrated advice in general.