Set a Static IP

This page will explain how to set a static IP in Linux.

  1. Get the network adaptor

    ip link
    

    Find the adaptor named ethx, ensx or something similar.

  2. Get IP and subnet mask (and more). Use the adaptor from the previous step, for example eth0.

    nmcli dev show eth0
    
    ifconfig eth0
    
  3. cd into /etc/netplan and run ls to see the config files.

    cd /etc/netplan
    ls
    
  4. Open the file named 01-netcfg.yaml (or something similar) using nano as root user:

    sudo nano 01-netcfg.yaml
    
    01-netcfg.yml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    network:
    version: 2
    ethernets:
        eth0:
        dhcp4: no
        addresses:
            - 192.168.1.100/24
        gateway4: 192.168.1.1
        nameservers:
            addresses:
            - 8.8.8.8
            - 1.1.1.1
    
    • Change the address 192.168.1.100/24 to the desired IP.
    • Change eth0 to the name of your network adaptor.
  5. Apply the changes:

    sudo netplan apply
    
  6. Check if it worked:

    ip addr show eth0