Day 40 - Networking in Linux

Last eight posts were all about the basics of Linux, working with the command-line, handling User, Groups and Files and Permissions and all other things that you need to know to work with Linux. This post will be about Basic Networking Commands in Linux. I already have wrote a series on Nerworking Basics from Day24 to Day31 which you can be found on This Page. Now to the networking commands in Linux.

Analyzing the Network

To analyze the current active network interfaces on your local network ifconfig command is used. ifconfig command displays the status of the current active network interfaces. Running this command on the terminal will print out the following results.

root@User:~$ ifconfig

ifconfig is just showing two interfaces here, one eth0 and the other lo. eth0 means Ethernet0 and it is the first network connection to the Linux and the second is the lo which is called the loopback address or localhost.

Ethernet0 (0 becasue Linux counts fron 0) is the physical connection to the network via a switch or a home router. lo is a special address that is used for local development and testing on the device. It is represented by either simply localhost as the domain or the IP address 127.0.0.1.

ifconfig can show other interfaces too if they are connected to the device. For example if there is a Wireless adapter connected to your deivce for connection to the WiFi it will show an interface named wlan0 and details about it. ifconfig can do much more stuff than just showing network interfaces like changing your IP address.

Changing IP address

To change the IP address of your network interface type ipconfig with network interface name and the IP address you want.

root@User:~$ ifconfig
eth0: inet 192.168.81.100 ...
      ...
root@User:~$ ifconfig eth0 192.168.80.200
root@User:~$ ifconfig
eth0: inet 192.168.80.200 ...
      ...

Ping a Network

This command ping is a simple command that send packets to domain or IP address provided as the argument to check if it recceives response from the target address to see wether the target is online or not.

root@User:~$ ping google.com

Pinging google.com [172.217.19.174] with 32 bytes of data:
Reply from 172.217.19.174: bytes=32 time=193ms TTL=118
Reply from 172.217.19.174: bytes=32 time=77ms TTL=118
Reply from 172.217.19.174: bytes=32 time=77ms TTL=118
Reply from 172.217.19.174: bytes=32 time=77ms TTL=118

Ping statistics for 172.217.19.174:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 77ms, Maximum = 193ms, Average = 106ms

CURL

CURL command allows a user to send requests and data to a web server. Generally it is used for testing purposes. For example if typed in the command line with google.com it will output the following respone sent by the google.com server.

root@User:~$ curl google.com

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

Google sent the response document HTML for the request to google.com which is a referrel to the https version of the the domain https://www.google.com/. and if we sent the curl request to the full domain it will send full HTML page that you see in the browser as the response for the request.

But these are just simply the GET request how can we send POST request with it and send data.

POST request with CURL

To send a POST request with the curl, we use -X switch with POST as the value for it.

root@User:~$ curl -X https://www.google.com

<!DOCTYPE html>
<html lang=en>
    <meta charset=utf-8>
    ...
    ...
    <title>Error 405 (Mrthod Not ALlowed)!!1</title>
    ...
</html>

Sending the POST request to Google will result in error as the Search Page will not accept any. That was just done to explain how to send the POST request.

Sending the data with CURL

To send the data like doing it on the web when logging into an account and entering the username and other stuff but here with curl and POST request, we use -d switch.Here d is for data and after the switch we add data into key=value format.

root@User:~$ curl -X POST -d username=something key=value https://www.google.com

<!DOCTYPE html>
<html lang=en>
    <meta charset=utf-8>
    ...
    ...
    <title>Error 405 (Mrthod Not ALlowed)!!1</title>
    ...
</html>

Again we will see the same response and also you shouldn't send any data to any website as long as you understand what you are doing and to whon you are sending this data to.


zainscizainsci