Verified and Tested 02/26/15

Introduction

This tutorial is a brief rundown of the BASH shell commands and tools that come in handy for the Linux administrator. Focus more on tools for administration, as opposed to the basic usage of Linux based Operating Systems. While this article is being written in the context of distros based on RHEL 4+ and Debian 6+, many of these commands may also be found on BSD, FreeBSD, any other Unix systems. Depending on your Operating system, each command’s flags may differ, or may no exist for the respective Operating System.

Prerequisites

RHEL 4+ and Debian 6+ based Operating Systems, including CentOS and Ubuntu. If you do not have a server, you can start up a reliable Linux server from Atlantic.Net in under 30 seconds.

Basic Bash Administration Tools

I will be providing a Need, coupled with the BASH command that can satisfy this need. Keep in mind that each command may have several many more functions and command line options available than the examples I’ll be showing below. I’ll be sticking to the basic commands that attain the bare minimum output you desire. I encourage all of you read the Manual page for each command.

I need to:

Find out how much hard drive space I have left| Command: df

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        79G  2.2G   73G   3% /
tmpfs           939M     0  939M   0% /dev/shm

List my available hard drives| Command: fdisk

# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00015f65

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1       10444    83884032+  83  Linux

View my systems resources(running tasks, CPU utilization)| Command: top

# top
top - 10:44:56 up 101 days, 25 min,  1 user,  load average: 0.00, 0.01, 0.00
Tasks:  99 total,   1 running,  98 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.0%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1922380k total,  1627448k used,   294932k free,   164252k buffers
Swap:        0k total,        0k used,        0k free,  1267812k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 8655 root      20   0 15032 1068  808 R  2.0  0.1   0:00.01 top
    1 root      20   0 19232 1420 1140 S  0.0  0.1   0:35.85 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:02.20 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:02.05 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
    6 root      RT   0     0    0    0 S  0.0  0.0   0:20.04 watchdog/0

View my systems memory utilization| Command: free

# free -m
             total       used       free     shared    buffers     cached
Mem:          1877       1589        288          0        160       1238
-/+ buffers/cache:        190       1686
Swap:            0          0          0

View my systems disk activity| Command: iostat

# iostat 2
Linux 2.6.32-431.el6.x86_64 (nginxs)    05/11/2015      _x86_64_        (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.11    0.00    0.05    0.02    0.00   99.83

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               0.54         0.10        35.16     863199  306833368

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.00    0.00    0.25    0.25    0.00   98.50

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               5.00         0.00        88.00          0        176

View my network interfaces and IP’s| Command: ifconfig

# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:00:D1:D0:61:AF
          inet addr:209.208.xx.xxx  Bcast:209.208.xx.xxx  Mask:255.255.255.0        
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6414823 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6864133 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1325544537 (1.2 GiB)  TX bytes:1155254908 (1.0 GiB)

eth1      Link encap:Ethernet  HWaddr 00:00:0A:D0:61:AF
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:296071 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:13464542 (12.8 MiB)  TX bytes:398 (398.0 b)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:9813 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9813 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:775658 (757.4 KiB)  TX bytes:775658 (757.4 KiB)

View my hardware’s temperature readings| Command: sensors

# sensors
coretemp-isa-0000
Adapter: ISA adapter
Core 0:      +39.0°C  (high = +76.0°C, crit = +100.0°C)

coretemp-isa-0001
Adapter: ISA adapter
Core 1:      +39.0°C  (high = +76.0°C, crit = +100.0°C)

it8718-isa-0290
Adapter: ISA adapter
in0:         +1.07 V  (min =  +0.00 V, max =  +4.08 V)
in1:         +1.92 V  (min =  +0.00 V, max =  +4.08 V)
in2:         +3.31 V  (min =  +0.00 V, max =  +4.08 V)
in3:         +2.91 V  (min =  +0.00 V, max =  +4.08 V)
in4:         +0.34 V  (min =  +0.00 V, max =  +2.10 V)
in5:         +4.08 V  (min =  +0.00 V, max =  +4.08 V)   ALARM
in6:         +4.08 V  (min =  +0.00 V, max =  +4.08 V)   ALARM
in7:         +3.15 V  (min =  +0.00 V, max =  +4.08 V)
Vbat:        +3.25 V
fan1:       1231 RPM  (min =    0 RPM)
fan2:       1268 RPM  (min =    0 RPM)
temp1:       -55.0°C  (low  = +127.0°C, high = +127.0°C)  sensor = thermistor
temp2:        -2.0°C  (low  = +127.0°C, high = +127.0°C)  sensor = thermistor
temp3:       +20.0°C  (low  = +127.0°C, high = +127.0°C)  sensor = thermal diode

Get permission level of a file/directory| Command: stat

# stat -c '%a' /home/testfile.txt
644

Find what ports are open on a remote/local host| Command: nmap

NOTE: You’ll likely have to install the nmap package from your YUM/APT package manager.

# nmap -p- localhost

Starting Nmap 5.51 ( http://nmap.org ) at 2015-05-11 11:07 EDT
Failed to find device eth1 which was referenced in /proc/net/route
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000011s latency).
Other addresses for localhost (not scanned): 127.0.0.1
Not shown: 65528 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
22/tcp   open  ssh
25/tcp   open  smtp
80/tcp   open  http
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
9000/tcp open  cslistener

Nmap done: 1 IP address (1 host up) scanned in 1.52 seconds

Find out what ports my server is listening on| Command: netstat

# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:139                 0.0.0.0:*                   LISTEN      5573/smbd
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      29209/nginx
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      3557/vsftpd
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      18608/sshd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      9400/master
tcp        0      0 0.0.0.0:445                 0.0.0.0:*                   LISTEN      5573/smbd
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      28996/php-fpm
tcp        0      0 :::139                      :::*                        LISTEN      5573/smbd
tcp        0      0 :::22                       :::*                        LISTEN      18608/sshd
tcp        0      0 ::1:25                      :::*                        LISTEN      9400/master
tcp        0      0 :::445                      :::*                        LISTEN      5573/smbd
udp        0      0 209.208.x.x:137          0.0.0.0:*                               5592/nmbd
udp        0      0 209.208.x.x:137          0.0.0.0:*                               5592/nmbd
udp        0      0 0.0.0.0:137                 0.0.0.0:*                               5592/nmbd
udp        0      0 209.208.x.x:138          0.0.0.0:*                               5592/nmbd
udp        0      0 209.208.x.x:138          0.0.0.0:*                               5592/nmbd
udp        0      0 0.0.0.0:138                 0.0.0.0:*                               5592/nmbd
udp        0      0 0.0.0.0:1194                0.0.0.0:*                               12988/openvpn

Thank you for following along this guide on Basic Bash Administration Tools, I hope that you enjoyed this guide, please check back for more updates.