Support / KnowledgeBase

 
Search the KnowledgeBase Search

Common SSH Commands

  • Applies to: All DV, DV, DV Developer, Grid

  • Difficulty: Medium

  • Time needed: 5 minutes

  • Tools needed: ssh

 
  • Applies to: All DV
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: SSH
  • Applies to: Grid
    • Difficulty: Medium
    • Time Needed: 20
    • Tools Required: SSH

Overview

This article is an introduction to finding your way around your server in SSH. It takes a Day 1 approach to SSH.

  • Hit Enter or Return after every command unless specified otherwise.
  • The domain example.com should always be replaced with your own domain name.
  • Whenever you see 00000, you should replace it with your site number.
  • Example paths and file names should also be replaced with your own server information.

Requirements

This article assumes that:

  1. You have enabled your SSH user, which should be the same as your primary domain.
  2. You have enabled an SSH user. This is usually root or a sudo user.
  3. You have used Terminal (Mac) or PuTTY (Windows) to log into the server. You should be looking at a command prompt.

If you need help with these steps, please see: Connecting via SSH to your serverConnecting via SSH to your server.

READ ME FIRST

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.

READ ME FIRST

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.

Home and navigation

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

  • This is /home/00000/users/.home/.
  • For the root user, this is /root/.

Linux folder structure

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. For example, all of your website content is located in /home/00000/domains/example.com//var/www/vhosts/example.com/.

TIP:

On the Grid, you can't do anything above your server directory. For practical purposes, you will always be in /home/00000/ or lower. However, starting your paths from / can still be useful for navigation.

Your home directory is two levels below your server directory. If you want to see the same folders that you see in FTP, run this command when you first log in:

cd /home/00000/

Or, if you don't want to look up your site number, this command will jump you up two levels:

cd ../..

TIP:

Remember that / is not the same as /root/. The / is the root level of the server. You cannot get any higher than that. The home directory for the user called root is /root/.

Common paths:

  • /home/00000/domains/example.com/html/ /var/www/vhosts/example.com/httpdocs/ - web document root
  • /home/00000/logs//var/www/vhosts/example.com/statistics/logs/ - per-domain access and error logs
  • /var/www/vhosts/example.com/conf/ - per-domain configuration files
  • /home/00000/etc/ - location of php.ini file/etc/ - configuration files
  • /etc/init.d/ - software daemon start files

Move into 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/
cd /var/www/vhosts/example.com/conf/

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

cd downloads/

You can go up a directory level:

cd ..

You can go to your home directory:

cd ~

Where am I?

Working with your server without graphics can take a little getting used to. If you ever need to see exactly which folder you're in, use the following command:

pwd

It stands for print working directory. You'll see output like this:

/home/00000/data/
/var/www/vhosts/

What's here?

Next, we want 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 example.com example.com   15 Oct 21 10:01 .
drwxr-xr-x  6 example.com example.com    6 Oct 21 09:13 ..
-rw-r--r--  1 example.com example.com  137 Oct 21 10:01 .htaccess
drwxr-xr-x  2 example.com example.com    4 Jun  8 17:24 errors
-rwxr-xr-x  1 example.com example.com  379 Jan 28  2010 hello.pl
-rw-r--r--  1 example.com example.com   45 Oct 30  2009 home.html
-rw-r--r--  1 example.com example.com   83 Oct 21 09:47 index.php
-rw-r--r--  1 example.com example.com   68 Jul 20 15:53 phpinfo.php
drwxr-x--- 10 domainuser psaserv 4.0K Oct 20 19:51 .
drwxr-xr-x 14 root       root    4.0K Aug 17 22:16 ..
-rw-r--r--  1 domainuser psacln    58 May 31 22:49 .htaccess
-rw-r--r--  1 domainuser psacln  2.1K Apr 20  2010 about.php
-rw-r--r--  1 domainuser psacln   907 Feb 25  2010 archive.php
drwxr-xr-x  2 domainuser psacln  4.0K Jul  2 16:43 image
-rw-r--r--  1 domainuser psacln  1.9K Mar  5  2010 index.php

Files

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 example.com example.com   83 Oct 21 09:47 index.php
-rw-r--r--  1 domainuser psacln  1.9K Mar  5  2010 index.php
  • -rw-r--r-- - These are the permissions for this file. r stands for "read," w stands for "write," and 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.
  • example.comdomainuser - The owner of this file.
  • example.compsacln - The group this file belongs to. May also be www-data.
  • 831.9K - The size of this file.
  • Oct 21Mar 5 2010 - The date this file was last modified.
  • index.php - The name of this file.

Change permissions

Change permissions, owner, and group

This section shows basic commands for changing the access 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.

To change permissions:

chmod 755 index.php

chmod is the command. 755 is a code which tells what kinds of read, write, and execute permissions you want for the file. index.php is an example - you should replace with your file name.

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

First number is for the owner, second for the group, and third for everyone.

To change owner:

chown domainuser index.php

chown is the command, short for "change owner." domainuser is an example of a possible owner name - you should substitute this with your desired owner name. Note that the owner must exist on the server. index.php is an example - you should replace with your file name.

To change group:

chgrp psacln index.php

chgrp is the command, short for "change group." psacln is an example of a common Plesk group - you should substitute this with your desired group name. Note that the group must exist on the server.

Change owner and group at once:

chown domainuser:psacln index.php

This command is exactly like the previous chown example, except that the group is also specified, after a colon (:).

Copy file

Use this command to copy a file to a different location (first example), or to the same folder but with a new name (second example):

cp logo.png image/logo.png
cp index.php index.php_old

cp is the command. The first file listed is the one you want to copy. The second file is the new name for the copied version of the file, including any path information for where the copy should be located. You can use a relative path like in the example above (that is, there is an image folder inside your current folder already), or you can use a full server path starting with /.

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

cp -R image/ image2

Move or rename file

The format for the move command is very similar to that for the copy command. Here's an example:

mv logo.png image/logo.png

mv is the basic command. This moves logo.png into the image/ subdirectory.

You can also use it to rename a file:

mv index.php index.php_old

This renames index.php to index.php_old.

Finally, you can move a folder just as easily as a single file. This example shows how to move the folder image/ up one level (.. means "up one level"):

mv image/ ..

Move or rename file

The format for the move command is very similar to that for the copy command. Here's an example:

mv logo.png image/logo.png

mv is the basic command. This moves logo.png into the image/ subdirectory.

You can also use it to rename a file:

mv index.php index.php_old

This renames index.php to index.php_old.

Finally, you can move a folder just as easily as a single file. This example shows how to move the folder image/ up one level (.. means "up one level"):

mv image/ ..

Copy file

Use this command to copy a file to a different location (first example), or to the same folder but with a new name (second example):

cp logo.png image/logo.png
cp index.php index.php_old

cp is the command. The first file listed is the one you want to copy. The second file is the new name for the copied version of the file, including any path information for where the copy should be located. You can use a relative path like in the example above (that is, there is an image folder inside your current folder already), or you can use a full server path starting with /.

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

cp -R image/ image2

Move or rename file

The format for the move command is very similar to that for the copy command. Here's an example:

mv logo.png image/logo.png

mv is the basic command. This moves logo.png into the image/ subdirectory.

You can also use it to rename a file:

mv index.php index.php_old

This renames index.php to index.php_old.

Finally, you can move a folder just as easily as a single file. This example shows how to move the folder image/ up one level (.. means "up one level"):

mv image/ ..

Rsync

The rsync command can be used instead of the cp command, and works on all platforms of linux. Rsync can be used in the same way as cp with added benefits such as file permission and ownership preservation, compression during transfer, and comparison between files for updates to back ups.

rsync -a logo.png images/logo.png
rsync -a index.php index.php_old

rsync is the command, followed by the '-a' flag which lets the system know to preserve the permissions, ownership, timestamp, and if rsyncing a directory, to do it recursively. The next part, 'logo.png' is the source file followed by the destination for that file to be copied to.

Another use for rsync is the ability to copy files and folders over a network to another server. This can be very helpful if you want to run some back ups, or migrate from one server to another within the (mt) Media Temple network. This first example will show you how to copy the entire document root for example.com to another server using it's SSH login credentials.

rsync -avz example.com user@host:/path/to/destination

When you hit enter, you will be prompted to enter in the password for user on the remote server. This will move the entire directory, example.com and it's contents over to the destination on the remote server. The options, '-avz' let rsync know that you want to archive the file permissions, ownership, get a verbose readout of what file is being processed, and you would like to compress the files in order to use less bandwidth.

You can also reverse this to download a copy of files from a remote server to your local machine.

rsync -avz user@host:/path/to/source ./

Once again, you will be prompted for the password for the user account on the remote server. This will copy the specified file from the remote server to the current working directory on your local machine.

Create or edit a file

  • Create or edit a file:
vi file.html

If this is a new file, it'll be empty when you open it, and you can start adding content. If this is an existing file, you'll see its contents, which you can now edit.

vi tip: 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.

  • Create an empty file (which you can later open for editing):
touch new_file.html

If you use an existing file name, it will instead "touch" that file and update its last-modified date.

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.

Read or search within a file

If you need to look through a file, the quickest way to get all the contents on your screen is cat:

cat index.html

<html>
<head>
<title>Home</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>

However, this can be overwhelming if you have a large file. In that case, you can use less or | more to conveniently scroll through the content. less uses the arrow keys, and more uses Enter. Type q to exit either of them.

less access_log
cat access_log | more
  • Search files for a specific phrase:
cat error_log | egrep "permission"

This will list only the lines containing the word "permission." cat shows the contents. Next, type the name of the file. | means the output should be filtered through the next command. egrep does the search. Your search term should go in quote marks, just in case it has special characters.

Create or edit a file

  • Create or edit a file:
vi file.html

If this is a new file, it'll be empty when you open it, and you can start adding content. If this is an existing file, you'll see its contents, which you can now edit.

vi tip: 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.

  • Create an empty file (which you can later open for editing):
touch new_file.html

If you use an existing file name, it will instead "touch" that file and update its last-modified date.

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.

Read or search within a file

If you need to look through a file, the quickest way to get all the contents on your screen is cat:

cat index.html

<html>
<head>
<title>Home</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>

However, this can be overwhelming if you have a large file. In that case, you can use less or | more to conveniently scroll through the content. less uses the arrow keys, and more uses Enter. Type q to exit either of them.

less access_log
cat access_log | more
  • Search files for a specific phrase:
cat error_log | egrep "permission"

This will list only the lines containing the word "permission." cat shows the contents. Next, type the name of the file. | means the output should be filtered through the next command. egrep does the search. Your search term should go in quote marks, just in case it has special characters.

Delete file

You can also delete a file using SSH. Be sure you don't need it any more before you delete it.

rm index.php_old

You will receive output like the following:

rm: remove regular file `index.php_old'? 

Type y confirm, or n to cancel. If you don't want to be prompted, add -f to the command:

rm -rf index.php_old

Also, if you want to remove an entire directory and all its contents, including subdirectories, add -r. It can become tedious quickly to approve every deletion in a directory, so -f is commonly used.

Please be cautious with the rm -rf command. It will irreversibly delete a folder and all of the files and subfolders in it.

To make sure you're deleting the right thing, you can always run a list command first. For example:

ls -alh /path/to/unwanted/folder/

Here's the recursive deletion command:

rm -rf /path/to/unwanted/folder/

Delete file

You can also delete a file using SSH. Be sure you don't need it any more before you delete it.

rm index.php_old

You will receive output like the following:

rm: remove regular file `index.php_old'? 

Type y confirm, or n to cancel. If you don't want to be prompted, add -f to the command:

rm -rf index.php_old

Also, if you want to remove an entire directory and all its contents, including subdirectories, add -r. It can become tedious quickly to approve every deletion in a directory, so -f is commonly used.

Please be cautious with the rm -rf command. It will irreversibly delete a folder and all of the files and subfolders in it.

To make sure you're deleting the right thing, you can always run a list command first. For example:

ls -alh /path/to/unwanted/folder/

Here's the recursive deletion command:

rm -rf /path/to/unwanted/folder/

Disk use

  • Total server disk use:
df -h

You will see output like this, with an amount used and a percent used:

Filesystem            Size  Used Avail Use% Mounted on
/dev/vzfs              20G  3.1G   17G  17% /
simfs                  20G  3.1G   17G  17% /tmp
simfs                  20G  3.1G   17G  17% /var/tmp
  • Show all folder sizes for the current directory recursively, with their sizes:
du -h

If you run this from a high-level directory, it can take a while to complete.

  • Show a disk use summary for the current directory:
du -sh

Again, this can take a little while if you're running it in a high-level directory.

  • Here's an advanced find command you can run to find files over 10 MB (no variables in this one, just copy and paste):
(Just replace 00000 with your own site number, the rest works as-is):
find /home/00000/ -noleaf -type f -size +10000k 2>/dev/null -exec ls -lh {} \; | awk '{ print $5 ": " $9 }' |sort -n
find / -mount -noleaf -type f -size +10000k -print0 | xargs -0 ls -lhSr | awk '{printf "%*s %s\n",7,$5":",$9}'

DV server administration

Plesk admin password

  • Show Plesk admin password:
cat /etc/psa/.psa.shadow

MySQL access

Run this command to log into MySQL:

mysql -h internal-db.s00000.gridserver.com -u username

The DV server has a simple shortcut for logging into MySQL:

my

Type quit to exit.

If you want to export or import a database, see Export and import MySQL databases.

Processes and system services

  • Show current server processes:
ps -auxf
  • Show processes and memory use live:
top
  • Start/Stop Services:
/etc/init.d/httpd restart

This example restarts Apache. You can also use start and stop. See Restart Plesk services for a list of services.

  • Check Bean Counters for system resource limits (hard and soft limits, failcounts, etc.):
cat /proc/user_beancounters

For an explanation of the results of several of these commands, and for more advanced resource tracking and troubleshooting, consider reading Troubleshooting DV Memory Issues.

Log Files

Log files can tell you a lot about what's happening on your DV server. Log files are generally very long, so you should use one of these commands to sort through them easily:

  • Show the most recent 100 lines of the file:
tail -n 100 /home/00000/logs/error_log
tail -n 100 /var/www/vhosts/example.com/statistics/logs/error_log
  • Watch this file live. Interact with your server (by trying to reproduce the error, or navigating your website) while this is running and watch the log file update. Use CTRL-C to exit.
tail -f /home/00000/logs/error_log
tail -f /var/www/vhosts/example.com/statistics/logs/access_log

Log locations

  • Apache error log for your domain:

/home/00000/logs/error_log

/var/www/vhosts/example.com/statistics/logs/error_log

  • Statistic log for your domain:

/home/00000/logs/access_log-YYYY-MM-DD-##.processed

/var/www/vhosts/example.com/statistics/logs/access_log

  • Server error log:

/var/log/messages

  • Apache error log:

/var/log/httpd/error_log

  • MySQL logs:

/var/log/mysqld.log

  • Mail logs:

/usr/local/psa/var/log/maillog

  • SSH history - just type:
history
  • You will need to enable your error log before it is available.
  • To gain access to individual user directories and email accounts, you will need to enable SSH for that email user. See Add email user for instructions on accessing your email users. The login for these users is of the following form:
    ssh username%example.com@example.com
  • It's normal to have hundreds of failed SSH and FTP connection attempts; it's a reminder that we all need to use strong passwords.
  • Set up log rotation to keep logs from growing too large.
  • The mail logs are great for seeing spam activity and for tracking message delivery/failure. See Troubleshooting common issues with email for further assistance.

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.
 

Continue