Skip to content

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.

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.

Terminal window
ip -br a # interface names and current addresses
ip 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. Not lo.
  • Address + prefix, e.g. 192.168.1.50/24. /24 = netmask 255.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.

Terminal window
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)

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):

Terminal window
sudo cp /etc/netplan/50-cloud-init.yaml /root/netplan-backup.yaml
sudo nano /etc/netplan/50-cloud-init.yaml
network:
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. Use routes: - to: default as above. Old tutorials still use gateway4, 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:

Terminal window
sudo netplan try

This 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:

Terminal window
tmux new -s net
sudo netplan try

Your 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):

Terminal window
nmcli con show
# NAME UUID TYPE DEVICE
# Wired connection 1 ... ethernet enp3s0
Terminal window
sudo 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):

Terminal window
sudo nmcli con up "Wired connection 1"

Check:

Terminal window
nmcli con show "Wired connection 1" | grep ipv4
ip -br a

To 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.

/etc/network/interfaces:

auto lo
iface lo inet loopback
auto ens18
iface ens18 inet static
address 192.168.1.50/24
gateway 192.168.1.1

DNS is separate. Without the resolvconf package, dns-nameservers lines do nothing. Just edit /etc/resolv.conf:

nameserver 192.168.1.1
nameserver 1.1.1.1

Apply:

Terminal window
sudo ifdown ens18 && sudo ifup ens18

Run 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.

Terminal window
ip -br a # new address on the interface
ip r # default via your gateway
ping -c 3 192.168.1.1 # gateway reachable
ping -c 3 1.1.1.1 # internet reachable (routing works)
ping -c 3 example.com # DNS works

If 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.