SSH Commands

Have you backed up recently? Please do so before using any of these commands... just in case. Also - If this is has help you, please help us help you:

Changing and Exploring Directories

To look at the current directory, there are 3 commands you can type. Depending on the server configuration and Operating System not all will work.

ls
ll
dir
ls -al

To change directory:

cd pathname/directory/subdirectory

Or to go up a level:

cd ../

Or you can chain these commands together:

cd ../../themes/images

You can go to the absolute root allowed by your host by typing:

cd /

Oh, oh and you can return to the previous directory you were at:

cd -

Finally the SSH terminal can predict what you’re going to type by pressing tab. So for example if you want to go to /var/www/vhosts/domainname.com/httpdocs but couldn’t remember which way round the www and vhosts were you could start by typing:

/var/w

Then press the Tab key. If there’s a file or folder starting with w (and not more that one of it) it will auto complete. So to complete the example above you would type:

/vTABwTABvTABdTABhTABdTAB

This displays as

/var/www/vhosts/domainname.com/httpdocs/

Looking at the example above you should have noticed two things. First was that tab autofilled the forward slashes(/) for at the end of each directory, thus shorting the number of characters I typed. Second was that I had to press Tab twice, first after the letter h then after the letter d. That was because within the domainname.com/ directory. There were two folders the with similar names.

# httpdocs (the one I was after)
# httpsdocs (very similar but with an s in there)

As a result the Tab key autofilled ‘http’ but didn’t know which of the two directories I was autofilling. Pressing d then Tab again confirmed that I wanted httpdocs and not httpsdocs which would have required the letter ‘s’ instead as that’s the first letter after the autofilled item.

Copying Files

To copy a file with the same directory simply type:

cp filename-to-copy.txt new-file-name.txt

For example:

cp index.html index.back.html

To copy between directories

cp filename-to copy.txt ../../new-directory/filename-to-copy.txt

For example

cp contactus.php ../contact/contactus.php

To copy all files from one directory to another, use the * character, which unofficially means all:

cp images/* ../skin/

Adding Flags to Commands

There are additional elements you can add to the command called Flags. These add extra properties to the command that might be necessary but don’t happen by default.

Flags are easily spotted as they generally appear after the command tag, in the case above ‘cp’ and always start with a hyphen ‘-’.

The most commonly used flag for copying files (cp) is the -a Flag. This will copy the file or directory across retaining the permissions whilst retaining the permissions and ownership:

cp -a contactus.php ../contact/index.php

Flags can also be daisy chained to add multiple properties to one command. A great example is the compression of a directory below using the .tar.gz method.

Moving Files

To move a file simply type:

mv current-directory/existingfile.txt ../new-directory/existingfile

For Example

mv images/header.jpg ../httpdocs/

Renaming Files

To rename a file, use the ‘cp’ command as before, but change the name of the file when stating the directory receiving the file.

mv oldfilename.txt newfilename.txt

For Example

mv index.php index.bac.php

Note: You must delete the original file from the server as essentially you’re not renaming the original file. Instead you’re creating a copy of the original file with a different name.

Compressing Files with Zip

To create a zip file, simply type:

zip -r compressed-file-name.zip directoryname

For Example

zip -r website-backup-2010-11-31.zip httpdocs/*

The hypen -r ensures that the file and directories within the parent directory being compressed are also included.

Decompressing Zip Files

To unzip a file:

unzip filename.zip

For Example:

unzip website-backup-2010-11-31.zip

Note: For simplicities sake, always place the .zip file in the directory you would like it’s contents to be unzipped to.

Compressing Files with tar.gz

To create a tar.gz file, simply type:

tar czvf archivename.tar.gz directory-or-file-to-archive/

For Example

tar czvf website-backup-2010-11-31.tar.gz httpdocs/

Note the Flags czvf, they stand for:
Compress - Creates the new archive.
Zip - Compresses the file.
File - Implies that we have given the compressed file a name.
Verbose - Prints what the command line is doing, like a progress report.

Decompressing Files with tar.gz

tar -xzf archivename.tar.gz

For Example:

tar -xzf website-backup-2010-11-31.tar.gz

Note: For simplicities sake, always place the .tar.gz file in the directory you would like it’s contents to be unzipped to.

Backing up Databases

To backup a database via ssh, type:

mysqldump -u database_username -p database_name > name_of_backup.sql

For Example:

mysqldump -u wordpress_bob -p wordpress_blog > wordpress_blog_20101031.sql

Importing Databases / Restoring Backed up Databases

To restore and import a database you first need to create the bank database then assign a user. Using these details you must replace the database name and user below:

mysql -u database_username -p database_name < name_of_backup.sql

For Example:

mysql -u wordpress_bob -p wordpress_blog < wordpress_blog_2011-03-21.sql

Note the direction of the arrow. It’s very easy to get these the wrong way. Doing so could cause big problems.

Backing up Files and Folders

To backup files, either use the compressing .tar.gz or .zip methods above. Then download the data for a local copy. I would always recommend compressing file’s using .tar.gz as opposed .zip as it results in much smaller file sizes.

Note: Always test your backups. I’ve been caught out after downloading a file that some how got corrupted whilst compressing. Always test your backups.

Changing and Setting Owner Group SSH

To change both the owner and group of a directory use the chown command. The numbers in the example below represent Owner:Group

For Example:

chown 10000:505 directoryname/

Moving Servers with SSH

The is by far on the best features of SSH, but not alot of shared hosting providers allow it. Once you’ve compressed the directories and files, normally you’ve download the large .zip or .tar.gz file then reupload it using an FTP client. Not anymore. You can actually get the new server to download the file from the old server using wget. Create your backup on server1 then make sure it’s accessible through a web browser. Login to server2 via SSH and navigate to the directory you wish to download the large back file to. Then type:

wget http://www.myoldsite.com/file_name.zip

For Example:

wget http://wordpress.org/latest.tar.gz

Changing File and Folder Permissions

To change just the owner of the file to the user ‘root’ type:

chown root filename.txt

To change the owner to root, but the group to ‘superadmin’ type:

chown root.superadmin filename.txt

To change owner and group for all files and folders within the directory aswell, recursively, use the ‘-R’ Flag:

chown -R ftpname.psacln filename.txt

You may also wish to CHMOD a file to set them as writable, read-only etc. To do so type:

chmod -777 filename.txt

Login as a super user (root) via SSH

As a security measure, sometimes the superuser ‘root’ can only be used once you’ve logged in as a standard user. To change your user, type:

- su

then type your password.

Deleting Files and Directories

I’ve intentionally left this towards the end for obvious reasons. To delete a file simply type:

rm filename-or-directory-to-delete.txt

Alternatively if you wish to delete a directory, and all directories and files within that recursively, type:

rm -r

This should ask you for confirmation, but never assume when deleting files that you will be asked to confirm the action. Always make sure you’re deleting the correct file before running the command.

SSH Restarting MySQL Server

This is more for advanced users and should never be used in a shared hosting environment. To restart the MySQL server using SSH, type:

/etc/init.d/mysqld restart
service mysql restart

Note: This is 2 separate commands and both need to be executed.

SSH Restarting Apache

This is more for advanced users and should never be used in a shared hosting environment. To restart Apache gracefully (allowing current connections to complete first) using SSH, type:

apachectl graceful

To restart Apache immediately type:

apachectl restart

Show System Processes with SSH

To see what the system processes in a human readable way, type:

ps aux --forest

Find All Files containing a Phrase

To search for a phrase inside a site, type the following replacing 'PHRASE' with your phrase:

grep -H -r "PHRASE" * | grep -v filter | cut -d":" -f1 | sort -u

Find out current Max MySQL Connections

To work out how many active MySQL sessions you can have at any one time login to MySQL

mysql -uadmin -p`cat /etc/psa/.psa.shadow`

Then give this commands

show variables like 'max_connections';

Increase Max MySQL Connections (Plesk Server)

To Increase the Max MySQL Connections type, you first need to open up the my.cnf file

vi /etc/my.cnf 

Then add the following line beneath the socket declaration

max_connections=250 

Note: This will increase the max number from 100 (generally the default) to 250.

Next type the following to save and close (remember to come out of insert mode in vi editor first)

:wq!

Finally restart MySQL

/etc/init.d/mysqld restart


Linux SSH Commands

Learn a tool that will change the way you make websites forever. SSH is one of the many things necessary to complete any web designer’s toolbox. For those that know, it’s powerful functions allow you, the web designer to perform many simple and complex tasks at a fraction of the speed popular tools like FTP clients. And if there’s anything we all need, it’s to save time.

What is SSH?

SSH otherwise know as Secure Shell is a protocol that allows data to be transferred securely between two networked devices. To put it simply, it’s a way of connecting to and using another machine either next to you, or the other side of the world securely.

Generally you would use a client such as PuTTY (free download) for PC or Terminal (built in) for Mac. Using the command line interface, you connect, navigate and control files on another machine.

Literally speaking when using Putty or Terminal your telling a computer to perform an action for you. This action could be a copy action, paste action, a open file action, a change permission action, pretty much a action you perform in explorer or finder. You tell it to perform these actions by typing in commands.

It can be very daunting looking at the command line for the first time not understanding how this system works but the easiest way to think about it is like thinking your talking to a person vert simply.

For example if you wanted to copy a file from one place you another you would say copy original folder / file-a.txt to new folder/ file-a.txt.

When typing this you would would say cp originalfolder/file-a.txt newfolder/file-a.txt once you have logged in.

It’s important when performing these actions to understand that when you perform some actions you may not receive the desired result. That’s because depending on the action, the user account performing the changes (via SSH) copying over it’s ownership and group details to the effected file. Meaning if you’re logged in via ftp, you may not be able to delete a file you’ve worked with using SSH as the user root.

Why Use SSH?

So now I’ve got you interested, why would you as a web designer use SSH? I think it’s probably easier, and clearer to show you. Click the Commons SSH Commands Tab to view the actions.

NOTE: Although SSH is now commonly available with Shared Hosting packages, you generally need to request access before your user credentials work. Please contact your hosting provider to check.

WARNING: It’s very easy to make mistakes when using SSH. Always double check what you’re doing. And always make sure you have a backup to fall back on.