Useful Linux Commands

From Jeff's Weather Service ~ Wiki Page
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: 147.81.80.40):

# 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 127.0.0.1/8 brd 127.255.255.255 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 147.81.80.40/22 brd 147.81.83.255 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
 147.81.80.0     *               255.255.252.0   U     0      0        0 eth0
 169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
 default         ciscore.wsicorp 0.0.0.0         UG    0      0        0 eth0

To determine IP address from a nameserver:

# nslookup

Example (the IP address below is: 216.236.239.80):

# nslookup wsi.com
Server:         147.81.81.105
Address:        147.81.81.105#53
 
Name:   wsi.com
Address: 216.236.239.80
 
IP address: 216.236.239.80

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

arp

To see what ports are open/closed

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

Example:

# nc -v -z -w3 producer2 22
producer2 [192.168.4.2] 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

OR

#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

Example:

# ip addr add 10.81.80.220/24 dev eth0

To view Windows network information

# nmblookup -A x.x.x.x

Example:

houk:/wxdisk/wxws>nmblookup -A 10.142.102.20
Looking up status of 10.142.102.20
      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

OR

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

ipcalc

# ipcalc <gateway> <netmask> -n: network -b: broadcast
[root@houk root]# ipcalc 10.216.148.30 255.255.255.224 -n -b
BROADCAST=10.216.148.31
NETWORK=10.216.148.0

Help with /etc/fstab

http://www.tuxfiles.org/linuxhelp/fstab.html

CIFS/Samba share and /etc/fstab

http://ubuntuforums.org/showthread.php?t=288534
http://linux.die.net/man/8/mount.cifs

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
 example:
 umount /wxdisk/Digital_Media/Movies_Output_1.0/Custom

To unmount an in use directory:

# umount -l /directory
 example:
 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/
 example:
 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/

/directory

 example:
 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
 example:
 rsync -e "ssh -C" --timeout=60 producer:/wxdisk/ --exclude=/wxdisk/install/

/wxdisk


SCP Options

To secure copy from remote system to locally using digdug:

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

digdug:/wxdisk/wxws/server_ok .

 The authenticity of host 'digdug (10.66.1.225)' 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,10.66.1.225' (RSA) to the list of known

hosts.

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

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
example:
 ssh wxws@wtsp.customer -L 5900:147.81.80.40: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

smartctl

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

hdparm

Hard Drive Parameters:

# hdparm /dev/sda

Hard Drive Speed Tests:

# hdparm -T -t /dev/sda

blkid

Hard drive partition identifier:

# blkid

List all UUIDs in order:

# blkid |sort -V

Change blkid

# tune2fs /dev/sdb1 -U `uuid`

fdisk

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

mke2fs

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
(or)
# mke2fs –j /dev/sda1

Create an ext4 file system:

# mkfs.ext4 /dev/sda1
(or)
# 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

OR:

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

# 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: http://computersight.com/communication-networks/security/how-to-zero-a-hard-drive-in-linux-using-shred/#ixzz236va8H3m

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

http://0pointer.de/blog/projects/why.html

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 multi-user.target. Run level 5 is emulated by graphical.target. runlevel3.target is a symbolic link to multi-user.target and runlevel5.target is a symbolic link to graphical.target.

You can switch to 'runlevel 3' by running

# systemctl isolate multi-user.target 

You can switch to 'runlevel 5' by running

# systemctl isolate graphical.target 

How do I change the default target?

# systemctl set-default <name of target>.target 

graphical.target is the default. You might want multi-user.target 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"
 example:
 [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>
 example:
 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
 USER       PID %CPU %MEM   VSZ  RSS    TTY   STAT START   TIME COMMAND  
 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
 example:
 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/syncSrcmat.sh
 .M......   /wxdisk/wxws/wxws_exe/ww_makepnt
 .M...UG.   /wxdisk/wxws/wxws_exe/ww_showplay

To list files from installed package:

rpm -ql
 example:
 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
 example:
 rpm -qpil

/wxdisk/install/WxPro/release/WxPro-docs-5.0.32.SP3.2007120518-1.noarch.rpm

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

rpm -qpl
 example:
 rpm -qpl

/wxdisk/install/WxPro/release/WxPro-docs-5.0.32.SP3.2007120518-1.noarch.rpm


To determined what rpm file installed a particular directory:

rpm -qf wxws_exe
 example:
 houk:/wxdisk/wxws>rpm -qf wxws_exe
 WeatherIdentity-Producer-Theme_B-3.2-1
 WxPro-sys-5.0.32.SP3.2007120518-1
 WxPro-bin-5.0.32.SP3.2007120518-1

To view package information, such as install date:

rpm -qi
 example:
 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:

xenbuilder2.fedora.redhat.com

 Group       : User Interface/X              Source RPM:

x11vnc-0.9.8-4.fc12.src.rpm

 Size        : 2754715                          License: GPLv2+ and GPLv2
 Signature   : (none)
 Packager    : Fedora Project
 URL         : http://www.karlrunge.com/x11vnc/
 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:

tcsh:

setenv DISPLAY :0

bash:

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
 example:
 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


Directories

To make a new directory:

mkdir <dirname>

To make recursive directories (that do not already exist)

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

To recursively delete directories:

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


File types

To determine file type:

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

To determine image properties:

identify <filename>
 example:
 houk:/wxdisk/Digital_Media/Images_Output_1.0/Custom>identify

watch_not_gone.sgi

 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

watch_not_gone.sgi

To determine SGI movie info:

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

To determine QT movie info:

oqtinfo <filename>
 example:
 houk:/wxdisk/Digital_Media/Movies_Output_1.0/Custom>oqtinfo mk_test1.mov
 

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 

Example:

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

Click here for more information: [http://ss64.com/bash/chmod.html http://ss64.com/bash/chmod.html]


Managing Group Access

http://www.yolinux.com/TUTORIALS/LinuxTutorialManagingGroups.html

Crontab Help

Values:

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)

Examples:

# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# 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>
 example:
 houk:/wxdisk/wxws>gdb -p 3566
 GNU gdb Red Hat Linux (6.3.0.0-1.132.EL3rh)
 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

conditions.

 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/libthread_db.so.1".
 Reading symbols from /lib/libtermcap.so.2...(no debugging symbols

found)...done.

 Loaded symbols for /lib/libtermcap.so.2
 Reading symbols from /lib/libcrypt.so.1...(no debugging symbols

found)...done.

 Loaded symbols for /lib/libcrypt.so.1
 Reading symbols from /lib/tls/libc.so.6...(no debugging symbols

found)...done.

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


To debug live processes, use:

# strace -p <process id>
 example:
 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.