Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Set Up a Static IP Address on Linux

Check your network interface, then set a fixed IP temporarily with ip, or permanently with Netplan.

How-To Guides2 min read

Why use a static IP

  • Reliable remote connections through SSH or remote desktop.
  • A stable address for websites and API servers.
  • Simple firewall rules and port forwarding.
  • Easier management of the internal network.
  • Easier monitoring and log analysis.

Check your network interface

ip addr show

It lists the interfaces and their addresses. Note:

  • The interface name (eth0, ens3, enp0s3).
  • The current IP address.
  • The netmask (for example /24).
  • The state of the interface.

Find the current gateway with ip route show default.

Temporary static IP with ip

sudo ip addr add 192.168.1.100/24 dev eth0

Bring the interface up if it is down:

sudo ip link set eth0 up

This setting is lost after a reboot.

Permanent static IP on Ubuntu with Netplan

Modern Ubuntu uses Netplan to manage the network.

Step 1: Find the configuration file

ls /etc/netplan/

The file is often 01-netcfg.yaml, 00-installer-config.yaml or 50-cloud-init.yaml.

Step 2: Edit it

sudo nano /etc/netplan/01-netcfg.yaml

Example:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

Use spaces, not tabs. The keyword gateway4 still works on older releases but is deprecated, so use routes as shown. Use your own interface name, address and gateway.

Step 3: Apply

sudo netplan try

netplan try rolls back after 120 seconds if you lose the connection, which protects you on a remote server. When it works, press Enter to keep it, or use:

sudo netplan apply

Static IP on CentOS, AlmaLinux and Rocky (NetworkManager)

sudo nmcli con mod eth0 ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns "8.8.8.8 8.8.4.4"
sudo nmcli con up eth0

Use the connection name shown by nmcli con show.

Debian (ifupdown)

Edit /etc/network/interfaces:

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

Then run sudo systemctl restart networking.

Check the setting

ip addr show
ping -c 3 google.com

The interface should show your address, and the ping should work.

Warning: A wrong address or gateway on a remote server can cut your connection. Keep console access, and pick an address outside the DHCP range of your network. On a cloud VPS, follow your provider's rules, because the provider assigns the addresses.

Quick recap

  • ip addr add is temporary; Netplan or NetworkManager is permanent.
  • Use netplan try on a remote machine.