Setting a Static IP Address on a Linux Machine
In the world of Linux networking, there are times when you want your machine to have a stable, unchanging IP address. This is known as a static IP address.
In this guide, we’ll walk you through the steps to set up a static IP address on a Linux machine.
Steps:
- Determine Your Network Interface
- Edit Network Configuration
- Configure the Static IP Address
- Apply the Configuration
- Verify the Configuration
Step 1: Determine Your Network Interface
Determine the network interface you want to configure
#Run either of these commands to list the available network interfaces
ifconfig
ip a
The primary one is often named something like eth0
(for Ethernet) or wlan0
(for Wi-Fi). You’ll need this information for the next steps.
Step 2: Edit Network Configuration
Now that you know your network interface, it’s time to open and edit the network configuration file for that interface. You’ll find these configuration files in the /etc/netplan/
directory. You can use a text editor like nano
or vi
to edit the file.
sudo nano /etc/netplan/01-netcfg.yaml
Step 3: Configure the Static IP Address
Inside the configuration file, you’ll see YAML code defining your network settings. By default, it might look something like this:
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
dhcp4: yes
To set a static IP address, you’ll need to change the dhcp4: yes
line. Here’s an example of what your configuration might look like for a static IP address:
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
addresses: [192.168.1.10/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
addresses
: Set your desired static IP address and subnet mask.gateway4
: Set your router’s IP address as the default gateway.nameservers
: Set DNS servers.
Step 4: Apply the Configuration
Apply the changes you made to the network configuration using the following command:
sudo netplan apply
Step 5: Verify the Configuration
To ensure that your Linux machine now has a static IP address, use the ip a or ifconfig command in the terminal. This will display the network configuration for your chosen interface, and you should see your newly configured static IP address.