WebHostGear.com - the hosting resource for professionalshosting tutorials 
hosting howto webhost guide server managementNovember 21, 2008
server management, apache tutorials, hosting tutorials, cpanel, server security
Home / Hosting Tutorials / Common SSH Commands - Linux Shell Commands

Common SSH Commands - Linux Shell Commands



Printer Friendly Printer Friendly Send to a friend Send to a friend
By : ramprage Rating : Average Rating : 6.86 From 636 Voter(s)

ear.

Updated: Jan 19, 2007: Added some new ssh commands for the linux shell.

We've put together some of the more frequently used SSH commands or linux shell commands, and organized them by name so you can easily find a command, their description and how to use it. This guide will continue to be updated and should not be considered a complete list of SSH commands or linux shell commands, but commands, we found, often used. If you would like to add to this guide, please email us and let us know.

Common SSH Commands or Linux Shell Commands,
ls : list files/directories in a directory, comparable to dir in windows/dos.
ls -al : shows all files (including ones that start with a period), directories, and details attributes for each file.

cd : change directory · · cd /usr/local/apache : go to /usr/local/apache/ directory
cd ~ : go to your home directory
cd - : go to the last directory you were in
cd .. : go up a directory cat : print file contents to the screen

cat filename.txt : cat the contents of filename.txt to your screen

chmod: changes file access permissions
The set of 3 go in this order from left to right:
USER - GROUP - EVERONE

Article provided by WebHostGear.com

0 = ---  No permission
1 = --X  Execute only
2 = -W-  Write only
3 = -WX  Write and execute
4 = R--  Read only
5 = R-X  Read and execute
6 = RW-  Read and write
7 = RWX  Read, write and execute

Usage:
chmod numberpermissions filename

chmod 000 : No one can access
chmod 644: Usually for HTML pages
chmod 755: Usually for CGI scripts


chown: changes file ownership permissions
The set of 2 go in this order from left to right:
USER - GROUP

chown root myfile.txt : Changes the owner of the file to root
chown root.root myfile.txt : Changes the owner and group of the file to root


tail : like cat, but only reads the end of the file
tail /var/log/messages : see the last 20 (by default) lines of /var/log/messages
tail -f /var/log/messages : watch the file continuously, while it's being updated
tail -200 /var/log/messages : print the last 200 lines of the file to the screen

more : like cat, but opens the file one screen at a time rather than all at once
more /etc/userdomains : browse through the userdomains file. hit Spaceto go to the next page, q to quit

pico : friendly, easy to use file editor
pico /home/burst/public_html/index.html : edit the index page for the user's website.


File Editing with VI ssh commands
vi : another editor, tons of features, harder to use at first than pico
vi /home/burst/public_html/index.html : edit the index page for the user's website.
Whie in the vi program you can use the following useful commands, you will need to hit SHIFT + : to go into command mode

:q! : This force quits the file without saving and exits vi
:w : This writes the file to disk, saves it
:wq : This saves the file to disk and exists vi
:LINENUMBER : EG :25 : Takes you to line 25 within the file
:$ : Takes you to the last line of the file
:0 : Takes you to the first line of the file

grep : looks for patterns in files
grep root /etc/passwd : shows all matches of root in /etc/passwd
grep -v root /etc/passwd : shows all lines that do not match root

ln : create's "links" between files and directories
ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf : Now you can edit /etc/httpd.conf rather than the original. changes will affect the orginal, however you can delete the link and it will not delete the original.


last : shows who logged in and when
last -20 : shows only the last 20 logins
last -20 -a : shows last 20 logins, with the hostname in the last field

w : shows who is currently logged in and where they are logged in from.
who : This also shows who is on the server in an shell.

netstat : shows all current network connections.
netstat -an : shows all connections to the server, the source and destination ips and ports.
netstat -rn : shows routing table for all ips bound to the server.

top : shows live system processes in a nice table, memory information, uptime and other useful info. This is excellent for managing your system processes, resources and ensure everything is working fine and your server isn't bogged down.
top then type Shift + M to sort by memory usage or Shift + P to sort by CPU usage

ps: ps is short for process status, which is similar to the top command. It's used to show currently running processes and their PID.
A process ID is a unique number that identifies a process, with that you can kill or terminate a running program on your server (see kill command).
ps U username : shows processes for a certain user
ps aux : shows all system processes
ps aux --forest : shows all system processes like the above but organizes in a hierarchy that's very useful!

touch : create an empty file
touch /home/burst/public_html/404.html : create an empty file called 404.html in the directory /home/burst/public_html/

file : attempts to guess what type of file a file is by looking at it's content.
file * : prints out a list of all files/directories in a directory

du : shows disk usage.
du -sh : shows a summary, in human-readble form, of total disk space used in the current directory, including subdirectories.
du -sh * : same thing, but for each file and directory. helpful when finding large files taking up space.

wc : word count
wc -l filename.txt : tells how many lines are in filename.txt

cp : copy a file
cp filename filename.backup : copies filename to filename.backup
cp -a /home/burst/new_design/* /home/burst/public_html/ : copies all files, retaining permissions form one directory to another.
cp -av * ../newdir : Copies all files and directories recurrsively in the current directory INTO newdir

mv : Move a file command
mv oldfilename newfilename : Move a file or directory from oldfilename to newfilename

rm : delete a file
rm filename.txt : deletes filename.txt, will more than likely ask if you really want to delete it
rm -f filename.txt : deletes filename.txt, will not ask for confirmation before deleting.
rm -rf tmp/ : recursively deletes the directory tmp, and all files in it, including subdirectories. BE VERY CAREFULL WITH THIS COMMAND!!!

TAR
: Creating and Extracting .tar.gz and .tar files
tar -zxvf file.tar.gz : Extracts the file
tar -xvf file.tar : Extracts the file
tar -cf archive.tar contents/ : Takes everything from contents/ and puts it into archive.tar
gzip -d filename.gz : Decompress the file, extract it

ZIP Files:  Extracting .zip files shell command
unzip file.zip


Firewall - iptables commands
iptables -I INPUT -s IPADDRESSHERE -j DROP : This command stops any connections from the IP address
iptables -L : List all rules in iptables
iptables -F : Flushes all iptables rules (clears the firewall)
iptables --save : Saves the currenty ruleset in memory to disk
service iptables restart : Restarts iptables

Apache Shell Commands
httpd -v : Outputs the build date and version of the Apache server.
httpd -l : Lists compiled in Apache modules
httpd status : Only works if mod_status is enabled and shows a page of active connections
service httpd restart : Restarted Apache web server

MySQL Shell Commands
mysqladmin processlist : Shows active mysql connections and queries
mysqladmin drop databasenamehere : Drops/deletes the selected database
mysqladmin create databasenamehere : Creates a mysql database

Restore MySQL Database Shell Command
mysql -u username -p password databasename < databasefile.sql : Restores a MySQL database from databasefile.sql

Backup MySQL Database Shell Command
mysqldump -u username -p password databasename > databasefile.sql : Backup MySQL database to databasefile.sql




kill: terminate a system process
kill -9 PID EG: kill -9 431
kill PID
EG: kill 10550
Use top or ps ux to get system PIDs (Process IDs)

EG:

PID TTY TIME COMMAND
10550 pts/3 0:01 /bin/csh
10574 pts/4 0:02 /bin/csh
10590 pts/4 0:09 APP

Each line represents one process, with a process being loosely defined as a running instance of a program. The column headed PID (process ID) shows the assigned process numbers of the processes. The heading COMMAND shows the location of the executed process.

Putting commands together
Often you will find you need to use different commands on the same line. Here are some examples. Note that the | character is called a pipe, it takes date from one program and pipes it to another.
> means create a new file, overwriting any content already there.
>> means tp append data to a file, creating a newone if it doesn not already exist.
< send input from a file back into a command.

grep User /usr/local/apache/conf/httpd.conf |more
This will dump all lines that match User from the httpd.conf, then print the results to your screen one page at a time.

last -a > /root/lastlogins.tmp
This will print all the current login history to a file called lastlogins.tmp in /root/

tail -10000 /var/log/exim_mainlog |grep domain.com |more
This will grab the last 10,000 lines from /var/log/exim_mainlog, find all occurances of domain.com (the period represents 'anything',
-- comment it out with a so it will be interpretted literally), then send it to your screen page by page.

netstat -an |grep :80 |wc -l
Show how many active connections there are to apache (httpd runs on port 80)

mysqladmin processlist |wc -l
Show how many current open connections there are to mysql

New! - Need server help? Hire an Expert

Get professional help with your configuration, script installation or server issue.
Learn how we can help you with any server problem and make your server run like new. Professional staff will contact you, after submitting a quote request, by phone or email.

Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent

Related Articles


» Official Cpanel Newbie Guide
» Subdomain to Separate IP
» Disable Direct Root Logins
» Upgrade Guide From Red Hat 7.3 to 9.0
» Securing Your /tmp Partition with Cpanel/WHM
» Need Help? Hire An Expert - Free Quote
» Nobody Check Security Tool
» How to install Shoutcast
» Windows Rootkit Detectors


Discuss this article with others in our new hosting forums

Comments / Feedback

chris
Re: vi you should include how to stop/quit and alos kill "vi" via the command line as this happens more than it should and if left running it will use most srv CPU recources.
Jia
If I have to type in several commands repeatedly, is there a button I can press to automatically have them typed on the command line instead of having to repeatedly type the same thing over and over again?
Dhiraj
yes Jai Pressing the upper arrow key will scroll through all the previously typed commands. Hope this helps.
Boris
Excellent for n00bs...
tt
Ctrl plus C works well to stop alot of srolling..

Hope it helps

thanks great site GUYS
c.jayanthi
it is very usefull for beginners.if you put all these information with example , it will be very nice.
mafiaboy
move: move or rename file
mateen
Dear Sir my name is Mateen so i am from afghanistan so now a days i installed linux server so I face tothe problem concerning the ACL in Linux 9.0 server if you help me and in Access control if you help me in this regard I will be appreciate you .

I am looking forward for you response

Kind regards
Mateen
Andrius
Greetings

What is the command to know what files are in use by particular user.

I ask this coz I want to know what files do belong to me in my server (I am not an admin)

And I would like to know how much hdd space all my files take..
floating carcass
hi,
I would like to know what ports are active/being used currently. Is there some way other than nmap...
fc
eddie
Andrius - simply type ll

that's (LL)
kim
what is the command to create a directory?
mk dir is not working for me...
frank
Hi, what command can be used to recover the deleted files. Some times i accidentally issue a command in a certain directory, thinking that i am in another directory working on files that i don't like, only to realize when i have deleted all files that directort.
I would like to know it.....
Andy
Hi, I have a php script that optimizes/cleans my database and I was wondering what command I would use to run it automatically using a cron. I tried using cat but all that did was send me the source code to the file. (I use cpanel)
niXin
Hi - Im using FreeBSD, I have 6 colums of data in mail dir . I wanna check witch mailbox is the biggest eg: ls mailbox | sort -4 hope it's clear ...:) sorry if it's not
newbie
Floating carcass, are you using mk dir with a space between mk and dir? if so, it's just one string: mkdir
Vijay Modi
Very Good Article
karunakar
last -a > /root/lastlogins.tmp
Computer Guru
Great article for the n00bs, good job..

Can't believe you forgot CHOWN and CHMOD!!!!!

The most important commands for most people doing uploads and installing ready-made scripts... CHOWN sets the owner of a file, folder, directory, etc.

CHMOD sets the permissions...

To find out what CHMOD permissions you need go to NeoSmart.net/DC.htm and get CHMOD-Win, which tells you which CHMOD number gives you the settings you need in plain text...

Other than that, cheers!
L. Young
I'm a webhost, is SSH a easy system to use for hacking in our servers?

also, is there any logs made of logins to SSH?

can you email me with the answers, and if anyone wants to test security on our servers in return for hosting, etc?

Cheers,
Liam
darkangel
I would like a ssh command that could show me the traffic on the server!!
Philip Snyder
This is kind of a more advanced problem, but one I'm sure everyone will run into at one point or another. I do web development on a linux box with apache serving from another box behind a firewall. I can ssh TO the server, but not vice-versa.

On my server, my php code spits out debug messages and error messages to a couple different log files. I want to tail these log files on my dev machine so I can watch the output while I develop. If I were content with just using tail, I could do the following:

ssh user@server tail -f logs/my.log

However, I want to use multitail (which allows regex generated color highlighting, etc)... and multitail is only installed on my dev machine.

I know this can be done, but its not particularly easy. Personally, I'm still working on it -- if anyone knows how, let me know. Otherwise, I'll figure it out and post it here. ;) I'm looking at ssh w/ pipes for the solution right now.. so here's how you make a pipe (think of it like a water pipe... the first drops of water in are the first drops of water out the other end -- hence the fifo part of mkfifo):

% mkfifo my.pipe
Anoop PK
Great !! Very Helpful
brindha
goood , add some more to the archieve
Jordan
That made no sense
kerk
is there a command to back up a mysql table?
sagar
Very Good Article
jhon
id is one of the best collection of commands of shh

thanks.
adeyemi
hello,
thanks for the oppotunity giving to us by reading some of this few dumps command online. pls sir i will like you to send me more command basically on unix system.
sandy
Nice
goamind
Great article!
But I want to know the command for uploading my files to a remote server that I connected with using
ssh -l user_name domain.com
Steve
To run a php script on a linux server that has lynx (a text based web browser)

* * * * * lynx -dump http://yoursite.com/cron.php
Aram
I need help on the part where i'm in the vi server.cfg. I've edited everything in there except the part where i need to SAVE it. PleasE HELP ME !!!
fani
How to downlaod a file from a remote system using ssh.
Actually i ssh'd into a system and i need a file to be downloaded on my pc or any other computer, I dont have any ftp server running on my pc and the system running sshd don't as the ftp either. what are the days to download the file
selva
It is very usefull for beginners.
Jack
As a newb to SSH usage on my servers I found some of these commands very helpfull (Thanks)

I'm still searching for one command and would appreciate if anyone can help me out.

I'm installing a site which is packed in filename.tar.gz , I unpack .tar without a problem but cant find a command to unpack from .gz

Thanks in advance.
Ghzanfar
Its Nice effort for all of Linux Lovers. But i've a problem that i want to get those commands which perform the same function on two or more shells of linux but their name is different. If some one can help?
Paul
Great stuff! Found exactly what I was looking for.

I esp. like this article/tutorial because it contains the BASIC stuff that a person need ;)
Ben Faust
The answer to the question of running a PHP script in a CRON:

php /absolute/path/to/script.php
james gideon

Hi,

Above listed command are very usefful for biggenner's,Please give more with examples like how they used in sever.

Thanks.
Praveen
Yes, very nice commands for new user which I am. These commands are very helpful. Pls send me more basic and commands which are used on newtork.

thanks
Kodiak
Thanks for this! Very helpful for this newbie. The article that referred me here had all sorts of errors - this one's nice and clean (apart from that one apostrophe: "file : attempts to guess what type of file a file is by looking at it's content" - it's should be its...)

Thanks!
joey
I haven't a clue about all this- I'm just starting out with what all the jargon means for personal server management purposes. I hope a year from now I'm well acquainted with this field and will be laughing at how little I knew!
Karl
Samne problem as Aram here... i can get into Vi (vim) and create the file but how do i save the file and exit without leaving the file in the swapfile. Do you have a list of te commands used in vim editor?

Regards.
danc
who knows how i can send messages through ssh console to the other user logged on a server also with ssh console. i know it can be done because i did it some time ago but i forgot and now i can't find out how.
Steve
Thanks for the feedback everyone, I've updated the SSH commands including many of the things you mentioned int the comments.

Cheers
kari
I also had a hard time w/ the vi section. A quick google search lead me to this site: http://www.cs.colostate.edu/helpdocs/vi.html
key will bring you out of insert mode, then you can enter the file commands (i.e. :wq) to save and whatnot.
Amit
Simpy great!!
soheil shahabi
Very nice, very basic yet vey useful, if would be nice if it was updated in a way and more things were added.
ndina
simpy and greet
spayced
very useful. except touch isn't specifically made for creating files, it's use is for updating the last modified timestamp. It's just a coincidence updating the timestamp for a nonexistant file creates the file :P
Nirmal
Very usful for starters,

I just brought VDS server, and got SSH, and this is gonna be very much helpful for me!

Cheers!
joey
Your site is great!
josh
kim the command is mkdir no spacing
sadman
nice tutorial, a building block for mastery... Ü
amrita
it's really great
Lê Quang Nhật Anh
Thanks a lot for this
deliq
thank you ! Thats very Helpfull for me !
kandrei
power corrupted my mail server and i lost all my old mails on the system. i secured users mail directories and my system configs. After reinstallation, I could only manage to get mails in their new mails folder en couldnot get the old ones how can i recover the old mails ?
amir.mozaffari
I Need Help In SSH And SQUID
tnak you .
fahad
Thanks a lot for this
Zafar
Goods Commands For Initials.
Jason
I was actually trying to figure out how to create a folder, then create a file, and stuff like that.
brahmam
i have always been thankful to you.Because it is very much help to me. Please add some more commands to this web. Ok
kro
HOW DO I CLOSE A PROGRAM I JUST EXECUTED USING SSH TO MY IPHONE??
Mohan G
Good Site !
Provides lot of info for the new bi's !!
:)
kay
*********************************

*********************************

Hi,

I had to pay for a private server because I own many websites but I don't know how to find which domains put high load on the server when it happens, can you tell me what to do or where to find a tutorial about this ?


Thanks.


*********************************

*********************************
rafiq
**********************
Hi,
This Site Is Very Helpfull For The Begainers
Thanks A Lot
rekha
Very good method of explaining.Post some more details regarding Mysql.
santa
How can I download a file from my server to my PC using SSH?
olive
Can anyone specify the syntax how to set a .swf file in a cron scheduler. Whether its possible to set swf in a cron
hellu
Hi,

I found this site very helpful as a new beginner.
thanks a lot.

 Add Comment
Name
Email
Image Code
Refresh Image

Comments / Feedback



Web Hosting News RSS ?


WebHostGear Hire an Expert - NEW!
Let us improve your servers performance, find that spammer and take care of that kernel upgrade. Hire us to help with any tutorials listed on the site or any other services needed. Get your free, NO obligation quote now

Our site offers free hosting tutorials, cpanel tutorial, web hosting news, shell commands, running a web hosting business, dedicated guides, linux tutorial, apache install, home web server, web server guide, ssh commands, dedicated servers, DNS nameservers, chkrootkit, apf firewall, exim configuration, server compromised, cron backup solution, ftp backup script

Server Tutorials


WebHostGear Reviewed by Ping Zine - Click here

Special Offer:


Links:
cPanel server administration

MidPhase Coupons

Reseller Hosting

Reseller Hosting FAQ

Icon

Web Hosting

Datacenter Discussion Forum

Lunarpages Coupon

Hosting Coupon



WebhostGear Sponsors
Going Up Advertise Hosting Free Uptime Check Web Hosting Chat Icons Banners Mall