Skip to main content

Setting Static IPs

Run the command ip link or ifconfig on the system to find out the Ethernet adapter name

#Debian systems comes with ip link by default
ip link

#ifconfig is not part of the default Debian install
ifconfig

Screenshot 2022-09-29 002707.png

Navigate and edit the /etc/network/interfaces file using your preferred text editor:

sudo nano /etc/network/interfaces

Screenshot 2022-09-29 003142.png

In the block where your Ethernet interface name is at, change allow-hotplug to auto, if the line exists, and add your static IP information in the following format:

auto ens192
iface ens192 inet static
address 192.168.1.25
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 1.1.1.1 1.1.0.0

Difference between allow-hotplug and auto is that auto brings the interface up at boot, while allow-hotplug can start messing with static ip configurations, and even assign itself 169 local address.

Screenshot 2022-09-29 003857.png

Save, and reboot.

Below is a simple Bash script I created for setting static IP. It can be combined with other scripts to automate initial server setup.

#!/bin/bash

#This is a simple script for setting static IP

ip -br l | awk '$1 !~ "lo|vir|wl" { print $1}'

echo -e "\n"

TEMP1=true
while $TEMP1
do
    echo "Enter the name of the ethernet adapter, shown above (ens,et0,etc):"
    read adaptername
    echo "Enter the name of the ethernet adapter again:"
    read adaptername2

    if [ "$adaptername" == "$adaptername2" ]
        then
            TEMP1=false
            echo -e "\n"
    else
        echo "Adapter Name did not match. Try Again."
    fi
done


TEMP1=true
while $TEMP1
do
    echo "Enter the new Static IP Address:"
    read staticip
    echo "Enter the new subnet mask (255.255.255.0):"
    read subnetmask
    echo "Enter the new Gatway address:"
    read gateway
    echo "Enter the new DNS Servers. Use space to seperate multiple DNS Server, such as 1.1.1.1 8.8.8.8 :"
    read dnsservers


    echo -e "You have enter the following information:\nStatic IP: $staticip\nSubnet Mask: $subnetmask\nGateway: $gateway\nDNS Server(s): $dnsservers\n"

    echo "Is the above correct? (y/n)"
    read temp2

    if [ "$temp2" == "y" ]
        then
            TEMP1=false
    elif [ "$temp2" == "n" ]
        then
            echo -e "Start over\n"
    else
        echo -e "Invalid Response. Try Again.\n"
    fi
done

sudo sed -i '/$adaptername2/,$d' /etc/network/interfaces
sudo echo -e "auto $adaptername2\niface $adaptername2 inet static\naddress $staticip\nnetmask $subnetmask\ngateway $gateway\ndns-nameservers $dnsservers\n" >> /etc/network/interfaces