Setting Debian 9 static IP is not too different from the previous version. You might find the network cards named a bit differently now. Apart from that, it is mostly the same. And we will look at an alternative at the end, and an issue while configuring Debian 9 static IP.
Debian 9 Static IP
Edit network interfaces
Let’s look at how to give your Debian 9 a static IP address. The first thing you need is to edit the network interfaces. Launch your favorite text editor and edit /etc/network/interfaces. It will look something like below. In this example, I’m using a wired network interface. It will now be called ens3 instead of eth0 like in earlier versions. Your interface might have a different name.
You need to change DHCP to static. Then you need to add the address, gateway, and netmask. In this example, I used 192.168.2.86 for address and 1918.104.22.168 for the gateway (because that is the address of my gateway). Most likely your settings will be different. Change them to fit your network. After editing the network interfaces my config looks like this after setting Debian 9 static IP.
Apply the Debian 9 static IP
To apply the changes we need to restart the network service. If you are connected to the computer via SSH, you will need to reboot. You will lose connection when restarting the network service if you are on SSH. If you are local or a KVM console restart your interface like below.
To restart the network service just type into the terminal or console: service networking restart (typing /etc/init.d/networking restart will also do). If you at this point check your network settings with ifconfig or ip addr, the network will be unreachable. To fix this run: ifup ens3 (or whatever your interface is called). Now run: ip addr, to verify your new settings.
Here I noticed my new static IP had become a secondary IP. I don’t want that. I only need 1 IP for this machine. At this point, I can use both 192.168.2.86 and 192.168.2.59 (which was the IP given by the DHCP server). I just rebooted to remove the DHCP assigned IP address.
Another option to static IP addresses
There is another option to a static IP address. Your router, if it is not too old and simple, has an option to assign IP addresses to specific mac addresses. On my wifi router, also used as a DHCP server it looks like this.
This is an inexpensive Asus RT-N66U router. But most wifi routers have this option to assign mac addresses to IP addresses. I prefer using Debian 9 static IP addresses instead of assigning them to the router.
Regarding Ubuntu Static IP is mostly a straight forward thing. I wouldn’t think it should be needed on a rented VPS. As they mostly get their IP addresses assigned. Anyway, here is how we do Ubuntu Static IP.
First we need to edit the network interface. sudo vim /etc/network/interfaces
Change the line: iface eth0 inet dhcp
to: iface eth0 inet static
And then add (in this example we use the ip 192.168.1.151) address 192.168.1.151 gateway 192.168.1.1 netmask 255.255.255.0 network 192.168.1.0 # Optional broadcast 192.168.1.255 # Optional
To exit VIM, press ESC and then type wq and then press enter to save the file.
Now your network interface is configured to use a static ip. It is still not active. To activate the static ip you will need to reboot or restart the network interface. In Ubuntu this will have to be done like this: sudo ifdown eth0 && sudo ifup eth0
Take notice of the both sudo. And you need to issue both commands at once if your are doing this via ssh or any other remote terminal. If not the network interface will go down, and so will your connection.