Configure Network Interfaces with Configuration Files on Linux?
This post will show you some basic steps to configure your network interface in Linux using configuration files. This skill is very useful, especially when the GUI does not work anymore. You can use some text editor in linux like VIM to edit the files. It’s easy.
Example of commands are applied for RHEL and Debian based Linux OS.
Here are 3 steps:
1. Locate the network configuration file
- RHEL/CentOS/Fedora: each interface has it own configuration file like
/etc/sysconfig/network-scripts/ifcfg-<interface_name>
NB: interface_name: as usual it would be like eth0, eth1, etc.
- Debian/Ubuntu:all the interfaces in computer have ONE common configuration file:
/etc/network/interfaces
2. Determine how IP address is assigned
- DHCP (Dynamic Host Configuration Protocol): IP address will be assigned automatically by DHCP server (in a home/small network, DHCP server is your Router)
- RHEL/CentOS/Fedora: edit configuration file located in Step 1 to:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
- Debian/Ubuntu: add below lines to configuration file located in Step 1
auto eth0
iface eth0 inet dhcp
- Static IP (mean you have to choose a properly IP address and configure it to your PC):
- RHEL/CentOS/Fedora: edit configuration file located in Step 1 to:
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.200
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes
- Debian/Ubuntu:add below lines to configuration file located in Step 1
auto eth0
iface eth0 inet static
address 192.168.1.200
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
I think all lines are quite clear. If you want to know what exactly they are, please visit the 2 links I paste at the end of this post.
3. Add DNS configuration (only when Static IP used in Step 2)
-
Locate file /etc/resolv.conf
-
Add these lines to the file
nameserve 208.67.222.222 - example IP address of primary name server
nameserver 208.67.220.220 - example IP address of secondary name server
4. Restart network service for applying new configuration
- RHEL/CentOS/Fedora: use the following command as root user
/etc/init.d/network restart
- Debian/Ubuntu: you might need to enter root password
sudo /etc/init.d/networking restart
It’s done. Start pinging google.com to check your network is working.
For more information, please visit 2 links:
http://www.yolinux.com/TUTORIALS/LinuxTutorialNetworking.html#CONFIGFILES http://www.redhat.com/docs/manuals/linux/RHL-8.0-Manual/ref-guide/s1-networkscripts-interfaces.html