# How to Configure a Network in Linux: Static IP & DHCP Setup Guide
Setting up networking is one of the first things you’ll do on a fresh Linux server. Whether you’re deploying a web server, a VPS, or a local virtual machine, a correct network configuration is what makes the box reachable. This guide walks through configuring the network on Linux using the classic `/etc/network/interfaces` file, covering both DHCP autoconfiguration and static IP assignment with DNS and gateways.
> **Quick note on recency:** The `/etc/network/interfaces` method (managed by the `ifupdown` package) is native to Debian and Debian-derived distributions such as Ubuntu (older releases) and Kali. Modern Ubuntu releases and some other distributions now default to **Netplan** or **NetworkManager**. If your system does not have an `/etc/network/interfaces` file or it appears to be ignored, check whether Netplan (`/etc/netplan/*.yaml`) or NetworkManager is managing your interfaces instead. Always back up your configuration before making changes.
—
## Before You Start: A Pre-Configuration Checklist
Technical tutorials fail most often when commands are copied verbatim without checking the local environment. System versions, network managers, interface names, and permission levels all affect the outcome. Before touching any config file, do the following:
– **Confirm your OS and network stack:** Know your distribution and version (for example, Debian 11/12 vs. Ubuntu 22.04/24.04). This determines whether `ifupdown`, Netplan, or NetworkManager is in charge.
– **Verify permissions:** Editing files under `/etc/` requires root or `sudo` access. Confirm you can elevate before you begin.
– **Back up the current configuration:** Copy the existing file so you can restore it if something breaks. A simple `cp /etc/network/interfaces /etc/network/interfaces.bak` is enough.
– **Test after each change:** Apply one modification at a time and verify connectivity. Don’t stack five edits and then try to debug a broken connection all at once.
—
## Step 1: View Your Network Interfaces
Before configuring anything, you need to know what network interfaces your system has and what they’re called. On modern Linux, interface names follow a predictable naming scheme (such as `enp0s3`, `ens33`, `eth0`) rather than the older `eth0`/`eth1` convention.
Run:
“`bash
ip link show
“`
A typical output looks like this:
“`
1: lo:
2: enp0s3:
3: enp0s8:
“`
Here, `lo` is the loopback interface (internal, always present), while `enp0s3` and `enp0s8` are physical or virtual network adapters. Note the exact names — you’ll use them in the configuration file.
—
## Step 2: Open the Configuration File
The main configuration file for `ifupdown` is `/etc/network/interfaces`. Open it with a text editor such as `nano`:
“`bash
sudo nano /etc/network/interfaces
“`
A default file usually looks like this:
“`
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
“`
The `source` line tells the system to also load any additional config files placed in `/etc/network/interfaces.d/`. The `lo` block brings up the loopback interface at boot. Leave these entries intact — they’re required for the system to function correctly.
—
## Step 3: Configure an Interface with DHCP
If your network has a DHCP server (most home routers, office networks, and cloud providers do), you can let the interface obtain its IP address automatically at boot. Add the following block to the file:
“`
auto enp0s3
iface enp0s3 inet dhcp
“`
– `auto enp0s3` — bring the interface up automatically at boot time.
– `iface enp0s3 inet dhcp` — configure `enp0s3` as an IPv4 interface that requests its address via DHCP.
This is the simplest setup and works well when you don’t need a fixed address. The DHCP server assigns the IP, subnet mask, default gateway, and DNS resolvers for you.
—
## Step 4: Configure a Static IP Address
For servers, you’ll usually want a static IP so the address doesn’t change between reboots. A static configuration requires you to specify the IP address, subnet mask, default gateway, and DNS nameservers explicitly.
Here’s an example for the `enp0s8` interface:
“`
auto enp0s8
iface enp0s8 inet static
address 192.168.1.102
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
“`
### What each line does
| Directive | Purpose |
|—|—|
| `auto enp0s8` | Brings the interface up at boot. |
| `iface enp0s8 inet static` | Sets the interface to use a manually assigned IPv4 address. |
| `address` | The static IP address assigned to this interface. |
| `netmask` | The subnet mask defining the network segment. `255.255.255.0` gives you a /24 network (254 usable addresses). |
| `gateway` | The default router that forwards traffic to other networks (typically your router’s IP). |
| `dns-nameservers` | The DNS resolver addresses used for name resolution. The example uses Google’s public DNS (`8.8.8.8` and `8.8.4.4`). |
Replace the example values with addresses that match your actual network. Using an IP that’s already in use by another device will cause a conflict and intermittent connectivity.
> **DNS note:** On some systems the `dns-nameservers` directive is processed by the `resolvconf` package. If DNS resolution still fails after applying the config, check `/etc/resolv.conf` or install `resolvconf` (`sudo apt install resolvconf`) so the `ifupdown` DNS settings are applied correctly.
—
## Step 5: Apply the Configuration
After saving the file, restart the networking service or bring the interface down and back up:
“`bash
sudo systemctl restart networking
“`
Or, to restart a single interface:
“`bash
sudo ifdown enp0s8 && sudo ifup enp0s8
“`
> **Caution:** If you’re connected to the server over SSH through the interface you’re reconfiguring, restarting networking may drop your session. Run these commands from a console session, via a recovery console, or on an interface you aren’t actively using for the connection.
Verify the new settings with:
“`bash
ip addr show enp0s8
“`
And test connectivity:
“`bash
ping -c 4 192.168.1.1 # gateway
ping -c 4 8.8.8.8 # external IP
ping -c 4 example.com # DNS resolution
“`
If the ping to the gateway works but external IPs fail, the problem is likely the gateway or routing. If external IPs work but domain names don’t resolve, the issue is DNS.
—
## Troubleshooting Approach
When something doesn’t work, resist the urge to search the entire error message at once. Instead, isolate the layer where the failure occurs:
1. **Link layer** — Is the interface up? (`ip link show`)
2. **IP layer** — Does the interface have the correct address? (`ip addr show`)
3. **Routing** — Can you reach the gateway? (`ping
4. **External connectivity** — Can you reach a public IP? (`ping 8.8.8.8`)
5. **DNS** — Can you resolve a hostname? (`ping example.com` or `nslookup example.com`)
6. **Service firewall** — Is `ufw`, `iptables`, or your provider’s firewall blocking the port?
Logs are more reliable than trial and error. Check `journalctl -u networking` or `dmesg | tail` for clues after applying changes. Breaking the problem into layers is faster than repeatedly re-running the full configuration hoping for a different result.
—
## Follow-Up Maintenance: Document Everything
A successful configuration isn’t the end of the job. Servers get migrated, plugins get upgraded, and network layouts change. To save yourself time later:
– **Record key commands** — Keep the exact directives and values you used.
– **Note modified files** — List every file you changed, with timestamps.
– **Save verification results** — Capture the `ip addr` and `ping` outputs that confirm the setup works.
– **Keep your backup** — Leave the `.bak` copy in place until the new config has run stably for a while.
These notes become invaluable when you migrate to a new server, add a second NIC, or hand the system off to someone else.
—
## Summary
Configuring a network on Linux through `/etc/network/interfaces` comes down to four steps: identify your interfaces, open the config file, choose DHCP or static addressing, and apply the change. DHCP is easiest for dynamic environments, while a static IP gives servers the stable address they need. Either way, back up first, apply one change at a time, and verify each layer — link, IP, routing, and DNS — as you go.
If your distribution uses Netplan or NetworkManager instead of `ifupdown`, the same logic applies: identify the interface, declare DHCP or static settings, and restart the network service. The syntax differs, but the underlying network concepts are the same.










