Configuring a static IP address on a Linux machine involves modifying network configuration files to set a specific IP address, subnet mask, gateway, and DNS servers. The exact steps can vary slightly depending on the Linux distribution you are using, but the general process involves editing relevant configuration files. Here’s a general guide:
Note: You need administrative privileges (such as root or using sudo
) to make these changes.
- Identify Network Interface:
Determine the name of the network interface you want to configure. Common interface names includeeth0
,enp0s3
,ens33
, etc. You can use theifconfig
orip addr
command to list available interfaces. - Edit Network Configuration File:
Locate and edit the network configuration file for your chosen network interface. The file names and locations can vary based on your Linux distribution:
- Debian/Ubuntu (using
nano
orvim
):sudo nano /etc/network/interfaces
- RedHat/CentOS (using
nano
orvim
):bash sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
- Configure the IP Address:
In the configuration file, modify the lines related to IP configuration. Replace the values with your desired static IP address, subnet mask, and gateway. For example:
# Debian/Ubuntu
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
# RedHat/CentOS
DEVICE=eth0
BOOTPROTO=none
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
- Set DNS Servers:
Add DNS server addresses to the configuration file to enable hostname resolution. Add lines likeDNS1
andDNS2
:
# Debian/Ubuntu
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
# RedHat/CentOS
DEVICE=eth0
BOOTPROTO=none
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
- Restart Networking:
After editing the configuration file, restart the network service to apply the changes:
- Debian/Ubuntu:
sudo systemctl restart networking
- RedHat/CentOS:
bash sudo systemctl restart network
- Test Connectivity:
Test the connectivity using the configured static IP address and verify that you can access resources on the network and the internet.
Keep in mind that the specific steps and file locations can vary depending on the Linux distribution and version you are using. Always double-check the documentation for your distribution to ensure accurate configuration.