LS, LL, W, Whoami, CD, PWD, Mkdir, MV, Rmdir, CP: Lab Session (6) : Networking
LS, LL, W, Whoami, CD, PWD, Mkdir, MV, Rmdir, CP: Lab Session (6) : Networking
Linux Utilities
vmstat - reports processes, memory, paging, block IO, traps, and cpu activities
System Monitor – displays basic system information and monitor system resources
Schedule a task
Put a file named “.crontab” in your home directory containing the following:
All process details can be accessed from opening the directory /proc/<pid>
In this lab session, the student will learn how to use common Linux utilities like ifconfig, route, ip, dhclient,
nc/socat, ufw/iptables etc. Some application level tools for diagnosing the network connection will also be
covered. Apart from this, the student will also learn how to configure a wireless router and create wireless
networks etc.
Network configuration
ifconfig
ifconfig is the basic NIC configuration tool on Linux, used almost everywhere extensively.
ifconfig -a
o The first interface that it shows, lo, is the localhost interface. It usually has the IP address
127.0.0.1 and represents the local machine itself. Any connection to 127.0.0.1 will be made to
the local machine itself.
2. Closing an interface:
route
route is used for defining the routing tables. ie. the next node that a packet should hop to.
route -n
dhclient
dhclientis used for DHCP discovery on a network. To automatically get the IP NIC configuration from a
DHCP server on the network, do:
dhclient <interface>
ip
ip is the swiss-army knife tool for network configuration. Its usage is mostly similar to the above tools, with
slight differences. man 8 ip lists the entire argument structure of this tool. Some examples are given below:
4. Add a route:
Apart from these, the ip command can also modify the ARP tables, tunnels, multicast addresses etc. More
comprehensive documentation is available at man 8 ip
/etc/network/interfaces
The network interfaces can also be configured permanently in the file /etc/network/interfaces. Here's an
example:
/etc/network/interfaces
auto eth0 # Automatically start eth0 if available
iface eth0 inet static # Configure eth0 statically using the internet protocol
address 192.168.1.174
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
up /home/user/crclient -u user08099 # run the cyberoam client and automatically
login when this interface is brought up
pre-down /home/user/crclient -l # run the cyberoam client and automatically
logout before this interface is brought down
auto lo # automatically start the interface lo
iface lo inet loopback # confiugre lo using the internet protocol, and make it a
loopback interface
iface eth1 inet dhcp # configure eth1 using dhcp
pre-up /etc/init.d/ssh start
up touch /home/user/.connected
down rm /home/user/.connected
down /etc/init.d/ssh stop
NetworkManager GUIs
Apart from the above utilities, you can use the NetworkManager GUIs to configure your network. The Ubuntu
wiki has comprehensive documentation for NetworkManager.
ufw/iptables
ufwand iptables are used for managing firewalls. The Ubuntu wiki has pretty good documentation along with
examples for ufw and iptables. We'll also look at configuring the firewall using Gufw.
Diagnostics
ping
ping is a utility which checks if a particular node is reachable by sending it ICMP echo requests. For example,
to ping google.com:
ping google.com
If you're unable to get any replies, check what the default gateway is using route -n, and then try to ping the
gateway. More comprehensive documentation is available at man 8 ping
nslookup
nslookup is a command-line domain name resolver. It can be used to check if the nameserver is reachable. eg.
to lookup google.com:
nslookup google.com
If it is unable to resolve google.com, but you were able to ping your gateway, you can try changing your
nameserver by editing /etc/resolv.conf. eg. to use Google's DNS servers, with iiitd.edu.in being the default
search domain, add the following lines to /etc/resolv.conf:
/etc/resolv.conf
search iiitd.edu.in
nameserver 8.8.8.8
nameserver 8.8.4.4
Wireshark
Wireshark is used for capturing packets on the network. It must be started as root to be able to capture any
packets. After starting Wireshark, press Ctrl+I and select eth0, eth1, wlan0, or any other interface that you're
interested in, and start capturing the packets. You can specify a regular expression to filter the packets, if you
like.
Communication
Remote access
ssh,which stands for Secure SHell, is used for gaining shell-based remote access to a machine. To login to a
remote machine, you can do:
ssh <user>@<host>
Once you're inside a machine, you have full access to it as if you were working on it locally. To run GUI
programs, a sample login session might proceed as follows:
The DISPLAY variable tells the GUI programs the address of the X server to connect to. X is the most
commonly used GUI subsystem on Linux.
One thing to note here is that due to certain security issues, X may not allow remote clients (such as client
running on machine2) to connect to it (the X server running on machine1). A secure and easy way to get past
this is to run a nested X server with access control turned off and use that for inbound connections. eg.:
File transfer
FTP
ftp <host>
Some of the commonly used FTP commands are:
2. CDUP - similar to cd ..
3. CWD - similar to cd
4. DELE - similar to rm
5. HELP
6. LIST - similar to ls
ssh
ssh comes bundled with some other utilities, like scp and sftp.
scp is used for copying files securely to/from a remote machine over ssh. To copy a file from the local machine
to a remote machine, you can do:
sftp is similar to ftp, but works over ssh. To connect to a remote host for sftp, you can do:
sftp <user>@<host>
ncftpget and ncftpput are scriptable, non-interactive ftp clients. eg. Let's say I want to download the latest
Xubuntu daily image from Ubuntu's FTP server. I can create the following script:
/usr/bin/getxubuntu.sh
#!/bin/bash
ncftpget -u anonymous -p qwerty@asdf.org \
-C ftp://cdimage.ubuntu.com/cdimage/xubuntu/daily-live/current/maverick-desktop-
amd64.iso \
/home/user/xubuntu-daily-"$(date +%F)".iso
/etc/crontab
0 0 * * * /usr/bin/getxubuntu.sh
nc/socat
ncand socat can be used for creating a listening socket or for connecting to a socket that's already listening.
Some examples:
nc -l <port>
2. Connect to a server:
nc <addr> <port>
socat - TCP-LISTEN:<port>
socat - TCP-CONNECT:<host>:<port>
rsync
rsync is used for updating files over a network. It can either push an update to a remote server or pull an
update. It supports connecting via a secure shell connection or a dedicated rsync daemon. Some examples:
1. Local copy: