Set static IP address on Debian Linux

he procedure is as follows to set up and configure a static IP information:

  1. Open the terminal application.
  2. Log in to remote or server using ssh command.
  3. Backup /etc/network/interfaces file running sudo cp /etc/network/interfaces /root/
  4. Edit the /etc/network/interfaces
  5. Configure static IP address for enp0s5 Ethernet interface: address 192.168.2.249
  6. Add subnet mask: netmask 255.255.255.0
  7. Set up default gateway IP: gateway 192.168.2.254
  8. Finally add DNS resolver IP: dns-nameservers 192.168.2.254 8.8.8.8 1.1.1.1

Let us see all commands and examples in details.

Finding your network interfaces name on Debian

Show available ethenet network interfaces:

1
2
3
4
5
6
7
root@debian:/etc/network# ip -c link show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:1d:62:ad brd ff:ff:ff:ff:ff:ff
altname enp2s1

Show the current IP address assinged to that network interface:

1
2
3
4
5
6
7
8
root@debian:/etc/network# ip -c addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:1d:62:ad brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 2.1.1.131/24 brd 2.1.1.255 scope global dynamic ens33
valid_lft 1322sec preferred_lft 1322sec
inet6 fe80::20c:29ff:fe1d:62ad/64 scope link
valid_lft forever preferred_lft forever

Configuring Static IP on Debian

The /etc/network/interfaces[/file] contains network interface configuration information for Debian Linux.

1
nano /etc/network/interfaces

Look for the primary network interface ens33, remove dhcp and allow-hotplug lines.

1
2
3
4
5
6
7
8
9
10
11
12
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens33
iface ens33 inet static
address 2.1.1.129
netmask 255.255.255.0
gateway 2.1.1.2
#dns-domain sweet.home
dns-nameservers 2.1.1.2

Restart networking service on Debian

1
2
systemctl restart networking.service
systemctl status networking.service

Check the new IP address assigned on Debian

1
2
3
4
5
6
7
8
9
10
ip -c addr show
ip -c addr show ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:1d:62:ad brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 2.1.1.129/24 brd 2.1.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1d:62ad/64 scope link
valid_lft forever preferred_lft forever

Another Way for All Linux Distros (*)

1
2
3
4
5
6
7
8
9
10
11
12
# Get a listing of network interfaces
nmcli device
# list here.. say, there's an ens33

# nmcli connection modify ens33 ipv4.gateway, ipv4.dns, ...
nmcli connection modify ens33 ipv4.addresses 2.1.1.130/24
nmcli connection modify ens33 ipv4.gateway 2.1.1.2
nmcli connection modify ens33 ipv4.dns 2.1.1.2
nmcli connection modify ens33 ipv4.method manual

# apply these settings right away!
nmcli connection down ens33 && nmcli connection up ens33

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!