Common SSH Commands


  • Applies to: Grid
    • Difficulty: Medium
    • Time Needed: 20
    • Tools Required: SSH
  • Applies to: Legacy DV & VPS Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: SSH
  • Applies to: VPS Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: SSH
  • Applies to: Managed WordPress Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: SSH
  • Applies to: Shared Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: SSH

Overview

This article is an introduction to some basic commands you will want to know while using SSH.

Requirements

This article assumes that:

STATEMENT OF SUPPORT
This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.

Linux folder structure

PLACEHOLDER NAMES
This article will be using the placeholder "00000", "example.com", and "file.html". Be sure to replace this with your actual site number, domain name, and file name.

Linux uses a nested folder structure to store different files. The top-level directory is considered the root directory, and is designated by /. Folders beneath the root level are separated by slashes.

When you first log into your server, you will be in your home directory:

/home/00000/users/.home/

And your website content is located in:

/home/00000/domains/example.com/

Move to another directory

Use this command to move into a directory:

cd

You can specify the full path from the root of the server:

cd /home/00000/domains/

You can type the path from where you are now (leave out the beginning /):

cd folder/

You can go up a directory level:

cd ..

You can return to your home directory:

cd ~

Linux folder structure

PLACEHOLDER NAMES
This article will be using the placeholder "user", "example.com", and "file.html". Be sure to replace this with your actual cPanel Login, domain name, and file name.

Linux uses a nested folder structure to store different files. The top-level directory is considered the root directory, and is designated by /. Folders beneath the root level are separated by slashes.

When you first log into your server, you will be in your home directory:

/home/user/

Your primary website content is located in:

/home/user/public_html/

Your add-on domain(s) content is located in:

/home/user/public_html/example.com

Move to another directory

Use this command to move into a directory:

cd

You can specify the full path from the root of the server:

cd /home/user/public_html/
  

You can type the path from where you are now (leave out the beginning /):

cd folder/

You can go up a directory level:

cd ..

You can return to your home directory:

cd ~

Linux folder structure

PLACEHOLDER NAMES
This article will be using the placeholder "user", "example.com", and "file.html". Be sure to replace this with your actual primary SSH/FTP user, domain name, and file name.

ROOT/SUDO ACCESS:
To access some folders you may need to use root or a sudo user.

Linux uses a nested folder structure to store different files. The top-level directory is considered the root directory, and is designated by /. Folders beneath the root level are separated by slashes.

When you first log into your server, you will be in your home directory:

/home/user/

Your website content is located in:

#Plesk
/home/var/www/vhosts/example.com/httpdocs/
#cPanel
/home/domainuser/public_html/

Move to another directory

Use this command to move into a directory:

cd

You can specify the full path from the root of the server:

cd /home/user/folder/

You can type the path from where you are now (leave out the beginning /):

cd folder/

You can go up a directory level:

cd ..

You can return to your home directory:

cd ~

Linux folder structure

PLACEHOLDER NAMES
This article will be using the placeholder "user", "example.com", and "file.html". Be sure to replace this with your actual primary SSH/FTP user, domain name, and file name.

Linux uses a nested folder structure to store different files. The top-level directory is considered the root directory, and is designated by /. Folders beneath the root level are separated by slashes.

When you first log into your server, you will be in your home directory:

/home/user/

Your website content is located in:


/home/user/html

Move to another directory

Use this command to move into a directory:

cd

You can specify the full path from the root of the server:

cd /home/user/folder/

You can type the path from where you are now (leave out the beginning /):

cd folder/

You can go up a directory level:

cd ..

You can return to your home directory:

cd ~

Where am I?

If you ever need to see exactly which folder you're in, use the following command:

pwd

What's in here?

To see a list of files and folders in our current directory:

ls -alh

ls is the list command, and -alh modifies the standard list in three ways. a means that all files, even hidden files, should be shown. l means that the long format is used - it shows things like file size and the date every file was last modified. h makes the sizes display in convenient units. Here's some sample output:

drwxr-xr-x  3 owner group   15 Oct 21 10:01 .
drwxr-xr-x  6 owner group    6 Oct 21 09:13 ..
-rw-r--r--  1 owner group  137 Oct 21 10:01 .htaccess
drwxr-xr-x  2 owner group    4 Jun  8 17:24 errors
-rwxr-xr-x  1 owner group  379 Jan 28  2010 hello.pl
-rw-r--r--  1 owner group   45 Oct 30  2009 home.html
-rw-r--r--  1 owner group   83 Oct 21 09:47 file.html
-rw-r--r--  1 owner group   68 Jul 20 15:53 phpinfo.php

Linux file structure

Let's break down the elements of a file that are displayed when you run the ls -alh command from the previous section:

-rw-r--r--  1 owner group  83 Oct 21 09:47 file.html
  • -rw-r--r-- - These are the permissions for this file.
    • r stands for read
    • w stands for write
    • x stands for execute
    • The first character is standalone, and the next nine are in groups of three: the first triplet (rw-) applies to the owner, the second (r--) to the group, and the third (r--) to everyone. So, in this example, the owner has read and write access, the group just has read access, and everyone has read access. See File Permissions for an in-depth discussion.
  • 1 - Number of links to this file.
  • owner - Will show the owner of this file.
  • group - Will show the group this file belongs to. May also be www-data.
  • 83 - The size of this file.
  • Oct 21 - The date this file was last modified.
  • index.php - The name of this file.

Change permissions

This section shows basic commands for changing the permission settings on a file. It is highly recommended that you read File Permissions before making any changes, so you'll know what kinds of changes are good and what might be a security risk. Permissions can be changed using chmod, for example:

 chmod 755 file.html

In the above example, 755 is a code which tells what kinds of read, write, and execute permissions you want for the file. The first number is for the owner, second for the group, and third for everyone.

Quick permissions guide:
7 = Read + Write + Execute
6 = Read + Write
5 = Read + Execute
4 = Read
3 = Write + Execute
2 = Write
1 = Execute
0 = All access denied

Change Owner/Group

Use chown to change the owner. Be sure to replace "ownername" with your actual owner:

chown ownername file.html

Use chgrp to change the group. Be sure to replace "groupname" with your actual group:

chgrp groupname file.html

To change the owner and group at once:

chown ownername:groupname file.html

Read file content

To look through a file, the quickest way to get all the contents on your screen is cat, for example:

cat file.html

If you have a large file, the amount of contents can be overwhelming. In that case, you have options to conveniently scroll through content. The less command lets you use the arrow keys to navigate through content, line by line. Press q to exit:

less file.html

The | more command allows you to press Enter to scroll down line by line. Press q to exit.

cat access_log | more

Search file content

To search a file for a specific phrase, use :

cat file.html | egrep "insertphrase"

This will list only the lines containing the phrase you've inserted. Be sure to include the quotation marks (").

Copy

Use the cp command to copy a file. Because we are creating a copy in the same location as the original, we will need to specify a new name in order to create our copy:

cp file.html file.html_copy

You can also copy a file to a new location. Be sure to replace "folder" with your actual folder path:

cp file.html folder/file.html

You can also copy an entire folder (along with all sub-folders using -R):

cp -R folder/ foldercopy

Move/Rename

Use mv to move a file to a new location. Be sure to replace "folder" with your actual folder path:

mv file.html folder/file.html

You can move an entire folder:

mv folder/ folder2

You can also rename a file using mv:

mv file.html 

Create file

Create an empty file (which you can later open for editing) using touch:

touch file.html

CHECK PERMISSIONS:
Files are created with the owner and group of your SSH user. Once you've created a new file, it's a good idea to run ls -alh to make sure its ownership matches the rest of the files in the directory. If not, run the chown command from the earlier section.

Edit file

To edit a file use vi:

vi file.html

USING VI:
Press "i" to enter "insert mode" so you can type and copy/paste. Use the arrow keys to move back and forth in the file. Press "Esc" to exit "insert mode" when you are done modifying the file. Type ":wq" to save and quit. See Understanding basic vi (visual editor) for more details.

Delete file

WARNING
Before deleting a file, be EXTRA CAREFUL to ensure that you do not need the file, AND that you are deleting the correct file. If you are trying to disable a file or folder, we recommend simply renaming it.

To delete a file use rm:

rm file.html

You can recursively delete an entire folder and all its sub-folders using -rf but be VERY CAREFUL when using this command:

rm -rf folder

Zip/Unzip file

Use this command to compress a file. In this example, "folder.zip" is the name of the compressed file we wish to create, and "folder" is the name of the directory we wish to compress:

zip -r folder.zip folder

To decompress simply use:

unzip folder.zip

Disk use

You can view your total server disk usage by using:

df -h

To show all folder sizes for the current directory recursively, with their sizes. This may take time if you are in a high directory:

du -h

To show a disk use summary for the current directory. This may take time if you are in a high directory:

du -sh

An advanced find command you can run to find files over 10 MB. You can adjust the 10 MB variable to your needs:

find / -mount -noleaf -type f -size +10000k -print0 | xargs -0 ls -lhSr | awk '{printf "%*s %s\n",7,$5":",$9}'

Processes and System Services

To show current server proceses:

ps -auxf

To show processes and memory use live:

top

To start/stop/restart services. Replace "servicename" with your actual service:

service servicename start
service servicename stop
service servicename restart

Log Files

Log files can tell you a lot about what's happening on your server. Log files are generally very long, so you should use one of these commands to sort through them easily. Make sure to replace "/path/to/logs" with your actual file path.

To show the most recent 100 lines in a file:

tail -n 100 /path/to/logs

To watch the log files propogate live:

tail -f /path/to/logs

To see your SSH history use:

history

NOTE:

Final tips

  • When you are typing a path or file name, hit "Tab" after the first few letters. If it's the only file or folder matching the letters you've typed, the rest of it will auto-complete.
  • Hit the up arrow to scroll back through previous commands - save yourself some typing!
  • Always make a backup copy of the working version of a file before editing it.
  • "q" or "CTRL+C" usually gets you out of any special mode you might be in.
  • If you've encountered an unknown command, type "man" and then the command name to learn more about it. (Example: man ls) This will also show you special options like the -alh option for the list command.