It is important to know how to configure your network settings through the shell terminal. Especially when it comes to hosting a server that is facing the internet, as well as port forwarding to the server through the LAN network. Here, we are going to be configuring our network interfaces in Debian.
In a Linux desktop, it is easier to make changes in the settings of your operating system. Most times, you will want to do that in shell since your server is going to be headless(shell only, no desktop).
Fortunately for Debian, configuring the network interfaces is nothing more than simply editing a certain configuration file located in /etc/network/interfaces.
Configuring the network in Debian
We are going to be opening the file interfaces, which is located under /etc/network/. Feel free to use any text editor as you see fit, we will be using nano for this guide.
Note: The name of your network interfaces may vary by computer/NIC manufacturer.
sudo nano /etc/network/interfaces
Here, you will see that it has already been configured during setup for DHCP. This is only for users running a headless computer. A desktop environment may have it configured elsewhere such as via Network-Manager in Debian/Ubuntu. This file contains the following:
- auto eth0 – This tells the network interface eth0 to start upon system boot. Your ethernet cable must be plugged into this interface in order to connect at startup.
- allow-hotplug eth0 – Basically allows the interface eth0 to turn on when something occurs such as plugging in an ethernet cable to the port.
- iface eth0 inet dhcp – Configures the eth0 interface to pull an IP address configuration from the DHCP server in our LAN network.
Static IP Address
In the interfaces file, you can also configure a static IP address. Nowadays it is important to configure one for servers running behind a router so that incoming ports are always forwarded to it.
In order to configure a static IP, simply make the following changes to your current configuration:
auto eth0 allow-hotplug eth0 iface eth0 inet static address 192.168.1.17 netmask 255.255.255.0 gateway 192.168.1.1
We have now defined our own address, netmask, and gateway/router to connect to. Go ahead and save the configuration by doing CTRL+O and CTRL+X to exit nano.
Next we need to apply our changes made to the interface by restarting it:
sudo ifconfig eth0 down && sudo ifconfig eth0 up
Now check to see if your new address has been applied successfully:
sudo ip addr
You should see your newly-reflected address under your network interface.