(Linux Command Line) Part 4 - Commands Line for Network Configuration in Linux (CentOS, Ubuntu)
Linux is an open source Operating System, mainly used on servers and mainframes due to its high stability, secured and free. This is part 4 of the serial Linux Command Line – Commands Line for Network Configuration in Linux (CentOS, Ubuntu)
Serial Linux Commands:
π Part 1 – Linux Commands for Server Management and Monitoring
π Part 2 – Linux Commands for File and Folder/ Directory Management
π Part 3 – Linux Commands for File Editor in Linux (CentOS, Ubuntu)
π Part 4 – Linux Commands for Network Configuration in Linux (CentOS, Ubuntu)
π Part 5 – Other Useful Linux Commands (CentOS, Ubuntu) you need to know
Bonus: E-book “Total Linux Command Line“, Download Here
1) Commands Line for Network Configuration in Linux (CentOS, Ubuntu)
Ubuntu, CentOS, and other Linux operating systems consider a network card as a device. They save the configuration in the text file, then load it every time the system boots up. Each computer needs a wired or wireless Ethernet card, listed in the /dev directory with a name beginning with three letters Eth, for example, eth0 for the first network card, eth1 for the second network card…
The Network configuration files of CentOS and Ubuntu are usually different. For example, in Ubuntu, the network configuration file is stored in /etc/network/interface/ while in CentOS, it is stored in /etc/sysconfig/network- Scripts/
1.1. Commands for Checking network information in CentOS and Ubuntu
COMMAND | PURPOSE / DESCRIPTION |
ifconfig -a | grep eth | Check the total number of network cards in the system |
ifconfig | Check the configuration (MAC, IP address, gateway ..) of all the network card in the machine (similar to the IPCONFIG command in Windows) |
ifconfig card-name | Check the configuration of the network card named card-name (eg: ifconfìg eth0) |
route -n | Check the route of packets (similar to the ROUTE command in Windows) |
1.2. Commands for Network Configuration in CentOS and Ubuntu
COMMAND | PURPOSE / DESCRIPTION |
sudo ifconfig eth0 up | Enable the network card eth0 in Ubuntu |
sudo ifconfig eth0 down | Disable the network card eth0 in Ubuntu |
ifconfig card IP1 netmask IP2 | Set the network configuration for a network card. For example, to set the IP for eth0 to 192.168.1.100, netmask= 255.255.255.0 with this command: ifconfig eth0 192.168.1.2 netmask 255.255.255.0 Warning: This configuration will be reset/ deleted after the system re-start |
vi /etc/network/interfaces | Set up the network configuration file in Ubuntu with VI Text Editor |
vi /etc/sysconfig/network-scripts/ifcfg-eth0 | Set up the configuration for network card Eth0 in CentOS with VI Text Editor |
/etc/init.d/networking restart | Restart the network card in Ubuntu (to apply changes) |
service network restart | Restart the network card in CentOS (to apply changes) |
vi /etc/resolv | Set name servers for CentOS and Ubuntu. For example: nameserver 8.8.8.8 nameserver 8.8.4.4 |
route add default gw 192.168.1.1 | Set 192.168.1.1 as the default gateway in Ubuntu. In contrast to the command Add, we use the command Delete (for example: route delete default gw 192.168.1.1) |
route add -net 192.168.5.0 mask 255.255.255.0 dev eth0 | Add a static routing to the network (for the network card Eth0). In contrast to the command Add, we use the command Delete |
1.3. Other Command Line for Networking in CentOS and Ubuntu
COMMAND | PURPOSE / DESCRIPTION |
netstat | Display network connections (same in Windows) |
traceroute serverA | Trace packet routing to serverA |
nslookup | Query the domain server (name server) |
rlogin serverA | Connect to a remote system |
telnet serverA | Connect to a remote system (better interacting than rlogin) |
rcp fileX serverA | Copy a file (fileX) from the remote server (serverA) |
ftp | Transfer files between systems on a network |
rsh commandA | Run a command on a remote system without logging in |
ping serverA | Check connection to a remote system (same in Windows) |
lcd path | Change local folder when logged in on remote machine |
mesg y/n | Set options for other users to write messages for you |
write user | Send a message to another user |
talk user | Allow 2 people to chat with each other |
Now I will guide in detail how to use the above commands to configure the network for CentOS and Ubuntu
2) Guide how to Configure the network in CentOS and Ubuntu
2.1. Configure network in Ubuntu
Step 1: Open the configuration file with VI text editor
sudo vi /etc/network/interfaces
Your screen will display the network configuration like this:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.1.0
gateway 192.168.1.1
=> You can replace the “red text” above with your IP
If you want to use a dynamic IP from the DHCP server, we declare the following commands in the file /etc/network/interfaces:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
Step 2: Reboot or Restart the Networking service by using the following command:
/etc/init.d/networking restart
Step 3: In most cases, your network device will work well after step 2. But in some case, you have to config the Nameserver. Here’s how:
sudo vi /etc/resolv.conf
Enter the contents of the file as below then Save it
nameserver 8.8.8.8
nameserver 8.8.8.4
Done !!
2.2. Configure network in CentOS
Step 1: Use the command ifconfig to check your network cards
Step 2: Specify which network card to config (for example: Eth0), then open the configuration file with VI text editor
vi /etc/sysconfig/network-scripts/ifcfg-eth0
Your screen will display the network configuration like this:
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
BROADCAST=192.168.1.255
HWADDR=00:25:90:C3:65:BC
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
=> You can replace the “red text” above with your IP
If you want to use a dynamic IP from the DHCP server, we need to change the file contents to:
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=DHCP
DNS1=8.8.8.8
Step 2: Reboot or Restart the Networking service by using the following command:
/etc/init.d/networking restart
All done !!
Feel free to leave a comment if you need any help.
Source: https://easytipz.com/computer-tips/linux-command-line-part-4-commands-line-for-network-configuration-in-linux-centos-ubuntu/
No comments: