How to Give an Eth Static IP on Linux
Introduction
In this article, we will guide you through the process of giving an eth static IP on Linux. A static IP address is a fixed IP address that remains the same for a specific period of time. This is useful for applications that require a specific IP address, such as remote access or backup servers. In this article, we will cover the steps to give an eth static IP on Linux using various methods.
Method 1: Using the Network Manager
The Network Manager is a built-in tool in Linux that allows you to manage network connections and IP addresses. Here’s how to give an eth static IP using the Network Manager:
- Open the Network Manager by searching for it in the Applications menu or by typing
nmcliin the terminal. - Select the network interface you want to configure (e.g. eth0).
- Click on the "Edit" button to open the network configuration editor.
- Click on the "IP" tab and select the "Static" option.
- Enter the IP address, subnet mask, gateway, and DNS server addresses.
- Click on the "Apply" button to save the changes.
Method 2: Using the command line
You can also give an eth static IP using the command line. Here’s how:
- Open a terminal and type
ip addr add 192.168.1.100/24 dev eth0to add a new IP address to the eth0 interface. - Type
ip route show defaultto view the default route. - Type
ip route add default via 192.168.1.1 dev eth0to add a new route to the eth0 interface. - Type
ip addr show eth0to view the current IP address and subnet mask.
Method 3: Using a script
You can also give an eth static IP using a script. Here’s an example script that adds a new IP address to the eth0 interface:
#!/bin/bash
# Get the IP address and subnet mask from the user
IP_ADDRESS=$(ip addr show eth0 | grep "inet " | awk '{print $2}' | cut -d/ -f1)
SUBNET_MASK=$(ip addr show eth0 | grep "inet " | awk '{print $2}' | cut -d/ -f1)
# Add the IP address to the eth0 interface
ip addr add $IP_ADDRESS/24 dev eth0
# Add a new route to the eth0 interface
ip route add default via 192.168.1.1 dev eth0
Method 4: Using a tool like ipaddr
ipaddr is a tool that allows you to manage IP addresses and network connections on Linux. Here’s how to use ipaddr to give an eth static IP:
- Open a terminal and type
ipaddr -i eth0to view the current IP address and subnet mask. - Type
ipaddr -i eth0 add 192.168.1.100/24to add a new IP address to the eth0 interface. - Type
ipaddr -i eth0 route add default via 192.168.1.1to add a new route to the eth0 interface.
Method 5: Using a tool like dhclient
dhclient is a tool that allows you to manage network connections and IP addresses on Linux. Here’s how to use dhclient to give an eth static IP:
- Open a terminal and type
dhclient -rto stop the dhclient service. - Type
dhclient -sto start the dhclient service. - Type
dhclient -i eth0to add a new IP address to the eth0 interface. - Type
dhclient -rto stop the dhclient service.
Conclusion
Giving an eth static IP on Linux is a straightforward process that can be done using various methods. The Network Manager, command line, script, ipaddr, and dhclient tools are all available options to manage IP addresses and network connections on Linux. By following the steps outlined in this article, you can easily give an eth static IP on your Linux system.
Tips and Variations
- You can use the
ip addrcommand to view the current IP address and subnet mask of the eth0 interface. - You can use the
ip routecommand to view the default route and any additional routes added to the eth0 interface. - You can use the
ip addrcommand to add a new IP address to the eth0 interface using theaddoption. - You can use the
ip routecommand to add a new route to the eth0 interface using theaddoption. - You can use the
ip addrcommand to view the current IP address and subnet mask of the eth0 interface using theshowoption. - You can use the
ip routecommand to view the default route and any additional routes added to the eth0 interface using theshowoption.
