Clinton Reeves / Posted 7.31.2012
SSH for deployments
Moving files around on a big website can be quite cumbersome and can present many different challenges.
To transfer the site files to the server via SFTP would be extremely time consuming. With a large database, loading the .sql file via phpMyAdmin wouldn't be possible (for more information about problems associated with loading large .sql files via phpMyAdmin, please check out my previous article titled “Uploading Large SQL Files”).
Depending on your web host, you may have access to a service called SSH. SSH, which stands for Secure Shell, is a network protocol that allows a user to securely gain access to a remote computer. With SSH, you can quickly and easily transfer large files, as well as run any command on the server as if it were loaded directly on your personal computer.
Of course, for many, SSH can be intimidating, given that it’s run in a terminal and completely text-based (you do not have a GUI). But, with a little time and practice, you’ll find that SSH is not that complex and can really help your workflow. Any time you’re running a release or transferring lots of files for a client, you’ll likely find that SSH is preferable over SFTP (if the host offers it).
Let’s check out a couple of simple commands that you can use to get you started using SSH.
1. Connecting via SSH
In order to use SSH, you'll first need to connect to your remote system. To create your SSH connection, first start by opening a terminal window. Once the terminal window is open, enter the following:
username is the systems SSH username.
Note that you may need to contact your hosting provider to obtain your SSH username and password if you do not already have it.
hostname.com is the name of your host.
Note that the hostname you use here is typically (but not always) the domain name of your site. Depending on your server’s configuration, it may also be the IP Address of the server.
Your terminal window should look something like this:
If you’re using an IP Address instead of the domain name, your terminal window may look like:
Now, in the same terminal window, press enter. The system will ask for you to enter the password for the user. Type the password and press enter. Depending on your system and its configuration, you should see a message like the following, once the SSH connection is successfully established:
2. Closing an active SSH Connection
Closing an SSH connection is very simple. In the terminal window in which that you opened the SSH Connection, simply enter the following command and press enter:
You should see a message like the following:
3. Transferring files to and from your remote server
Transferring files to and from your remote server is a breeze with SSH. To perform this action, you are actually going to use a service called “SCP” which stands for Secure Copy. SCP or “Secure Copy” allows you to copy files over an SSH connection. To use SCP, start by opening a new terminal window. You will not need to be connected via SSH to run your SCP Command.
4. Transfer files from your computer to your remote computer
To copy files on your local computer to your remote computer, simply enter the following command:
scp /path/to/local/file/file.ext firstname.lastname@example.org:/path/to/save/file/on/remote/computer/
Your terminal window should look something like the following:
The command above will allow you to copy one file on your local machine to a remote machine. If you would like to copy more than one file within a directory on your local computer you would need to use the following command:
scp -R /path/to/local/dir email@example.com:/path/to/save/file/on/remote/computer/
The “-R” flag stands for “recursive” and simply means: copy all files, folders and sub-folders in this directory.
Once the command has been entered into your terminal, press enter enter your SSH user password when prompted.
5. Transferring files from remote computer to your local computer
To copy files on your remote computer to your local computer, enter the following:
scp firstname.lastname@example.org:/path/to/remote/file/file.ext /path/to/save/local/file/file.ext
Your terminal window should then look like this:
The command above will allow you to copy one file on your remote machine to your local machine. If you would like to copy more than one file within a directory on your remote computer you would use the following command:
scp -R email@example.com:/path/to/remote/file/file.ext /path/to/save/local/file/file.ext
6. Zipping and unzipping remote files
SFTP can be quite frustrating when it comes to working with compressed files. If you have ever been in a situation where you need to either compress or extract the contents of a file via SFTP, you've probably realized that you can't. SSH can solve this problem for you.
After your SSH connection has been made, if you would like to compress a single file, you can enter the following command:
zip <name-of-zip-file> /path/to/file/to/zip/filename.ext
Where <name-of-zip-file> is the name of the zip file that will be created and '/path/to/file/to/zip/filename.ext' is the full path to the specific file you would like to compress.
You can also use the above command to compress entire directories and all of their contents. This is great for backing up your site or any files you are currently working with.
To compress full directories, you would use the following command:
zip -r <name-of-zip-file> /path/to/file/to/zip/filename.ext
The “-r” flag stands for “recursive,” meaning: compress all of the files, folders and sub-folders in this directory and include them in the zip file that's created.
There you have it. Half a dozen basic SSH commands to get you started.
Questions? Comments? As always, leave a comment and we’ll get right back to you!
Photo credit: aaronparecki
Back-end developer at Q Digital Studio
Clinton Reeves is a back-end developer at Q Digital Studio. Clinton lives and breathes code and has the B.S. in Information Systems to back it up. Though a self-proclaimed nerd who happily embraces said nerdiness, Clinton actually makes coding look cool.
Even though Clinton thinks about code all day (and sometimes even dreams in code), it wasn't always so easy for him. In college, he took a programming class and really liked it, so he switched majors. Programming did not come intuitively to Clinton and was really hard for him to learn. But it was that challenge that ultimately attracted him and kept him interested. These days he makes coding look positively effortless, but that may have something to do with the requisite eight cups of coffee he drinks daily.