How to assign a static IP to a Linux machine on a local network

Post Reply
User avatar
isscbta
Team Member
Posts: 131
Joined: Mon Jul 19, 2021 1:41 am
Has thanked: 16 times
Been thanked: 3 times

First, check the IP address assigned to the Linux machine by DHCP:

Code: Select all

# ifconfig 
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 111.111.111.111  netmask 255.255.255.0  broadcast 111.111.111.255
        inet6 fe80::219:99ff:fef5:c33e  prefixlen 64  scopeid 0x20<link>
        ether 00:19:99:f5:c3:3e  txqueuelen 1000  (Ethernet)
        RX packets 21179  bytes 3719145 (3.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26980  bytes 8981209 (8.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
From this, we find out that it is: 111.111.111.111

Now, let's look at the current version of the /etc/network/interfaces file.

Code: Select all

# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug enp2s0
iface enp2s0 inet dhcp
Let's modify it to be:

Code: Select all

# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug enp2s0
# iface enp2s0 inet dhcp # *** COMMENT THIS LINE ***

# *** ADD THIS BLOCK FOR STATIC IP ***
auto enp2s0 # NETWORK NAME (enp2s0) IS IMPORTANT, WE TOOK IT FROM PREVIOUS BLOCK
iface enp2s0 inet static # NETWORK NAME AGAIN
 address 111.111.111.111 # OUR IP THAT WE SAW A FEW MINUTES AGO
 netmask 255.255.255.0
 gateway 111.111.111.1 # THE SAME AS IP JUST ENDED WITH .1
 dns-nameservers 8.8.8.8
At the end:

Code: Select all

reboot
Post Reply