Thursday, November 4, 2010

Linux Commands

Basics Commands


CommandExampleDescription
cat
Sends file contents to standard output. This is a way to list the contents of short files to the screen. It works well with piping.

cat .bashrcSends the contents of the ".bashrc" file to the screen.
cd
Change directory

cd /homeChange the current working directory to /home. The '/' indicates relative to root, and no matter what directory you are in when you execute this command, the directory will be changed to "/home".

cd httpdChange the current working directory to httpd, relative to the current location which is "/home". The full path of the new working directory is "/home/httpd".

cd ..Move to the parent directory of the current directory. This command will make the current working directory "/home.

cd ~Move to the user's home directory which is "/home/username". The '~' indicates the users home directory.
cp
Copy files

cp myfile yourfileCopy the files "myfile" to the file "yourfile" in the current working directory. This command will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it exists.

cp -i myfile yourfileWith the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.

cp -i /data/myfile .Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt before overwriting the file.

cp -dpr srcdir destdirCopy all files from the directory "srcdir" to the directory "destdir" preserving links (-p option), file attributes (-p option), and copy recursively (-r option). With these options, a directory and all it contents can be copied to another directory.
dddd if=/dev/hdb1 of=/backup/ Disk duplicate. The man page says this command is to "Convert and copy a file", but although used by more advanced users, it can be a very handy command. The "if" means input file, "of" means output file.
df
Show the amount of disk space used on each mounted filesystem.
lessless textfileSimilar to the more command, but the user can page up and down through the file. The example displays the contents of textfile.
ln
Creates a symbolic link to a file.

ln -s test symlinkCreates a symbolic link named symlink that points to the file test Typing "ls -i test symlink" will show the two files are different with different inodes. Typing "ls -l test symlink" will show that symlink points to the file test.
locate
A fast database driven file locator.

slocate -uThis command builds the slocate database. It will take several minutes to complete this command. This command must be used before searching for files, however cron runs this command periodically on most systems.

locate whereisLists all files whose names contain the string "whereis".
logout
Logs the current user off the system.
ls
List files

lsList files in the current working directory except those starting with . and only show the file name.

ls -alList all files in the current working directory in long listing format showing permissions, ownership, size, and time and date stamp
more
Allows file contents or piped output to be sent to the screen one page at a time.

more /etc/profileLists the contents of the "/etc/profile" file to the screen one page at a time.

ls -al |morePerforms a directory listing of all files and pipes the output of the listing through more. If the directory listing is longer than a page, it will be listed one page at a time.
mv
Move or rename files

mv -i myfile yourfileMove the file from "myfile" to "yourfile". This effectively changes the name of "myfile" to "yourfile".

mv -i /data/myfile .Move the file from "myfile" from the directory "/data" to the current working directory.
pwd
Show the name of the current working directory

more /etc/profileLists the contents of the "/etc/profile" file to the screen one page at a time.
shutdown
Shuts the system down.

shutdown -h nowShuts the system down to halt immediately.

shutdown -r nowShuts the system down immediately and the system reboots.
whereis
Show where the binary, source and manual page files are for a command

whereis lsLocates binaries and manual pages for the ls command.

LAN and Internet exploration tools related Commands

1) telnet   Telnet allows you to login remotely from a remote computer to a host server running any unix or unix clone system. Other variation is called RLOGIN/rlogin. A newer variation also allows you to login more securely using the secure shell (SSH). 2) who Shows you who else is currently logged in on your ISP’s LAN. Other good commands to explore the other users on your LAN are “w,” “rwho, ” “users.” Example: ======== kishore.kb@ws173:~$ who kishore.kb tty7 2010-11-05 00:40 (:0) kishore.kb pts/0 2010-11-05 00:46 (:0.0) kishore.kb pts/1 2010-11-05 00:51 (:0.0) ======== 3) netstat All sorts of statistics on your LAN, including all Internet connections. For real fun, try “netstat -r” to see the kernel routing table. However, be careful. I was teaching a friend the basics of summing up a Unix system and I told her to do that and ‘ifconfig’. She was booted off the system the next day for ‘hacker suspicion’ even though both are legitimate commands for users.” 4) whois Get lots of information on Internet hosts outside you LAN. IF you type whois it will display all the information about the domain like its registrar, status of the domain, the domain owner's contact information. etc Example:  ================ kishore.kb@ws173:~$ whois poornam.com Whois Server Version 2.0 Domain names in the .com and .net domains can now be registered with many different competing registrars. Go to http://www.internic.net for detailed information. Domain Name: POORNAM.COM Registrar: ENOM, INC. Whois Server: whois.enom.com Referral URL: http://www.enom.com Name Server: NS.POORNAM.COM Name Server: NS1.POORNAM.COM Name Server: NS2.POORNAM.COM Status: ok Updated Date: 11-oct-2010 Creation Date: 09-nov-1999 Expiration Date: 09-nov-2011 Domain name: poornam.com Registrant Contact: Poornam Info Vision Pvt. Ltd. Poornam Info Vision Pvt. Ltd. () Fax: 'leka', 5th Cross Road,, Keerthi Nagar, Elamakara Cochin, Kerala 682026 IN Administrative Contact: Poornam Info Vision Pvt. Ltd. Amarjyoti Krishnan (amar@POORNAM.COM) Kerala Fax: IN leka Keerthi Nagar, Elamakara , Cochin
================
5) nslookup Get a whole bunch more information on other Internet hosts.  Example: ============  kishore.kb@ws173:~$ nslookup poornam.com Server: 172.17.254.1 Address: 172.17.254.1#53 Non-authoritative answer: Name: poornam.com Address: 140.99.18.32 ============ 6) dig Even more info on other Internet hosts. Nslookup and dig are not redundant. dig will give the Ip address of the domain and also the nameserver details. Example: ===========  kishore.kb@ws173:~$ dig kishore.com ;; QUESTION SECTION: ;kishore.com. IN A ;; ANSWER SECTION: kishore.com. 3600 IN A 168.144.145.204 ;; AUTHORITY SECTION: kishore.com. 3600 IN NS ns4.softcomca.com. kishore.com. 3600 IN NS ns3.softcomca.com. ===========  In the answer section the A record 168.144.145.204 is the IP address the domain kishore.com is pointing to.

7) finger Not only can you use finger inside your LAN. It will sometimes get you valuable information about users on other Internet hosts. I have attached one example below: =======  kishore.kb@ws173:~$ finger Login Name Tty Idle Login Time Office Office Phone kishore.kb Kishore Kumar Banjari tty7 2:57 Nov 5 00:40 (:0) kishore.kb Kishore Kumar Banjari pts/0 2:50 Nov 5 00:46 (:0.0) kishore.kb@ws173:~$ finger -lmsp Login: kishore.kb Name: Kishore Kumar Banjari Directory: /afs/.cpiv.com/techhome/kishore.kb Shell: /bin/bash On since Fri Nov 5 00:40 (IST) on tty7 from :0 2 hours 59 minutes idle On since Fri Nov 5 00:46 (IST) on pts/0 from :0.0 2 hours 51 minutes idle ======= 8) ping Find out if a distant computer is alive and run diagnostic tests -- or just plain be a meanie and clobber people with pings. (I strongly advise *against* using ping to annoy or harm others.) Example:  kishore.kb@ws173:~$ ping google.com PING google.com (209.85.231.104) 56(84) bytes of data. 64 bytes from maa03s01-in-f104.1e100.net (209.85.231.104): icmp_seq=1 ttl=57 time=39.1 ms 64 bytes from maa03s01-in-f104.1e100.net (209.85.231.104): icmp_seq=2 ttl=57 time=41.9 ms If the host is reachable there will be no packet loss. 9) traceroute Kind of like ping with attitude. Maps Internet connections, reveals routers and boxes running firewalls. 10) ftp Use it to upload and download files to and from other computers. Some commands used in the ftp prompt are given below:  get --> to copy one file from the remote machine to local machine.put --> to put one file froem local machine to remote machine.ls --> Its shows the list of files and folders inside the current directory.mget and mput are for multiple file copy.bye --> to exit the FTP environment(same as exit)11) SSH - It's a secure shell remote connection to the remote host: Example: ssh root@192.168.148.142 or ssh -l root 192.168.148.142 or
ssh root@linuxconfig.org_remote 
 
For troubleshooting we can us the ssh command as  
"ssh -vvv root@linuxconfig.org_remote"
 
Some Important Linux System Monitoring Tools:

1. top 
The top program provides a dynamic real-time view of a running system i.e. actual process activity. By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds.

Example:
=========
kishore.kb@ws173:~$ top

top - 05:42:25 up  5:02,  3 users,  load average: 0.56, 0.60, 0.56
Tasks: 154 total,   2 running, 152 sleeping,   0 stopped,   0 zombie
Cpu(s):  5.0%us,  1.8%sy,  0.0%ni, 93.2%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1017000k total,   994244k used,    22756k free,    10484k buffers
Swap:  2000052k total,    61872k used,  1938180k free,   257564k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                        
 3467 kishore.  20   0  616m 289m  26m S    6 29.2  64:52.40 firefox-bin                                                    
 3271 kishore.  20   0  160m 4004 2968 S    3  0.4   5:22.56 pulseaudio                                                     
 6150 kishore.  20   0  247m 141m  17m S    2 14.2   1:41.65 opera     
=========


2.vmstat:
 The command vmstat reports information about processes, memory, paging, 
block IO, traps, and cpu activity.

Example:kishore.kb@ws173:~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 1  0  63332  35432  10488 247272    0    2    17    32  456  287 14  3 83  0


--> Display Memory Utilization Slabinfo:

vmstat -m

--> Get Information About Active / Inactive Memory Pages.

vmstat -a





3.w - Find Out Who Is Logged on And What They Are Doing
  
w command displays information about the users currently on the machine, and their processes.

Example:
kishore.kb@ws173:~$ w

==========
 06:05:20 up  5:25,  3 users,  load average: 0.51, 0.48, 0.50
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
kishore. tty7     :0               00:40    5:25m 26:54   0.15s x-session-manager
kishore. pts/0    :0.0             00:46    5:17m 19.75s 19.75s ssh kishore.kb@im.inhouse.net -X
kishore. pts/1    :0.0             00:51    0.00s  0.10s  0.00s w
==========



4. uptime - Tell How Long The System Has Been Running.The uptime command can be used to see how long the server has been running. The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.

Example:
==========
 kishore.kb@ws173:~$ uptime
 06:08:06 up  5:27,  3 users,  load average: 0.40, 0.46, 0.48
==========


5.ps - Displays The Processes. ps command will report a snapshot of the current processes. To select all processes use the -A or -e option:

Example:
 ======
 kishore.kb@ws173:~$ ps
  PID TTY          TIME CMD
 3880 pts/1    00:00:00 bash
 6889 pts/1    00:00:00 ps
=====

6. free - Memory Usage
 The command free displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.

For showing in the Mb format free -m is use
To see in the Kilobyte  for mat free -k is used
To see in the byte format we can use free -b

Example:
=========
 kishore.kb@ws173:~$ free -m
             total       used       free     shared    buffers     cached
Mem:           993        946         46          0         11        230
-/+ buffers/cache:        704        288
Swap:         1953         63       1889

kishore.kb@ws173:~$ free -k
             total       used       free     shared    buffers     cached
Mem:       1017000     964132      52868          0      11844     236228
-/+ buffers/cache:     716060     300940
Swap:      2000052      64788    1935264

kishore.kb@ws173:~$ free -b
             total       used       free     shared    buffers     cached
Mem:    1041408000  987193344   54214656          0   12128256  241897472
-/+ buffers/cache:  733167616  308240384
Swap:   2048053248   66342912 1981710336
=========

7.iostat - Average CPU Load, Disk Activity.

The command iostat report Central Processing Unit (CPU) statistics and input/output statistics for devices, partitions and network filesystems (NFS).

8.sar - Collect and Report System Activity

The sar command is used to collect, report, and save system activity information. To see network counter, enter: 
# sar -n DEV | more

9. mpstat - Multiprocessor Usage

The mpstat command displays activities for each available processor, processor 0 being the first one. mpstat -P ALL to display average CPU utilization per processor:

# mpstat -P ALL

Linux 2.6.18-128.1.14.el5 (www.kishore.in)   06/26/2009

06:48:11 PM  CPU   %user   %nice    %sys %iowait    %irq   %soft  %steal   %idle    intr/s
06:48:11 PM  all    3.50    0.09    0.34    0.03    0.01    0.17    0.00   95.86   1218.04
06:48:11 PM    0    3.44    0.08    0.31    0.02    0.00    0.12    0.00   96.04   1000.31
06:48:11 PM    1    3.10    0.08    0.32    0.09    0.02    0.11    0.00   96.28 

10.pmap - Process Memory Usage

The command pmap report memory map of a process. Use this command to find out causes of memory bottlenecks.
 # pmap -d PID

11. netstat and ss  :Network Statistics
 The command netstat displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. ss command is used to dump socket statistics. It allows showing information similar to netstat.

Example:
==========
kishore.kb@ws173:~$ netstat
 unix  3      [ ]         STREAM     CONNECTED     6769   
unix  3      [ ]         STREAM     CONNECTED     6668     @/var/run/hald/dbus-P2JVK3kDjb
unix  3      [ ]         STREAM     CONNECTED     6419   
unix  3      [ ]         STREAM     CONNECTED     6312     @/var/run/hald/dbus-7EXcXjhyqS
unix  3      [ ]         STREAM     CONNECTED     6311   
unix  3      [ ]         STREAM     CONNECTED     6280     /var/run/dbus/system_bus_socket
=========

Hope you have got some ideas regarding the Linux commands.

Kishore

No comments:

Post a Comment