Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 15Network · Linux

How to set a static IP address

Servers need an address that does not change: DNS records, firewall rules, load balancers and monitoring all point to it. RHEL, Rocky and Alma use NetworkManager (nmcli); Ubuntu Server uses netplan. This how-to sets a fixed IPv4 address, gateway and DNS on both, shows how to do it without losing a remote connection, and how to stop cloud-init or DHCP from changing it back.

  • 5 Lessen
  • 6 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

15.1

The quick answer

RHEL, Rocky, Alma (NetworkManager):

nmcli connection show
NAME    UUID                                  TYPE      DEVICE
ens192  4c1f2a9e-7d1b-4b6e-9a51-0f3e5c2d8a10  ethernet  ens192
nmcli connection modify ens192 ipv4.method manual ipv4.addresses 192.0.2.10/24 ipv4.gateway 192.0.2.1 ipv4.dns "192.0.2.53 192.0.2.54"
nmcli connection up ens192

Ubuntu Server (netplan): write the file below, then netplan try.

Commando's in deze les

15.2

Step by step

  1. Write down the facts first: interface name (ip -br link), the new address with prefix (/24), gateway, DNS servers. Check that nobody else uses the address:
ip -br addr
lo        UNKNOWN  127.0.0.1/8
ens192    UP       192.0.2.77/24
arping -D -c 3 -I ens192 192.0.2.10

arping -D shows no reply when the address is free.

  1. RHEL: change the connection profile (the settings are saved and survive a reboot):
nmcli connection modify ens192 ipv4.method manual ipv4.addresses 192.0.2.10/24 ipv4.gateway 192.0.2.1 ipv4.dns "192.0.2.53 192.0.2.54" ipv4.dns-search example.com
nmcli connection modify ens192 connection.autoconnect yes
nmcli connection up ens192

The profile file is /etc/NetworkManager/system-connections/ens192.nmconnection. A second address: nmcli connection modify ens192 +ipv4.addresses 192.0.2.11/24.

  1. Ubuntu: write the netplan file (YAML: spaces, no tabs):
/etc/netplan/60-static.yaml
network:
  version: 2
  ethernets:
    ens192:
      dhcp4: false
      addresses:
        - 192.0.2.10/24
      routes:
        - to: default
          via: 192.0.2.1
      nameservers:
        addresses: [192.0.2.53, 192.0.2.54]
        search: [example.com]
chmod 600 /etc/netplan/60-static.yaml
netplan generate
netplan try

netplan try applies the change and rolls it back after 120 seconds if you do not press Enter — your safety net on a remote server. Remove or change the old file (often 50-cloud-init.yaml) so the two files do not fight.

  1. Stop cloud-init from writing the network again (cloud and VM images):
/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}
  1. Remote server? Work from the VM or cloud console if you can. Over SSH, change the address in one step (nmcli connection up, netplan try) and reconnect to the new IP.

15.3

Check that it worked

ip -4 addr show ens192
ip route
default via 192.0.2.1 dev ens192 proto static metric 100
getent hosts example.com
ping -c 2 192.0.2.1

Then reboot once (in a maintenance window) and check again — the address must stay.

15.4

Common problems

ProblemCauseFix
Old DHCP address still there next to the new oneipv4.method is still auto, or the DHCP lease is active.nmcli connection modify … ipv4.method manual, then nmcli connection up.
No internet, local network worksGateway missing or wrong.ip route; set ipv4.gateway / routes: to: default.
Names do not resolveNo DNS servers set.ipv4.dns / nameservers; check with resolvectl status (Ubuntu) or /etc/resolv.conf.
After a reboot the change is gone (Ubuntu)cloud-init or another netplan file overwrote it.Disable cloud-init networking; keep one netplan file.
Permissions for /etc/netplan/... are too openThe netplan file is readable by all.chmod 600 the file.
SSH froze after the changeThe address changed under your session.Reconnect to the new IP; use the console; next time netplan try.

15.5

Go further

1Which command makes a netplan change undo itself if you lose the connection?

netplan try — it rolls back after 120 seconds unless you confirm.

2Where does NetworkManager save the static address on RHEL 9 and 10?

In the connection profile /etc/NetworkManager/system-connections/NAME.nmconnection.

Alle commando's in dit hoofdstuk