Are you trying to figure out how to create a static IP on Alpine Linux? If so, read this article to know more.
Generally, the IP addresses are assigned using the dynamic host configuration protocol (DHCP). The DHCP automatically assigns an IP address to the client such that these clients or nodes become part of the network.
However, setting a static IP address, such as a web server, database, or FTP server, is sometimes useful.
In this article, we will discuss how to create a static IP on Alpine Linux.
Let’s get started!
Why do we need a static IP address on Alpine Linux?
You may want to have a static IP address on Alpine Linux for many reasons. And the following are examples of this:
- Alpine Linux may be configured as a dynamic host configuration protocol (DHCP) or KVM server to host multiple virtual machines (VM).
- Also, static IP address enables working with port forwarding, firewalling, and HTTP servers quite easily.
Configuring static IP address on Alpine Linux
Configuring a static IP address on Alpine Linux is an essential task for many users as it enables them to have a fixed IP address for their system.
A static IP address is required when you need a consistent IP address for your device or when using services requiring a fixed IP address. There are three basic steps to configure a static IP address on Alpine Linux:
- Check the current IP address.
- Configure the current IP address.
- Configure the DNS or name server IP.
How to Create a Static IP on Alpine Linux
Now, follow the steps below for the configuration part of how to create a static IP on Alpine Linux.
Check the current IP address
Before moving forward, it is essential to take note of your current IP address. Type the following on your Terminal:
$ ifconfig
You will see the current network interface eth0 and the IP address assigned to it.
Configure the current IP address
In this step, we will configure the current IP address. For that, we will display the network interfaces first. Then, we will edit the /etc/network/interfaces
file. Finally, we will restart the networking daemon on Alpine Linux.
Displaying the network interfaces
The configuration information for network interfaces is stored in /etc/network/interfaces
. Type the following command on the Terminal to display the network interfaces:
$cat /etc/network/interfaces
You will see the first configuration as a loopback address:
auto lo
iface lo inet loopback
The second configuration is the active network interface IP configuration.
auto eth0
iface eth0 inet dhcp
Note that the second configuration is set to use DHCP.
Disable the DHCP address for interface eth0
In this part, we will set a static IP of 192.168.2.110 with a gateway of 192.168.2.1. This is the router’s address. The first step we will do is to comment out the second configuration. Open the file /etc/network/interfaces
for editing using your favorite editor as follows:
$sudo vim /etc/network/interfaces
Then comment out the configuration as follows:
#auto eth0
#iface eth0 inet dhcp
Doing so will disable the DCHP addressing for the interface eth0.
Note: To comment, we use the # at the start of the line.
Edit the configuration file specifying the IP address and gateway
Now, we will edit the configuration file specifying the IP address and gateway considering the environment’s IP subnet as follows:
auto eth0
iface eth0 inet static
address 192.168.2.120/24
gateway 192.168.2.1
hostname linuxshelltips
Save the changes and exit. The following figure shows the final configuration:
Assign multiple IP addresses to the same network interface
You can also add different IP addresses to the same network interface. Suppose we have assigned an additional IP address to the interface eth0 as follows:
iface eth0 inet static
address 192.168.2.150/24
Then restart the networking daemon as follows:
$ service networking restart
You can then confirm that the configuration has changed with the following command:
$ ip a
Restart the networking daemon
Now, we will restart the networking daemon for the changes to take effect. Type the following command on the Terminal:
$ service networking restart
Note down the new IP address by typing the following command:
$ ifconfig
Or, you may type the following command:
$ ip a
You should see that the new IP address is now to Alpine Linux.
Note: To restart the networking daemon, you can use the following command:
# service networking restart
IPv6 static address configuration
Before moving forward, we will mention the configuration for IPv6. The syntax is as follows:
iface eth0 inet6 static
address ipv6-here/64
gateway ipv6-gw-here
pre-up echo 0 > /proc/sys/net/ipv6/conf/eth0/accept_ra
Configure DNS or name server IP
We must also configure DNS or name server IP in /etc/resolv.conf
file. Note that you don’t need to change anything if you already use DHCP configuration. Your entries should be something like below:
nameserver 192.168.2.1
The IP address mentioned here is the router’s IP. If you have changed anything, you can restart the networking daemon for the changes to take effect.
Configuring a static IP address on Alpine Linux is essential when you need a consistent IP address for your device or when using services that require a fixed IP address.
This article outlined the three basic steps of how to create a static IP on Alpine Linux: checking the current IP address, configuring the current IP address, and configuring the DNS or name server IP.
Following the steps in this guide, you can assign a static IP address on Alpine Linux and manage your network connections. If you have any questions on this topic, kindly leave a comment down below.
If this guide helped you, please share it.