There are lots of times when you need a static IP on a machine — especially virtual machines or server systems. It’s pretty simple on a Debian system and can make your life much easier on Debian VMs so you can set up name resolution between the host and guest. Regardless of your reasons, 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’):

ip a

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:

cd /etc/network
 ls -lA
cp interfaces interfaces.old

Now, let’s take a quick look at the file:

cat interfaces

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

Keyword
Meaning
allow-hotplug
wait for device detection and then setup IP links
auto
try to setup the device at boot time

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:)

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 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! I’m using the same setup as in my ‘Easy internet access for Hyper-V guests using NAT’ article for this example. If you need to set up IPv6, then you can add a section like:


# 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. If you use DHCP6 then use:

iface eth0 inet6 dhcp

If you use SLAAC (stateless autoconfiguration) then use:

iface eth0 inet6 auto

You can also mix elements but that’s beyond the scope of this article. Save this file and you’re all set.

Name resolution

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):

nano /etc/resolv.conf

It should look something like this (assuming you used the Default Switch in a Hyper-V environment, for example):


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 8.8.8.8 and 8.8.4.4. You can add multiple nameservers like this:


domain mydomain.net
search mydomain.net
nameserver 8.8.8.8
nameserver 8.8.4.4
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.

Confirming hostname

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).

nano /etc/hostname

Just update this one-liner and save the file. That’s it. Let’s change the domain now:

nano /etc/hosts

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

Save it 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’.

reboot

Quick reminder…

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.

Final thoughts

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? Suggestions? Want to add your tips? Things you want me to cover in a future article? Comment below!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu