by Megan McDermott, 29 October 2009 - 12:50am
For a long time, I avoided using ssh. As a designer, I thought it would be too complicated and difficult to learn. As it turns out, I was wrong! For the most part, ssh (and the shell commands needed to perform tasks) isn't that difficult to do. Being able to use SSH will make it easier and faster for you to perform many common tasks on your web server. Even if you're on a shared hosting account, you may be able to access your website using SSH (check with your hosting provider to find out).
What is SSH?
SSH, or Secure Shell, is a method of accessing servers securely on linux or other unix-based systems. Normally, ssh is performed in a terminal using the bash shell (or command line). Using the command line tools available on the server is where most of the convenience lies.
In Linux or Mac you can use ssh through the terminal. To use ssh on Windows you need to use a program called PuTTY.
Many other utilities discussed in this article are actually part of the GNU coreutils collection. This collection is commonly included with Linux, but also work on Windows, OS X and more. It possible to have a web server with SSH, but not using GNU programs. If some of the commands below don't work on your server, contact your server administrator to find out which utilities your server is running.
Why use SSH?
Using ssh will allow you to perform more advanced tasks, faster. It does require you to learn some text commands that may seem cryptic at first. The most common of these are easy to learn and remember.
Always be careful when running shell commands. Make sure you understand what your command is doing and double check to make sure your syntax is correct. If you are copying commands provided on a website or forum, it is important that you understand what the command is doing. Incorrect or malicious commands could erase your entire website or otherwise compromise your server.
More information on malicious commands.
Getting started: logging in
Using a terminal on Linux or Mac
- open a terminal
ssh email@example.com (probably the same hostname, username & password you use to ftp)
- enter your password at the prompt
Using PuTTY in Windows
- start up PuTTY
- in the Host Name field, enter your host name (probably yoursite.com or whatever you use to FTP). Ensure that SSH is checked under Connection type. Click Open to start your session.
- Say yes to the prompt asking you if you want to add this key to PuTTY's cache
- Enter your username and password at the prompt (probably the same hostname, username & password you use to ftp)
- use the
up arrow to cycle through recently used commands
man [command] to view the manual for that command (e.g.
man ls). Press
q to leave the manual.
ctrl+C to escape a command
Two commands are needed to view and navigate folder structures on your site. The
ls (or list) command shows you the files and folders in a directory.
cd (or change directory) command lets you move beteween folders. To find out where you are, use
pwd (short for print working directory). This will show you the current directory path.
Once you become familiar with your web server's structure, you will get very fast at navigating. This is often more convenient than using a GUI-based FTP client or file manager that requires you to click to open directories.
When you first log in to your server you may be directed straight to your website folders or you may end up in a folder for your user account. It might take some navigating around to figure out how to get to your website files. Often going to /var/www or /www will get you where you need to be.
- list files in a directory
- long format list - shows permissions, file owners (individual and group), file sizes, and last modified dates
- shows all files (including hidden files, such as .htaccess)
- shows long format and all files at once
- navigate to the directory folder
- navigate to the directory folder2 which is located inside folder1
- move up one level
- move up two levels
- go up two levels and navigate to the directory folder
- go back to the site root
- show current path
When enteirng a file path, press tab to auto-complete.
ls -l command
Several commands can be used to look at the contents of files:
- prints the file type and some additional information about the file (varies depending on the file type)
- prints the contents of the file
- prints the contents of the file on one screen (useful for longer files); press enter to scroll through the file and q to return to the prompt.
- an enhanced version of
more with additional shortcuts. Use the up and down arrows to scroll.
- prints the first 10 lines (by default) of the file
- prints the last 10 lines (by default) of the file
- prints the entire contents of the file style.css
cat -n style.css
- prints the contents of style.css with line numbers
head -20 style.css
- prints the first 20 lines of style.css
Moving, copying, and deleting
Three more commands:
These three commands are structured the same way and take the same options.
- recursive (includes all sub-files and folders)
- force (supresses any warnings or prompts)
- lists files as they are being processed
* is a wildcard. When used alone it represents all files in the directory. When included with other text it represents any number of additional characters.
mv file.html ../
- moves file.html up one level
mv file.html folder/file.html
- moves file.html into folder
mv file.html file2.html
- moves file.html to file2.html (effectively renames the file)
mv * folder/
- moves all files in the current directory into folder
cp file1.html file2.html
- copies file1.html to file2.html
cp * ../
- copies folder contents up one level
cp -r * ../
- copies folder contents up one level recursively (includes all sub-files and folders)
rm -rf *
- removes all files and folders, including sub-folders, and suppress all prompts
Creating new directories & files
These are pretty basic commands:
- creates a new file, or updates timestamps on existing file
- creates a new directory
- creates a new file called file.html
- creates a new folder called folder
Upgrading scripts using
If you're running a CMS such as Wordpress or Drupal, you will probably need to upgrade the system and modules or plugins quite frequently. Instead of downloading the upgrade to your hard drive, unzipping, and uploading again, you can quickly complete the process directly on the server.
- Navigate to the directory where your CMS or plugin/module resides. Depending on your hosting setup the command will look something like this:
cd httpdocs/wp-content/plugins/ (for wordpress plugins)
cd httpdocs/sites/all/modules (for drupal modules)
- Get the URL for the plugin or module you want to download (you should be able to right-click on the download link and copy the link address).
- Download the plugin to your server using the
If you're ugrading more than one plugin or module at once, you can separate them with spaces, like this
- Wait for the plugin to download to your server
- Extract the file using
tar (depending on the file type):
tar -xvzf theplugin.tar.gz
Depending on where the files extract to, you may need to move them around to overwrite the existing versions.
To discuss, ask questions or comment on this article please see the Webmaster Forums discussion on SSH: It's really not that scary.
Stay tuned for the next article in this series on managing file permissions using CHMOD. Further articles will cover more advanced command line utilities.