Difference between revisions of "Linux Commands"

From GWAVA Technologies Training
Jump to: navigation, search
(Disk Commands)
Line 36: Line 36:
  
 
Note: Reload comes with some nice scripts that Tay created for setting up cifs and ncp mounts. They are found under /opt/beginfinite/reload/setup. You just enter the setup information into the corresponding conf file and run the script.
 
Note: Reload comes with some nice scripts that Tay created for setting up cifs and ncp mounts. They are found under /opt/beginfinite/reload/setup. You just enter the setup information into the corresponding conf file and run the script.
 
  
 
cifsmnt.conf:
 
cifsmnt.conf:
Line 70: Line 69:
 
==Directory Commands==
 
==Directory Commands==
  
===Change Directory===
+
===cd Change Directory===
 
Change directory. Tab complete can help traverse the tree.
 
Change directory. Tab complete can help traverse the tree.
 
  cd [destination directory]
 
  cd [destination directory]
Line 82: Line 81:
 
  ...
 
  ...
  
===Directory details===
+
===du Directory details===
 
Returns an estimate of file space usage, in human readable format (k,M,G). Warning: May take a long time.
 
Returns an estimate of file space usage, in human readable format (k,M,G). Warning: May take a long time.
 
  du -h
 
  du -h
Line 89: Line 88:
 
  du -hsx * | sort -rh | head -10
 
  du -hsx * | sort -rh | head -10
  
===Create a directory===
+
===md Create a directory===
 
Creates a directory
 
Creates a directory
 
  md [directory name]
 
  md [directory name]
 
  mkdir [directory name]
 
  mkdir [directory name]
  
===Copy a directory===
+
===cp -R Copy a directory===
 
Copy a directory recursively "R"
 
Copy a directory recursively "R"
 
  cp -R "source" "destination"
 
  cp -R "source" "destination"
  
===Remove a directory ===
+
===rm -r Remove a directory ===
 
Removes a directory and its contents. Warning: irreversible, fast and dangerous if done in the wrong place with wrong switches. [http://thenextweb.com/media/2012/05/21/how-pixars-toy-story-2-was-deleted-twice-once-by-technology-and-again-for-its-own-good/]
 
Removes a directory and its contents. Warning: irreversible, fast and dangerous if done in the wrong place with wrong switches. [http://thenextweb.com/media/2012/05/21/how-pixars-toy-story-2-was-deleted-twice-once-by-technology-and-again-for-its-own-good/]
 
  rm -r "directory"
 
  rm -r "directory"
  
===Directory List===
+
===l Directory List===
 
Return a list of files in a directory. This always works (shows file size in bytes)
 
Return a list of files in a directory. This always works (shows file size in bytes)
 
  ls -s
 
  ls -s
Line 127: Line 126:
 
  lsof
 
  lsof
  
===Change Permissions===
+
===chmod Change Permissions===
 
Permissions are a big deal in Linux [https://en.wikipedia.org/wiki/Chmod] not so much in Windows.
 
Permissions are a big deal in Linux [https://en.wikipedia.org/wiki/Chmod] not so much in Windows.
  
Line 139: Line 138:
 
  chmod -R 777 /[destination]
 
  chmod -R 777 /[destination]
  
===Change Ownership===
+
===chown Change Ownership===
 
Ownership of a directory can make a big difference in how Retain works
 
Ownership of a directory can make a big difference in how Retain works
 
  chown user:group directory/
 
  chown user:group directory/
Line 147: Line 146:
  
 
==File Commands==
 
==File Commands==
 +
===cat Concatenate==
 
Show the contents of a file
 
Show the contents of a file
 
  cat "filename"
 
  cat "filename"
Line 156: Line 156:
 
  cat "filename" | less
 
  cat "filename" | less
  
===Find a file===
+
===find Find a file===
 
Find a file from the root of the file system (aka looks everywhere for it) Warning: may take a long time.
 
Find a file from the root of the file system (aka looks everywhere for it) Warning: may take a long time.
 
  find / -name "fileName"
 
  find / -name "fileName"
Line 178: Line 178:
 
  whereis [commandname]
 
  whereis [commandname]
  
===Copy a file===
+
===cp Copy file===
 
  cp origin /destination/file
 
  cp origin /destination/file
  
Line 193: Line 193:
 
  rsync -ravP [source] [destination]
 
  rsync -ravP [source] [destination]
  
===Move a file===
+
===mv Move file===
 
Moves file to the destination directory in verbose "v" mode.
 
Moves file to the destination directory in verbose "v" mode.
 
  mv -v "file" "destination"
 
  mv -v "file" "destination"
  
===Remove or delete a file===
+
===rm Remove or delete a file===
 
Removes a particular file
 
Removes a particular file
 
  rm "file"
 
  rm "file"
  
===Change Permissions===
+
===chmod Change Permissions===
 
Permissions are a big deal in Linux [https://en.wikipedia.org/wiki/Chmod] not so much in Windows.
 
Permissions are a big deal in Linux [https://en.wikipedia.org/wiki/Chmod] not so much in Windows.
 
Add execute "x" rights to all levels on all shell scripts in the current directory
 
Add execute "x" rights to all levels on all shell scripts in the current directory
 
  chmod +x *.sh
 
  chmod +x *.sh
  
===Symbolic link===
+
===ln Symbolic link===
 
Make symbolic links between files. This is useful if you need to change the log directory or repair a symbolic link that was removed.
 
Make symbolic links between files. This is useful if you need to change the log directory or repair a symbolic link that was removed.
 
  ln -s [path to new directory] ./retain-tomcat7
 
  ln -s [path to new directory] ./retain-tomcat7
  
 
==Network==
 
==Network==
 +
===wget Get File from website===
 
Download the Latest Version of Retain
 
Download the Latest Version of Retain
 
  wget "http://download.gwava.com/download.php?product=Retain&version=current"
 
  wget "http://download.gwava.com/download.php?product=Retain&version=current"
  
 +
===ifconfig===
 
Find the IP address of the server. Use this command to find the IP address(es) and MAC address(es) the system uses. eth0 is the first NIC. If the system has multiple NICs, they are named, eth0, eth1, and so on. lo is loopback.
 
Find the IP address of the server. Use this command to find the IP address(es) and MAC address(es) the system uses. eth0 is the first NIC. If the system has multiple NICs, they are named, eth0, eth1, and so on. lo is loopback.
 
  ifconfig
 
  ifconfig
  
 +
===host===
 
look up mx records
 
look up mx records
 
  host -t MX [domain.com]
 
  host -t MX [domain.com]
Line 223: Line 226:
 
  cat /etc/resolv.conf  
 
  cat /etc/resolv.conf  
  
 +
===ping===
 
Ping to see if a server is alive. Ctrl-c to stop.
 
Ping to see if a server is alive. Ctrl-c to stop.
 
  ping [ip address]
 
  ping [ip address]
  
 +
===DNS===
 
DNS
 
DNS
 
  nslookup
 
  nslookup
Line 236: Line 241:
 
  rcnscd start
 
  rcnscd start
  
 +
===traceroute===
 
Trace the route to a server
 
Trace the route to a server
 
  traceroute [domain.com]
 
  traceroute [domain.com]
  
 +
===hostname===
 
Show the system name
 
Show the system name
 
  hostname
 
  hostname
  
SSH
+
===ssh===
 
Secure connect to another system, exit to leave ssh shell
 
Secure connect to another system, exit to leave ssh shell
 
  ssh [hostname or ip address]
 
  ssh [hostname or ip address]
  
telnet
+
===telnet===
 
Useful for testing connections. You can send a test email with it:
 
Useful for testing connections. You can send a test email with it:
 
  telnet [IP address or DNS hostname] [port]
 
  telnet [IP address or DNS hostname] [port]
Line 258: Line 265:
 
  quit[ENTER]
 
  quit[ENTER]
  
 +
===netstat===
 
A useful tool for checking your network configuration and activity.
 
A useful tool for checking your network configuration and activity.
 
  netstat -lnp -A inet
 
  netstat -lnp -A inet
  
 
==Process Commands==
 
==Process Commands==
 +
===free Memory usage===
 
Display memory usage
 
Display memory usage
 
  free -m
 
  free -m
  
 +
===top System resouces===
 
Display an overview of the system resources can be used to find a process
 
Display an overview of the system resources can be used to find a process
 
  top
 
  top
  
 +
===vmstat===
 
Report virtual memory statistics: processes, memory, paging, block IO, traps, and cpu activity.
 
Report virtual memory statistics: processes, memory, paging, block IO, traps, and cpu activity.
 
  vmstat
 
  vmstat
  
 +
===chkconfig===
 
Check runlevel of a process
 
Check runlevel of a process
 
  chkconfig [process name]
 
  chkconfig [process name]
  
===Find a process===
+
===ps aux Find a process===
 
To find a running process you know the name of  
 
To find a running process you know the name of  
 
  ps aux | grep [process name]
 
  ps aux | grep [process name]
  
===Kill a process===
+
===kill End a process===
 
You have to find a runaway process with <code>top</code> or <code>ps</code>
 
You have to find a runaway process with <code>top</code> or <code>ps</code>
 
  kill [process ID]
 
  kill [process ID]
Line 286: Line 298:
 
  killall -15 [processname]
 
  killall -15 [processname]
  
===Who is logged in===
+
===w Who is logged in===
 
Returns a list of logged in users if the TTY column shows "pts/3" that shows us that use is remotely logged in.
 
Returns a list of logged in users if the TTY column shows "pts/3" that shows us that use is remotely logged in.
 
  who
 
  who
 
  w
 
  w
  
===File system information===
+
===fstab File system information===
 
The fstab file will also contain information about any mounted filesystems as well
 
The fstab file will also contain information about any mounted filesystems as well
 
  cat /etc/fstab
 
  cat /etc/fstab
 
  
 
===Linux Version===
 
===Linux Version===
 
To find the version of Linux that is installed use this command:
 
To find the version of Linux that is installed use this command:
 
  cat /etc/*-release
 
  cat /etc/*-release
 +
 
==Utilities==
 
==Utilities==
===History===
+
===history Command history===
 
Get a history of commands
 
Get a history of commands
 
  history
 
  history
Line 308: Line 320:
 
  ![Ctrl-r][start typing desired command]
 
  ![Ctrl-r][start typing desired command]
  
===Create archives===
+
===tar Create archives===
 
Create an uncompressed tape archive of a bunch of files  
 
Create an uncompressed tape archive of a bunch of files  
 
  tar -cvf [dest file name].tar [source directory]  
 
  tar -cvf [dest file name].tar [source directory]  
Line 314: Line 326:
 
  tar -xvf [filename]  
 
  tar -xvf [filename]  
  
===compress files===
+
===zip/gzip Compress files===
 
Zip
 
Zip
 
  zip [dest file name] [source files or directory]  
 
  zip [dest file name] [source files or directory]  
Line 326: Line 338:
 
  gzip -dv [file name]
 
  gzip -dv [file name]
  
===Clear Screen===
+
===clear Clear Screen===
 
Clear Screen
 
Clear Screen
 
  clear
 
  clear
  
===Schedule Tasks===
+
===crontab Schedule Tasks===
 
crontab – Schedule a command to run at a later time.
 
crontab – Schedule a command to run at a later time.
  
Line 347: Line 359:
 
This will load the crontab editor. Add the entry below, save the change and exit the editor.
 
This will load the crontab editor. Add the entry below, save the change and exit the editor.
 
  crontab -e
 
  crontab -e
 +
Restarts ntp-client every minute
 
  0 * * * * root /etc/init.d/ntp-client restart
 
  0 * * * * root /etc/init.d/ntp-client restart
 +
Runs the script at the top of every hour
 +
00 */1 * * * /root/Desktop/gopostal/gopostalfullauto.sh
  
===Date and time===
+
===date Date and time===
 
This command can be used to display the current time on the server or adjust the time.
 
This command can be used to display the current time on the server or adjust the time.
 
  date
 
  date
  
===Package Installer===
+
===rpm Package Installer===
 
Package installer. There are numerous useful flags that can be used with this command. For those flags, look here: http://www.novell.com/coolsolutions/tip/19918.html
 
Package installer. There are numerous useful flags that can be used with this command. For those flags, look here: http://www.novell.com/coolsolutions/tip/19918.html
 
Most commonly, however, you'll just use the commands to install, upgrade and uninstall.
 
Most commonly, however, you'll just use the commands to install, upgrade and uninstall.
 
  rpm -ivh [filename].rpm
 
  rpm -ivh [filename].rpm
  
===Shutdown and reboot===
+
===shutdown Shutdown and reboot===
 
Shutdown the server
 
Shutdown the server
 
  shutdown -r now
 
  shutdown -r now

Revision as of 14:57, 24 November 2015

Useful Linux Commands

You can use man [command] to get information about a command, but that is more programmer oriented. You are generally better off going to a search engine and searching for [command] examples.

One of the great powers of Linux is being able to pipe | or redirect outputs from one command to another to do things.

You can also use these commands to create bash scripts.

Contents

Disk Commands

df Disk Free

Returns a quick overview of what disks are mounted and how full they are in human readable formatting.

df -h

mount Mount an NFS Volume

Mount a file system or device. You can also type mount by itself to show current mounts on the system. Before making the mount, make sure you have a directory on the Linux box to mount the filesystem to. Usually mounts go under the /mnt directory.

Create a mount point

mkdir /mnt/whatever

Once you have the directory set up, you can now mount the filesystem or share.

Linux mounts use nfs usually:

mount -v -t nfs 10.1.1.20:/exportdir /mnt/whatever 

Netware mounts use npc usually:

ncpmount -S [server] -A [IP address] -U [fully distinguished name] -V /vol1 /mnt -o tcp

Example:

ncpmount -S school -A 192.168.100.1 -u admin.wwws  -V vol1 /mnt/nwserver -o tcp

Windows mounts can use cifs or samba:

mount -t cifs //ntserver/download -o username=admin,password=password /mnt/whatever
mount -t smbfs -o username=admin,password=password //ntserver/download /mnt/ntserver

Note: Reload comes with some nice scripts that Tay created for setting up cifs and ncp mounts. They are found under /opt/beginfinite/reload/setup. You just enter the setup information into the corresponding conf file and run the script.

cifsmnt.conf:

    1. Author: Tay Kratzer taykratzer@taykratzer.com ##
    2. All values must be kept within double quotes. ##

CIFS_USERNAME="administrator" CIFS_PASSWORD="windoze" CIFS_SHARE="share1" CIFS_SERVERIP="137.65.55.210" CIFS_RETRY_COUNT="10" CIFS_RETRY_SECONDS="30" LINUX_MOUNT_POINT="/mnt/winserver"

ncpmnt.conf:

    1. Author: Tay Kratzer taykratzer@taykratzer.com ##
    2. All values must be kept within double quotes. ##

NCP_LOGIN_NAME="admin.acme" NCP_LOGIN_PASSWORD="novell" NCP_VOL="sys" NCPVOL_SERVERIP="137.65.55.210" NCPVOL_SERVERNAME="wwwfs1" NCPVOL_MAP_RETRY_COUNT="10" NCPVOL_MAP_RETRY_SECONDS="30" LINUX_MOUNT_POINT="/mnt/nwserver"

Once you've set up the conf file, run the script from the command line.

umount Unmount filesystem

umount – Unmount a device or mounted filesystem.

testserver:/ # umount /mnt/whatever

Directory Commands

cd Change Directory

Change directory. Tab complete can help traverse the tree.

cd [destination directory]

Change to root directory

cd /

go up one level

cd ..
..

go up two levels

cd ../..
...

du Directory details

Returns an estimate of file space usage, in human readable format (k,M,G). Warning: May take a long time.

du -h

Returns a total size of the directories under where this command was run and sorts by size.

du -hsx * | sort -rh | head -10

md Create a directory

Creates a directory

md [directory name]
mkdir [directory name]

cp -R Copy a directory

Copy a directory recursively "R"

cp -R "source" "destination"

rm -r Remove a directory

Removes a directory and its contents. Warning: irreversible, fast and dangerous if done in the wrong place with wrong switches. [1]

rm -r "directory"

l Directory List

Return a list of files in a directory. This always works (shows file size in bytes)

ls -s

This is a SLES alias of the above

l

Display file size in human-readable values "-h" (K,M,G)

l -h

Sorted by size "S", ascending "r", human-readable "h"

l -Srh

Sorted by time "t", ascending "r", human-readable "h"

l -trh

Show hidden files "a", long list format "l", human-readable "h"

l -alh

Sorted by reverse chonological order l -qaltr

List all open files

lsof

chmod Change Permissions

Permissions are a big deal in Linux [2] not so much in Windows.

Add read and write permissions to User and Group but only read to Other recursively "R" in the destination directory

chmod -R 664 /[destination]

Add Execute, read, and write permissions to User and Group but only read to Other recursively "R" in the destination directory

chmod -R 774 /[destination]

Add all the permissions

chmod -R 777 /[destination]

chown Change Ownership

Ownership of a directory can make a big difference in how Retain works

chown user:group directory/

Change the ownership recursively "R" to user tomcat and group tomcat of the logs directory in the current directory

chown -R tomcat:tomcat logs/

File Commands

=cat Concatenate

Show the contents of a file

cat "filename"

Pipe through more to display a screen full of text at a time (space for next page, enter for next line, ctrl-c to break out)

cat "filename" | more

Pipe through less to display file in less.

cat "filename" | less

find Find a file

Find a file from the root of the file system (aka looks everywhere for it) Warning: may take a long time.

find / -name "fileName"

Find all files with the string "log" as part of the filename.

find . | grep log

Find and recursively unzip all items in a directory structure called zippedDir to the current directory.

find /zippedDir/-name "*.zip" -exec unzip {} \;

Find files in current directory older than 7 days and delete them.

find . -mtime +7 -exec rm {} \;

Find any file off the root and beyond that is larger than 20M:

find / -type f -size +20M -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'

Find the number "wc" lines "l" of files "-type f" in a directory structure:

find [directory]/ -type f | wc -l

Report all known instances of a command. A quick way to find out if something is installed.

whereis [commandname]

cp Copy file

cp origin /destination/file

To copy particular files recursively

cp -r *.txt /destination

To copy to the current directory

cp /software/origin.file .

To secure copy a file to a remote location

scp [source] [destination IP address]:[username]/[destination]

Sync a file or directory between locations, recurse "r" through subdirectories, retain "a" permissions, ownership and timestamps, verbose "v" logging to screen, show progress "P"

rsync -ravP [source] [destination]

mv Move file

Moves file to the destination directory in verbose "v" mode.

mv -v "file" "destination"

rm Remove or delete a file

Removes a particular file

rm "file"

chmod Change Permissions

Permissions are a big deal in Linux [3] not so much in Windows. Add execute "x" rights to all levels on all shell scripts in the current directory

chmod +x *.sh

ln Symbolic link

Make symbolic links between files. This is useful if you need to change the log directory or repair a symbolic link that was removed.

ln -s [path to new directory] ./retain-tomcat7

Network

wget Get File from website

Download the Latest Version of Retain

wget "http://download.gwava.com/download.php?product=Retain&version=current"

ifconfig

Find the IP address of the server. Use this command to find the IP address(es) and MAC address(es) the system uses. eth0 is the first NIC. If the system has multiple NICs, they are named, eth0, eth1, and so on. lo is loopback.

ifconfig

host

look up mx records

host -t MX [domain.com]

Determine hosts

cat /etc/resolv.conf 

ping

Ping to see if a server is alive. Ctrl-c to stop.

ping [ip address]

DNS

DNS

nslookup
server
dig [servername or ipaddress]

Clear DNS Cache

rcnscd stop 

then

rcnscd start

traceroute

Trace the route to a server

traceroute [domain.com]

hostname

Show the system name

hostname

ssh

Secure connect to another system, exit to leave ssh shell

ssh [hostname or ip address]

telnet

Useful for testing connections. You can send a test email with it:

telnet [IP address or DNS hostname] [port]
ehlo [ENTER]
mail from:  retain@[your domain name]  (note: the "from" can actually be spoofed - it really can contain any text of user@domain.com)[ENTER]
rcpt to: [yours or a user's email address][ENTER]
data[ENTER]
subject:  test email from telnet on Retain server[ENTER]
This is a test[ENTER]
.[ENTER]   (you are literally typing a "period" here and pressing [ENTER])
quit[ENTER]

netstat

A useful tool for checking your network configuration and activity.

netstat -lnp -A inet

Process Commands

free Memory usage

Display memory usage

free -m

top System resouces

Display an overview of the system resources can be used to find a process

top

vmstat

Report virtual memory statistics: processes, memory, paging, block IO, traps, and cpu activity.

vmstat

chkconfig

Check runlevel of a process

chkconfig [process name]

ps aux Find a process

To find a running process you know the name of

ps aux | grep [process name]

kill End a process

You have to find a runaway process with top or ps

kill [process ID]

It the process us hung you may have to be more forceful

kill -9 [process ID]

To kill all processes associated with a particualr name

killall -15 [processname]

w Who is logged in

Returns a list of logged in users if the TTY column shows "pts/3" that shows us that use is remotely logged in.

who
w

fstab File system information

The fstab file will also contain information about any mounted filesystems as well

cat /etc/fstab

Linux Version

To find the version of Linux that is installed use this command:

cat /etc/*-release

Utilities

history Command history

Get a history of commands

history

To run a past command

![number of command to run from history]

To run a past command by name

![Ctrl-r][start typing desired command]

tar Create archives

Create an uncompressed tape archive of a bunch of files

tar -cvf [dest file name].tar [source directory] 

Extract a tar

tar -xvf [filename] 

zip/gzip Compress files

Zip

zip [dest file name] [source files or directory] 

Unzip

unzip [file].zip -d /[directory]

Create a gzipped tar file

tar -czvf [dest file name] [source directory] 

Extract a gziped tar

tar -xvzf [filename] 

Extract gzip

gzip -dv [file name]

clear Clear Screen

Clear Screen

clear

crontab Schedule Tasks

crontab – Schedule a command to run at a later time.

Each line in the cron (cron is the daemon) table follows the following format: 7 fields left to right. Field Meaning 1 Minute (0-59) 2 Hour (2-24) 3 Day of month (1-31) 4 Month (1-12, Jan, Feb, ...) 5 Day of week (0-6) 0=Sunday, 1=Monday ... or Sun, Mon, Tue, Wed, Thur, Fri 6 User that the command will run as 7 Command to execute

So, if you want to have Netcleanse restart the ntp-client to reset the time every hour, then you'd use this command:

This will load the crontab editor. Add the entry below, save the change and exit the editor.

crontab -e

Restarts ntp-client every minute

0 * * * * root /etc/init.d/ntp-client restart

Runs the script at the top of every hour

00 */1 * * * /root/Desktop/gopostal/gopostalfullauto.sh

date Date and time

This command can be used to display the current time on the server or adjust the time.

date

rpm Package Installer

Package installer. There are numerous useful flags that can be used with this command. For those flags, look here: http://www.novell.com/coolsolutions/tip/19918.html Most commonly, however, you'll just use the commands to install, upgrade and uninstall.

rpm -ivh [filename].rpm

shutdown Shutdown and reboot

Shutdown the server

shutdown -r now
init 6
reboot -f (Last resort reboot method. The only time you'd want to use this option is if something is wrong with the init daemon.)

vi

vi – Text editor for Linux. There are a variety of commands you can use in vi, here are some of the most useful:

i – insert text ESC – exit mode, for example, exit the insert text mode / – perform a search (n, repeats search; N, search backwards) dd – deletes a line yy – copies current line yw – copies current word u – undo last change p – pastes whatever was copied after the cursor position

wq – writes changes and exits vi
q! – quits without saving changes

To open a file:

vi [filename]

For more details on using vi, look here: [4]

Personal tools
Namespaces

Variants
Actions
Home
Exchange
GroupWise
JAVA
Linux
MTK
Retain
GW Monitoring and Reporting (Redline)
GW Disaster Recovery (Reload)
GW Forensics (Reveal)
GWAVA
Secure Messaging Gateway
GW Mailbox Management (Vertigo)
Windows
Other
User Experience
Toolbox
Languages
Toolbox