Useful Linux Commands

From Jeff's Weather Service ~ Wiki Page
Revision as of 21:47, 13 December 2014 by Wikiroot (Talk | contribs) (System Startup Time and Tools (F15 and newer))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Please note, most of the command below require su -.

Network commands

To determine IP address and gateway

To determine the IP address:

# ip addr

Example (the IP address below is:

# ip addr
 1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue 
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   inet brd scope host lo
 2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 100
   link/ether 00:07:e9:0c:a9:e7 brd ff:ff:ff:ff:ff:ff
   inet brd scope global eth0
 3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
   link/ether 00:06:5b:bd:97:71 brd ff:ff:ff:ff:ff:ff

To determine the gateway:

# route

Example (the gateway below is: ciscore.wsicorp):

 # route
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface     *        U     0      0        0 eth0     *          U     0      0        0 eth0
 default         ciscore.wsicorp         UG    0      0        0 eth0

To determine IP address from a nameserver:

# nslookup

Example (the IP address below is:

# nslookup
IP address:

To determine network speed and driver

# ethtool eth0
To determine network driver:
ethtool -i eth0

To determine MAC addresses of other boxes on the network


To see what ports are open/closed

# nc -v -z -w3 <system> <port>


# nc -v -z -w3 producer2 22
producer2 [] 22 (ssh) open

To see if data is coming through a port

# tcpdump dst port 22

Another way to verify is a port is in use:

# lsof -i :22


#lsof -i @houk

To add an IP address on the fly (generally used to connect to a Linux Recovery DVD)

# ip addr add x.x.x.x/24 dev eth0


# ip addr add dev eth0

To view Windows network information

# nmblookup -A x.x.x.x


houk:/wxdisk/wxws>nmblookup -A
Looking up status of
      CSR-SYSTEMXP   <00> -         M <ACTIVE> 
      WEATHER         <00> - <GROUP> M <ACTIVE> 
      CSR-SYSTEMXP   <20> -         M <ACTIVE> 
      WEATHER         <1e> - <GROUP> M <ACTIVE> 

      MAC Address = 00-19-B9-12-EA-70

To stop/restart network

# service network stop
# service network start
# service network restart


# cd /etc/init.d/
# network stop; network start


# ipcalc <gateway> <netmask> -n: network -b: broadcast
[root@houk root]# ipcalc -n -b

Help with /etc/fstab

CIFS/Samba share and /etc/fstab

Mount Commands

To mount all NFS mounts:

# mount -at nfs

To unmount all NFS mounts:

# umount -at nfs

To unmount just one directory:

# umount /directory
 umount /wxdisk/Digital_Media/Movies_Output_1.0/Custom

To unmount an in use directory:

# umount -l /directory
 umount -l /wxdisk/Digital_Media/Movies_Output_1.0/Custom

Rsync options

To rsync multiple files (not entire wxdisk partition), use the following:

rsync -e -ssh -avrPWxz <system>:/directory/ /directory/
 rsync -e ssh -avrPWxz producer:/wxdisk/wxws/scripts/ /wxdisk/wxws/scripts)

To rsync all of the wxdisk partition:

rsync -e "ssh -oCompression=no -c blowfish" -avrPWx <system>:/directory/


 rsync -e "ssh -oCompression=no -c blowfish" -avrPWx producer:/wxdisk/

--exclude=/wxdisk/install/ /wxdisk

To rsync with a 60 second timeout:

rsync -e "ssh -C" --timeout=60 <system>:/directory/ /directory
 rsync -e "ssh -C" --timeout=60 producer:/wxdisk/ --exclude=/wxdisk/install/


SCP Options

To secure copy from remote system to locally using digdug:

scp -C -P <SSH_port> digdug:/directory/ . 
 houk:/wxdisk/customer_files/MISC>scp -C -P 3866

digdug:/wxdisk/wxws/server_ok .

 The authenticity of host 'digdug (' can't be established.
 RSA key fingerprint is 33:bf:7e:8e:be:47:17:b1:d0:69:fd:5e:a4:54:66:f7.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added 'digdug,' (RSA) to the list of known


 wxws@digdug's password: 
 No value for $TERM and no -T specified

100% 0 0.0KB/s --:-- ETA

 Generally, you may have to delete the digdug line in

/wxdisk/wxws/.ssh/known_hosts file before running the above command.

SSH Options

To setup an SSH tunnel:

ssh wxws@<IP address> -L 5900:<IP address>:5900
 ssh wxws@wtsp.customer -L 5900:

Disk Space issue

To sort disk space on a particular drive:

du -k|sort -rn  

To sort file sizes in a particular directory:

ls-F -al !* | sort -n +4

Troubleshooting Hard drives


In Linux use (only on Dell 530/650s):

# smartctl -a /dev/hda OR smartctl -a /dev/hdc

In CentOS use:

# smartctl -a -d ata /dev/sda OR smartctl -a -d ata /dev/sdb


Hard Drive Parameters:

# hdparm /dev/sda

Hard Drive Speed Tests:

# hdparm -T -t /dev/sda


Hard drive partition identifier:

# blkid

List all UUIDs in order:

# blkid |sort -V

Change blkid

# tune2fs /dev/sdb1 -U `uuid`


On older systems:

# fdisk -l /dev/hda OR fdisk -l /dev/hdc

On newer systems:

# fdisk -l /dev/sda OR fdisk -l /dev/sdb

Global fdisk:

# fdisk -l


Creating an ext2, or ext3, or ext4 filesystem

Once you’ve partitioned your hard disk using fdisk command, use mke2fs to create either ext2, ext3, or ext4 file system.

Create an ext2 file system:

# mke2fs /dev/sda1

Create an ext3 file system:

# mkfs.ext3 /dev/sda1
# mke2fs –j /dev/sda1

Create an ext4 file system:

# mkfs.ext4 /dev/sda1
# mke2fs -t ext4 /dev/sda1

Converting ext2 to ext3

For example, if you are upgrading /dev/sda2 that is mounted as /home, from ext2 to ext3, do the following.

# umount /dev/sda2
# tune2fs -j /dev/sda2
# mount /dev/sda2 /home

Note: You really don’t need to umount and mount it, as ext2 to ext3 conversion can happen on a live file system. But, I feel better doing the conversion offline. Converting ext3 to ext4

If you are upgrading /dev/sda2 that is mounted as /home, from ext3 to ext4, do the following.

# umount /dev/sda2
# tune2fs -O extents,uninit_bg,dir_index /dev/sda2
# e2fsck -pf /dev/sda2
# mount /dev/sda2 /home

dd copy

To wipe drive:

# dd if=/dev/zero of=/dev/sdY bs=512 count=1
# dd if=/dev/zero of=/dev/sdc bs=512 count=1

To copy entire drives including partitions: Where sdX is the source partition and sdY is the destination

# dd if=/dev/sdX of=/dev/sdY bs=32M
# dd if=/dev/sdb of=/dev/sdc bs=32M

While dd is running, from another terminal, one can view the status by using this command:

# kill -USR1 PID
# kill -USR1 23696


Sending a USR1 signal to a running `dd' process makes it print I/O statistics to standard error and then resume copying.

# dd if=/dev/zero of=/dev/null& pid=$!
# kill -USR1 $pid; sleep 1; kill $pid
18335302+0 records in 18335302+0 records out 9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s


# iostat -x 5m sdX sdY
# iostat -x 5m sdb sdc

shred (write zeroes to a drive)

# shred -n1 -z -v /dev/sdb

-n is the number of iterations you want to write zeros to the hard drive. My data is not very sensitve so I only do two passes. The US military does seven.

-v means verbose, which is nice if your bored and want to see how far along the zero is.

-z actually writes a zero to hide the shreding.

Read more:

Software RAID

To check raid status:

cat /proc/mdstat

To check detailed raid info:

# dumpe2fs /dev/md0

To re-add drive partition back to raid on Dell 530s, 650s:

# raidhotadd /dev/md0 /dev/hda5 (if hda5 is missing) 
# raidhotadd /dev/md0 /dev/hdc5 (if hdc5 is missing)

To re-add drive partition back to raid on Dell 370s, 380s, 390s, 670s, 690s, HP 8600s:

# raidhotadd /dev/md0 /dev/sda5 (if sda5 is missing) 
# raidhotadd /dev/md0 /dev/sdb5 (if sdb5 is missing)

Systemd commands

How do I change the target (runlevel)?

systemd has the concept of targets which is a more flexible replacement for runlevels in sysvinit.

Run level 3 is emulated by Run level 5 is emulated by is a symbolic link to and is a symbolic link to

You can switch to 'runlevel 3' by running

# systemctl isolate 

You can switch to 'runlevel 5' by running

# systemctl isolate 

How do I change the default target?

# systemctl set-default <name of target>.target is the default. You might want for the equivalent of non graphical (runlevel 3) from sysv init. The full list of targets can be accessed via systemctl list-units --type=target

systemd does not use /etc/inittab file.

How do I know the current target?

# systemctl get-default

System Startup Time and Tools (F15 and newer)

To show system startup time:

# systemd-analyze
Startup finished in 1632ms (kernel) + 1242ms (initrd) + 47806ms (userspace) = 50681ms

To show individual programs startup time:

# systemd-analyze blame

System Hardware

To determine hardware:

# dmidecode|grep -i works

To determine serial/service tag:

# dmidecode|grep -i serial

To tell the bios version:

# dmidecode|grep -2 "BIOS Information"
 [root@houk root]# dmidecode |grep -2 "BIOS Info"
 Handle 0x0000
       DMI type 0, 20 bytes.
       BIOS Information
               Vendor: Dell Computer Corporation
               Version: A09
Note: -2 means the number of lines after BIOS Information.  Therefore, if you

want more lines, for example: 4 use -4.

Memory and swap

To view memory and swap usage for current date:

sar -r -f

For a specific date:

sar -r -f  /var/log/sa/sa<date>
 sar -r -f /var/log/sa/sa18

To check memory usage of a particular program:

 ps -aux|grep -i skycam
 blanchard:/wxdisk/wxws>ps -aux|grep -i skycam
 wxws     27290  0.0 13.6 395588 281616 ?     SL   Jun17   0:40
/wxdisk/Show_FX/bin//skycam -geometry +50+50
 wxws     27992  0.0  0.0  3868  580   pts/3  S    19:36   0:00 grep -i skycam

 RSS: Resident set size = the non-swapped physical memory that a task has
used; Resident Set currently in physical memory including Code, Data, Stack
 VSZ: Virtual memory usage of entire process = VmLib + VmExe + VmData + VmStk 

Another memory command:

cat /proc/meminfo

RPM commands

To view which RPM packages are installed:

rpm -qa|grep -i "wxpro-"

To verify installed package contents:

rpm -V
 houk:/wxdisk/wxws>rpm -V WxPro-bin-5.0.32.SP3.2007120518-1
 .....UG.   /
 ....L...   /wxdisk
 .M......   /wxdisk/wxws
 .M...UG.   /wxdisk/wxws/wxws_exe/change_priority
 S.5....T   /wxdisk/wxws/wxws_exe/
 .M......   /wxdisk/wxws/wxws_exe/ww_makepnt
 .M...UG.   /wxdisk/wxws/wxws_exe/ww_showplay

To list files from installed package:

rpm -ql
 rpm -ql WxPro-bin-5.0.32.SP3.2007120518-1

To display package info and files, the rpm file doesn't need to be installed:

rpm -qpil
 rpm -qpil


To display package files, the rpm file doesn't need to be installed:

rpm -qpl
 rpm -qpl


To determined what rpm file installed a particular directory:

rpm -qf wxws_exe
 houk:/wxdisk/wxws>rpm -qf wxws_exe

To view package information, such as install date:

rpm -qi
 desktop:/home/jeff>rpm -qi x11vnc-0.9.8-4.fc12.i686
 Name        : x11vnc                       Relocations: (not relocatable)
 Version     : 0.9.8                             Vendor: Fedora Project
 Release     : 4.fc12                        Build Date: Mon 20 Jul 2009

06:29:32 PM EDT

 Install Date: Mon 20 Jul 2009 08:05:33 PM EDT      Build Host:

 Group       : User Interface/X              Source RPM:


 Size        : 2754715                          License: GPLv2+ and GPLv2
 Signature   : (none)
 Packager    : Fedora Project
 URL         :
 Summary     : VNC server for the current X11 session
 Description :
 x11vnc is to X Window System what WinVNC is to Windows, i.e. a server
 which serves the current X Window System desktop via RFB (VNC)
 protocol to the user.
 Based on the ideas of x0rfbserver and on LibVNCServer, it has evolved
 into a versatile and performant while still easy to use program.

Screen Resolution and Video driver info

To set current DISPLAY:


setenv DISPLAY :0


export DISPLAY=:0

To determine the existing screen size:

setenv DISPLAY :0 OR
export DISPLAY=:0
xrandr --verbose

To determine NVIDIA driver:

setenv DISPLAY :0 OR
export DISPLAY=:0
glxinfo|grep -i nvidia

To determine NVIDIA hardware:

setenv DISPLAY :0 OR
export DISPLAY=:0
glxinfo|grep -i geforce

To detect the video output card (look for NVIDIA-SDI):

cat /var/log/Xorg.0.log|grep -i crt
 desktop:/home/jeff>cat /var/log/Xorg.0.log|grep -i crt
 (**) NVIDIA(0): Option "MetaModes" "CRT: nvidia-auto-select +0+0"
 (**) NVIDIA(0): Option "TwinViewXineramaInfoOrder" "CRT-0"
 (--) NVIDIA(0):     Samsung SyncMaster (CRT-0)
 (--) NVIDIA(0): Samsung SyncMaster (CRT-0): 350.0 MHz maximum pixel clock
 (II) NVIDIA(0): Display Device found referenced in MetaMode: CRT-0
 (II) NVIDIA(0): Assigned Display Device: CRT-0
 (II) NVIDIA(0):     "CRT:nvidia-auto-select+0+0"
 (--) NVIDIA(1):     Samsung SyncMaster (CRT-0)
 (--) NVIDIA(1): Samsung SyncMaster (CRT-0): 350.0 MHz maximum pixel clock
 (II) NVIDIA(0): Setting mode "CRT:nvidia-auto-select+0+0"

USB devices/drives

List USB devices:

# lsusb
# lsusb -v (verbose)
# cat /proc/bus/usb/devices


To make a new directory:

mkdir <dirname>

To make recursive directories (that do not already exist)

mkdir -p <dirname/dir2name>
 mkdir -p new/directory
 This creates a directory called new and a directory called new/directory.

To recursively delete directories:

 rm -rf <deleted_dir>/
  rm -rf new/

File types

To determine file type:

file <filename>
 houk:/wxdisk/customer_files/MISC>file server_ok 
 server_ok: empty

To determine image properties:

identify <filename>


 watch_not_gone.sgi SGI 720x486 720x486+0+0 DirectClass 1.335mb
 For more information:
 houk:/wxdisk/Digital_Media/Images_Output_1.0/Custom>identify -verbose


To determine SGI movie info:

mvinfo <filename>
 houk:/wxdisk/Digital_Media/Movies_Output_1.0/Custom>mvinfo TWN_SE_Nowrad

To determine QT movie info:

oqtinfo <filename>

To create a screen capture from a terminal window:

import -window root filename.jpg

File Permissions

Permission types:

Read (r),
Write (w),
Execute (or access for directories) (x),
Execute only if the file is a directory or already has execute permission for

Calculating it, using Math:

r 4 (read)
w 2 (write)
x 1 (execute)

some user (X),

Set user or group ID on execution (s),
Save program text on swap device (t),
The permissions that the user who owns the file currently has for it (u),
The permissions that other users in the file's group have for it (g),
Permissions that other users not in the file's group have for it (o).

Letter combinations:

The user who owns it (u)
Other users in the file's group (g)
Other users not in the file's group (o)
All users (a)
Format: uuugggooo

Numeric mode:

From one to four octal digits
Any omitted digits are assumed to be leading zeros.
The first digit = selects attributes for the set user ID (4) and set group ID

(2) and save text image (1)S

The second digit = permissions for the user who owns the file: read (4),

write (2), and execute (1)

The third digit = permissions for other users in the file's group: read (4),

write (2), and execute (1)

The fourth digit = permissions for other users NOT in the file's group: read

(4), write (2), and execute (1)

The octal (0-7) value is calculated by adding up the values for each digit
User (rwx) = 4+2+1 = 7
Group(rx) = 4+1 = 5
World (rx) = 4+1 = 5
chmode mode = 0755 


chmod o+w filename
filename -------w-
chmod 777 filename
filename rwxrwxrwx

Click here for more information: []

Managing Group Access

Crontab Help


field         allowed values
-----         --------------
minute        0-59
hour          0-23
day of month  1-31
month         1-12 (or names, see below)
day of week   0-7 (0 or 7 is Sun, or use names)


# use /bin/sh to run commands, overriding the default set by cron
# mail any output to `paul', no matter whose crontab this is
# run five minutes after midnight, every day
5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * *     $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5    mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun     echo "run at 5 after 4 every sunday"

Debugging Tools

To debug frozen/locked up process use:

# gdb -p <process id>
 houk:/wxdisk/wxws>gdb -p 3566
 GNU gdb Red Hat Linux (
 Copyright 2004 Free Software Foundation, Inc.
 GDB is free software, covered by the GNU General Public License, and you are
 welcome to change it and/or distribute copies of it under certain


 Type "show copying" to see the conditions.
 There is absolutely no warranty for GDB.  Type "show warranty" for details.
 This GDB was configured as "i386-redhat-linux-gnu".
 Attaching to process 3566
 Reading symbols from /bin/tcsh...(no debugging symbols found)...done.
 Using host libthread_db library "/lib/tls/".
 Reading symbols from /lib/ debugging symbols


 Loaded symbols for /lib/
 Reading symbols from /lib/ debugging symbols


 Loaded symbols for /lib/
 Reading symbols from /lib/tls/ debugging symbols


 Loaded symbols for /lib/tls/
 Reading symbols from /lib/
 (no debugging symbols found)...done.
 Loaded symbols for /lib/
 0xb74972a8 in sigsuspend () from /lib/tls/
 (gdb) where
 #0  0xb74972a8 in sigsuspend () from /lib/tls/
 #1  0xb7497554 in __sigpause () from /lib/tls/
 #2  0x08064157 in ?? ()
 #3  0x00000002 in ?? ()
 #4  0x00000000 in ?? ()
 (gdb) quit

To debug live processes, use:

# strace -p <process id>
 houk:/wxdisk/wxws>strace -p 3566
 Process 3566 attached - interrupt to quit

Simple foreach loop to convert and same file format

Run as: tcsh 
[jeff@desktop images]$ foreach x ( *.JPG )
foreach? convert $x -resize 200x150! `echo $x| sed 's/.JPG/.jpg/g'`
foreach? end

VI Help

Shift-6 "^" - Go to the beginning of a line.
Shift-4 "$" - Go to the end of a line.
:1 - Go to the top of a page (line 1).
:$ - Go to the end of a page.