SSH: It's really not that scary

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.

Warning

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

  1. open a terminal
  2. type ssh [email protected] (probably the same hostname, username & password you use to ftp)
  3. enter your password at the prompt

Using PuTTY in Windows

  1. start up PuTTY
  2. 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.
  3. Say yes to the prompt asking you if you want to add this key to PuTTY's cache
  4. Enter your username and password at the prompt (probably the same hostname, username & password you use to ftp)

Shortcuts

  • use the up arrow to cycle through recently used commands
  • use man [command] to view the manual for that command (e.g. man ls). Press q to leave the manual.
  • use ctrl+C to escape a command

Navigating around

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.

Sample commands

ls
list files in a directory
ls -l
long format list - shows permissions, file owners (individual and group), file sizes, and last modified dates
ls -a
shows all files (including hidden files, such as .htaccess)
ls -la
shows long format and all files at once
cd folder
navigate to the directory folder
cd folder1/folder2
navigate to the directory folder2 which is located inside folder1
cd ../
move up one level
cd ../../
move up two levels
cd ../../folder
go up two levels and navigate to the directory folder
cd /
go back to the site root
pwd
show current path

Shortcut

When enteirng a file path, press tab to auto-complete.

A sample ls command

sample ls command

A sample ls -l command

sample ls -l command

Reading files

Several commands can be used to look at the contents of files:

file
prints the file type and some additional information about the file (varies depending on the file type)
cat
prints the contents of the file
more
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.
less
an enhanced version of more with additional shortcuts. Use the up and down arrows to scroll.
head
prints the first 10 lines (by default) of the file
tail
prints the last 10 lines (by default) of the file

Sample commands

cat style.css
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:

mv
move
cp
copy
rm
remove

These three commands are structured the same way and take the same options.

Common options

-r
recursive (includes all sub-files and folders)
-f
force (supresses any warnings or prompts)
-v
lists files as they are being processed

Shortcut

* 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.

Sample commands

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:

touch
creates a new file, or updates timestamps on existing file
mkdir
creates a new directory

Sample commands

touch file.html
creates a new file called file.html
mkdir folder
creates a new folder called folder


 

Upgrading scripts using wget and unzip or tar

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.

  1. 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)

  2. 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).
  3. Download the plugin to your server using the wget command:  

    wget http://example.com.com/theplugin.zip

    If you're ugrading more than one plugin or module at once, you can separate them with spaces, like this

    wget http://example.com/theplugin1.zip http://example.com/theplugin2.zip

  4. Wait for the plugin to download to your server
  5. Extract the file using unzip or tar (depending on the file type):

    unzip theplugin.zip

    tar -xvzf theplugin.tar.gz

Depending on where the files extract to, you may need to move them around to overwrite the existing versions.

Discussion

To discuss, ask questions or comment on this article please see the Webmaster Forums discussion on SSH: It's really not that scary.

What's next?

Stay tuned for the next article in this series on managing file permissions using CHMOD. Further articles will cover more advanced command line utilities.

Resources

 

Megan McDermott's picture

About the Author

Megan is co-founder and editor of A Padded Cell and administrator at The Webmaster Forums. She has been designing websites since 1997, with expertise in design, information architecture, usability, HTML/CSS, Drupal theming, and more. Megan is also a partner and co-founder of Woolwich Web Works: A small team that can do big things!