There are lots of times when you need a static IP, especially for server systems. It’s pretty simple on Debian, we only have to edit a few files and run a few simple commands.
Static IP setup
In order to set a static IP, we need to get to the guts of your networking. First, let’s get the name of your network adapter (in most cases on VMs it is ‘eth0’):
This will display your current IP environment. You should see ‘lo’ as your first adapter, that’s your loopback interface. After that, you’ll see your other adapters. In most cases you’ll only have one other adapter and it will be listed as ‘2’. As I said before, on a virtual machine it’s probably ‘eth0’. On a physical machine, it could be something a little more exotic. Whatever it is, remember the name for a few minutes.
Debian uses a simple text file to define your network options. We are going to edit that file after making a backup:
# change your network configuration directory cd /etc/network # check out the files that live here ls -lA # make a backup of the interface configuration file cp interfaces interfaces.old
Now, let’s take a quick look at the file, type
At the bottom of that file, you’ll see a section like this:
allow-hotplug eth0 iface eth0 inet dhcp
Recognize that ‘eth0’ from earlier? If it’s named something else, that’s alright, it’s just important that it’s the same name as you found earlier. We’re going to edit this file a little and create a more flexible setup. Open the interfaces file in whatever editor you like. We’re going to delete the DHCP instruction, add one line and clean up the comment so the file looks like this (obviously, change ‘eth0’ to match your setup):
# The primary network interface (eth0) allow-hotplug eth0 auto eth0
|allow-hotplug||Wait for device detection and then set up IP|
|auto||Try to set up the device at boot|
On line 4, do you see the ‘source’ directive? That means the OS should read any files in that path in addition to this file. We will take advantage of that and create a file for our static configuration separate from this main file. Save and exit your interfaces file then type the following at the prompt (you don’t have to use nano as your editor, but I will for this example):
cd interfaces.d nano eth0.static
This opens nano with a new file called eth0.static which, I think, is a pretty self-explanatory filename. If your adapter is named something else, go ahead and choose a different filename. Let’s make the file look like this:
### Static configuration for eth0 # IPv4 iface eth0 inet static address 10.0.1.100 netmask 24 # you could also use decimal notation (255.255.255.0) gateway 10.0.1.254
The settings here are quite clear, so adjust as necessary for your environment. If your adapter is not called ‘eth0’ make sure you change that! If you need to set up a static IPv6, then you can add a section like this after the one we just added:
# IPv6 iface eth0 inet6 static address fde5:a3af:f4b6:0c16::1234 netmask 64 gateway fde5:a3af:f4b6:0c16::ffff
Obviously you should adjust this for your environment. For example, if you use DHCP6 then use:
iface eth0 inet6 dhcp
If you use SLAAC (stateless auto-configuration) then use:
iface eth0 inet6 auto
You can also mix elements too, but that’s beyond the scope of this article. Save this file and you’re all set.
DHCP sets your /etc/resolv.conf file automatically for you, but you need to change that in a static environment. Open the file in nano (or your editor of choice) by typing
It should look something like this (in this case, assuming you used the Default Switch in a Hyper-V environment):
domain mshome.net search mshome.net nameserver 172.26.78.193
You’ll want to change these values to match your networking environment. Your nameserver is your network’s DNS server or you can use Google’s servers, for example, at 188.8.131.52 and 184.108.40.206. You can add multiple nameservers like this:
domain mydomain.net search mydomain.net nameserver 220.127.116.11 nameserver 18.104.22.168 nameserver fdee:ab14:d0c3:d4ff::255
In all cases when you’re moving to a different network, you need to change the domain and search domain lines to match your new network environment. Make your changes and save the file.
Very often, moving to a static configuration means that you have a particular domain of which you will now be part. Your computer name should reflect that. Let’s check our current computer name and domain, respectively:
hostname hostname -f
If these are all good, then you have nothing else to do. If you need to change them, then we can easily do so. Let’s start with the computer name (hostname).
Just update this one-liner and save the file. That’s it. Let’s change the domain now:
You’ll see a line like this:
127.0.1.1 mycomputer.mydomain.tld mycomputer
Just change that to reflect your new domain, let’s say ‘newdomain.net’:
127.0.1.1 mycomputer.newdomain.net mycomputer
You’ll find a similar situation for the IP6 section of the hosts file, assuming you are using/care about IP6, and you can edit it in the same way. Now, save the and that’s all. Actually, you can verify the hostname and domain changes right away just like we did before:
hostname hostname -f
This is a really good time to reboot and make sure all your settings ‘stick’.
Now that your system is using a static IP address, remember to update your hosts file and/or DHCP and DNS entries on this system and on other systems as needed! Also, if you have sshd setup and running on your server (see my article) then you might want to look at adding ‘ListenAddress’ directives to reflect your static IP(s) in your sshd_config file and thus, tighten up your security.
That was pretty painless. Your system now has a static IP address and the proper fully-qualified domain name (FQDN).
Thanks for reading my techie-thoughts on this issue. Have any comments or suggestions? Want to add your tips? Things you want me to cover in a future article? Comment below!