n this article, we’ll explore networking under GNU/Linux. You’ll find it interesting to manage the entire network through certain valid keystrokes known as commands. Imagine that you have to access the contents of several other machines from a mount point in your machine. Then imagine shutting down, rebooting, and installing applications on those remote machines, all at one time? Could you configure the WLAN and LAN interfaces from the CLI? This tutorial gives you some insights to the exciting bytes on controlling your network under GNU/Linux.
First we will learn the ‘Hello World’ of a networked machine.
Let us ping!
ping is a universal command that is available on every operating system to test the reachability of a network. When you shoot your terminal with the ping command and an IP address as its argument, the machine will try to send some bits of raw data towards the machine owning that IP address. If some machine exists with that IP address, it will send back certain bits. Thus the machine receives the bits and it confirms that a path is available from the current machine to the other through a network. We can check the existence of certain machines on the network by just pinging.
To see which machines are up in the current network, let’s write a bit of shell script. Open vim as the root:
…and key in the following lines in it:
Save the file, and make it executable by running the following command:
Now, run the script as:
Configuring your network
Now, let us look at how to configure your machine on the network. You can configure it using two methods. It can be configured manually by the ifconfig command for static IP addressing or it can be done via the DHCP (Direct Host Control Protocol).
Static IP addressing is the one that you explicitly instruct the system to use by giving an IP address for a given Ethernet or wireless interface. In case you’re using the DHCP, simply issuing the dhclient command will fetch the system an available IP address from the DHCP server in your network. Note that it may not be the same IP address that your machine fetches each time you issue dhclient.
Interface cards
Machines are networked either via network cables or using wireless protocols. LAN cards used for networking are known as Ethernet and wireless LAN (WLAN) cards. We interface the network via this outlet. In *nix platforms, Ethernet cards or WLAN cards are denoted as eth0, eth1, etc, or wlan0, wlan1, etc, respectively.
ifconfig
We have ifconfig, a.k.a the interface config, for setting up a network on the machine. To get information about the availability of interface devices available on the current machine, open a terminal and execute the following as the root:
Here I have three interfaces — eth0, lo and wlan0, where:
- eth0 corresponds to the Ethernet card
- lo corresponds to a loopback device that points to the localhost network
- wlan0 corresponds to the wireless LAN card
Static IP addressing
For static IP addressing, issue the following command as the root:
For example:
ifconfig -a gives you details of all interface devices and configurations. In order to receive details of only one Ethernet device, execute ifconfig eth0. The following is an example output:
Now, let us go about setting the subnet mask. This is done easily with ifconfig, as follows:
Now that the IP address and subnet mask is configured (or reconfigured), how do you get your Ethernet up (i.e., available) and down? The ifup and ifdown commands help you with that as follows:
Wireless networking
In order to hack a wireless card, we have another utility called iwconfig. It works similar to ifconfig, but it has lots of additional features that are bonded to wireless cards. If we are using a wireless network with static IP, we can attach our wireless card interface to a network as follows:
…or:
…where ‘slynux’ is the ESSID (that is, the name of the wireless network) and ‘8c140b2037’ is the security key. Of course, you need to replace these variables with the values that hold good in your network. You can also scan and check the availability of wireless network(s) in your vicinity using the iwlist command as follows:
The above command will list out the various wireless networks available with a number of properties.
Then we can set the IP for the interface card using the ifconfig command itself:
If you are using dynamic addressing, you can obtain the IP address as follows:
The settings that you’ve configured with the ifconfig tool are available until the system reboot. But it’s a waste of time if you need to configure it on every system start. And hence we take the aid of network configuration scripts. On an Ubuntu (or any other Debian-based) system, this file is located at /etc/networks/interfaces, and contains data similar to the following:
It is necessary to learn this scripting in order to play with your network. The syntax for these are as follows:
- Add the following lines if you want to configure eth0 as the DHCP:
- Add the following files if you want to configure static IP:
- If it is a wireless network, add the following lines along with the above lines:
Now, to restart the network daemon, execute the following as the root:
Spoofing a MAC ID
It is a real hassle for cable Internet customers that they are restricted to using a single machine for Internet access. If you want to plug your laptop in your friend’s cable Internet connection,
you have to call the service provider to refresh the MAC address.
The MAC address is permanent to the hardware and cannot be changed. Since we operate the hardware via the software abstraction layer, it is quite possible to do some software-level cheating for the network card’s MAC ID. We can simply spoof it to some other MAC addresses.
You can obtain the original MAC ID from the ifconfig output. Mine is as follows:
Now, let’s change the last part of the MAC ID from 22 to 23:
Now, run ifconfig again:
Easy enough? Well, let’s consider the following instance: suppose you are in a Wi-Fi campus and the access to the wireless network is restricted using MAC addressing. You can simply look at your friend’s laptop MAC ID and spoof it. Yes! You are now free to access the network. Have fun!
DNS (Domain Name Service)
DNS is responsible for name resolution. When you point your browser to www.google.com, it points to a server on the Internet. How does that happen? As you are aware, all networked computers are assigned with IP addresses. But how do you access the Web page hosted in Google’s remote machine by simply typing a name like google.com?
That phenomenon is achieved using domain name resolution. There are some servers on the Web called name servers (or DNS) that resolve certain names to corresponding IP addresses, like google.com to its corresponding IP address, in our case. So, we should have the IP addresses of the DNS servers (generally provided by the Internet service provider) handy so that we don’t have to remember everyone else’s when we browse the Web. When we point our browser to google.com, it consults one of these name servers to find out the IP address and thus load the Web page. But where do we configure the IP addresses of these name servers?
If your network is configured with DHCP, there is no need to specify the name server explicitly. For static IPs, it is, however, necessary. We enter the DNS servers’ IP addresses in the /etc/resolv.conf file. Mine looks like the following:
Note that you don’t really need to use the DNS addresses provided by your ISP. For safety purposes, I use OpenDNS—the IP addresses are listed in the above snippet. You can learn more on why OpenDNS is a much safer bet at www.opendns.org.
SSH (Secure Shell)
SSH can be defined as the blood of *nix networks. SSH enables users and administrators to make remote logins to other machines that are connected through any kind of network. If you know the user name, password and IP address of another machine on the network, you can remotely log in to that machine and work on it as if you are actually working in front of that machine. The following is an example in which I’m authenticating to a system with the IP address of 192.168.1.3 as the user test:
You can open the CD tray of the other machine, close the tray, shutdown, reboot the machine — depending on the privileges the user name you’ve logged in with, has.
sftp is an extension to the ssh protocol that helps us to use the SSH connection to transfer files between machines. The following is an example:
To download a file from the remote machine we use the get command, and to upload a file, we use put. In the above snippet you can see that I’m downloading a file named t.c using the get command, after logging in to the remote machine using sftp.
sshfs is another extension to SSH, which empowers you to mount directories on a remote machine as a filesystem to a specified mount point:
In the above snippet, I’m mounting the home directory of the user ‘test’ on 192.168.1.3 to my local machine under the /mnt/test directory.
Proxy server configuration
Many of us on a college campus or office network access the Internet through a proxy server. How do you set the proxy server details in your shell environment? You can set the proxy for different protocols as follows:
If you want these settings to be permanent, each time you log in add these lines to your ~/.bash_profile file.
That’s all, folks! Hope you have enjoyed learning the secrets of networking. Happy hacking till we meet again!
No comments:
Post a Comment