Ok so I have been meaning to talk about SSH (Secure Shell) & Linux Commands for a while now, but just haven’t had the time to put together a worthy resource. So today I finally sat down at gathered my thoughts on this subject for you guys! SSH is one of those things that is hard to explain to people who have never heard of it before, it is one of those things that people either LOVE IT or they just have no idea WTF it is lol. There is a steep learning curve to the Linux command line and SSH – it scares a lot of people off at the sight of it – because it looks like something you see in a hacker movie and you have already mentally written it off as “Un-Learnable” without even giving it a try. Why? Well because its something for super science fiction nerds that build robots and satellites and puts land rovers on mars or the moon, or super 1337 hackers (h4x0rz!?) in the movies (like THE NAPSTER in The Italian Job!) breaking into government systems. Right? Well no. Anyone can learn SSH, anyone can use SSH, you just have to take the time to learn the technology (and if you are a geek like me it consumes your life and you learn the technology underneath it, and on top of it, and wont stop until you understand the assembly languages that tie all this computer junk together, but thats another topic lol). Before we get too far in (some of you have probably mentally checked out alright, QUIT IT lol) I want to give the newbies a quick warning, BRACE YOURSELVES. lots of nerdy looking lingo and command examples ARE COMING. But I swear if you actually read through it, it makes sense and you will be smarter for it YOU CAN DO IT. lol. So real quick lets talk about what SSH is a little more in depth.
What Is SSH?
SSH r0x0rs j00r b0x0rzzz!! (man I haven’t typed like that since I used to play counter strike) Ok, wikipedia always helps information go down smooth, so here is what the wiki sums SSH up as….
Secure Shell (SSH) is a network protocol for secure data communication, remote shell services or command execution and other secure network services between two networked computers that it connects via a secure channel over an insecure network: a server and a client (running SSH server and SSH client programs, respectively). The protocol specification distinguishes two major versions that are referred to as SSH-1 and SSH-2.
The best-known application of the protocol is for access to shell accounts on Unix-like operating systems. It was designed as a replacement for Telnet and other insecure remote shell protocols such as the Berkeley rsh and rexec protocols, which send information, notably passwords, in plaintext, rendering them susceptible to interception and disclosure using packet analysis. The encryption used by SSH is intended to provide confidentiality and integrity of data over an unsecured network, such as the Internet.
SSH uses public-key cryptography to authenticate the remote computer and allow it to authenticate the user, if necessary. Anyone can produce a matching pair of different keys (public and private). The public key is placed on all computers that must allow access to the owner of the matching private key (the owner keeps the private key secret). While authentication is based on the private key, the key itself is never transferred through the network during authentication.
SSH only verifies if the same person offering the public key also owns the matching private key. Hence, in all versions of SSH it is important to verify unknown public keys, i.e. associate the public keys with identities, before accepting them as valid. Accepting an attacker’s public key without validation will authorize an unauthorized attacker as a valid user.
Ok yeah that was a nice dry summary chock full o’ sleep-inducing nerdyness – let me try to get you back by telling you WHY It is important to know about, and use SSH as a web designer & developer. I had a hard time grasping exactly WHY people were talking about and using SSH for quite some time – but I finally know the pleasure of using SSH. Here is why it is important.
Why Is SSH Important?
These are the 3 reasons you should attempt to use it regularly
- ITS SO FAST. So much faster than using a GUI or FTP for file manipulations or transferring files between servers etc. Especially when you have a slow internet (which I sometimes do)
- SSH connections are encrypted. In this world of daily increasing paranoia that you are going to get hacked, this is a plus lol. But with great power, comes great responsibility. (thanks Uncle Ben). Also it is important to understand that Nothing is 100% secure. Ever.
- So Lightweight and Efficient. You can take it anywhere you go. It is a LOT easier (once you are comfortable with it, that is) to open up terminal on a mac (or putty or whatever) and SSH to a server and make a few text changes, rather than to open up some bulky FTP program and pull down and open up files and edit them manually and re-upload them. It doesn’t seem like much, but it really is so much easier over SSH. (sometimes, not all the time, but having that option gives you FREEDOM!!!)
Ok, so while I am no SSH or Command Line Expert (not sure there are too many out there) I am familiar enough with SSH to love it to pieces, and thank it greatly anytime I have to move a site from one server to another. I will do my best to make sense of SSH for you and point you in the direction of some AWESOME resources to help you on your command line ninja journey.
Here we go!
Getting Started with SSH – Setup
Getting started is the easiest part, for most – If you have a web hosting account somewhere like Hostgator or Media Temple (big or small, shared is typically ok) then you probably already have access to SSH (some shared hosts try to hide it in a corner, but its usually there, if not, switch hosts!!)
Now connecting via SSH to your server from your computer is easy as well if you have a linux based operating system, or a modern Macintosh (apple) computer. It is a tad trickier if you are running windows – Windows users can get it by running the base install of Cygwin with OpenSSH installed, or simply downloading: Putty. Most of the steps in this article assume Unix (Linux, Mac OS/X) or Cygwin syntax, however.
If you are using Windows – just downloading putty is the easiest way to get setup to connect via SSH so I would just do that. If you are curious about the other SSH Clients available then read up on that. Here are a few resources that are helpful when getting started with SSH .
Before we dig into the resources I have amassed on my journeys, I would like to walk you through some quick and easy code examples of SSH while moving a site to another server – this will help show you (for the visual learners out there) just how quick, easy, and useful SSH is with a real life example. From there you can decide if you wish to dive into the rabbit hole known as SSH with the resources.
Moving a site to another server with SSH
I actually had to do this a few times lately, so its fresh in my mind and I thought this would make a good example that you can easily follow along, and I can explain some of the quirks in SSH (that took me a while to figure out). It is really hard to just look at command line code and be like “oh yeah that makes sense” with no explanation lol. I have moved a lot of sites in my day (back in the day, a lot of times, it took forever) so I can say without a doubt, this is the quickest, and easiest way to move a site.
Ok here we go. We are going to move our site to another server. So what we will be doing specifically is connecting to our old server via SSH, zipping up (or tarring up) all the files we need, moving the files to the new server (via SSH of course), logging into our new server via SSH, unzipping (or un-tarring) our files, (if it was a wordpress site, we could also export/import the database SQL via mysql commands) then we are done!
If you are on Mac, open up your terminal! It will look something like this by default, with your computer name and user name. If you have never used it before, that can be confusing, just ignore it, you type your commands after all of that, it enters that stuff by default (so we will not mention it again here, since its redundant, you just ignore that stuff, most websites put in the $ before command examples, such as “$ command” you don’t actually have to type the dollar sign in. Hopefully that clears stuff up.)
Ok, lets try to connect to our old server via SSH (keep in mind im using generic domain names like ‘[email protected]’ and ‘[email protected]’ you replace that stuff with your stuff. Type the following into your command line terminal.
Hit Enter to initiate the command – you’ll be prompted for your user password on the server. (usually the same user/pass as your cpanel, or hosting panel, or you sometimes setup SSH login separately (like with MediaTemple)). You will see the following.
[email protected]'s password:
You then enter the password – CAREFUL, it will not show you the characters you entered, just type it in, and hit enter. If you mess it up, you can always hit control + c (to cancel start over) or just hit backspace a bunch of times and enter it again. If the login worked, This will drop you into the top directory. If you type “ls” it will give you a list of all of the directories or files where you currently are. So Type that in like this and hit Enter.
You will see a list of files from the current directory – you use this to navigate the file system of the server to find your site files, you typically have to move a few directories to find them. To move through the directory you just use the “cd” command – which is Change Directory. You type in the command followed by the folder or direction such as “cd foldername” or “cd ../” to go back a directory. So lets go back to the root, which is typically where you start, or sometimes you have to go up a directory, so lets go up a directory to make sure. Type this and hit enter
now you should be at the root, from there, the typical path to the files are something like this “cd usr/www/vhosts/olddomain.com/httpdocs/” or something like that, but its nice to go through each folder at the beginning, so you would go “cd usr” hit enter – then do “ls” and hit enter to see the current files, then do “cd www” hit enter – then do “ls” hit enter to see where you are in the file structure. that will get you used to the command line.
Ok so lets say we are in the root folder of our site, and we want to zip up all the site files. If we do this:
We see this
Maildir logs tmp dev images css olddomain.com someotherdomain.com testdomain.com cgi-bin
Lets say we just want to zip up olddomain.com and move it to the new server, we don’t care about the other stuff. (we are going to use tar, because its just the usual way people zip things up over the command line) We will want to do this:
tar -pczf olddomain.tar.gz olddomain.com/
look at that and let it soak in, I know it wont make any sense if you have never seen it before lol, but here is the gist. Basically the command is “tar -pczf” then “olddomain.tar.gz” is the filename we specified for the file that the server will create – “olddomain.com/” is the folder in our current working directory that we have specified to zip up (tar up). So it will take a few seconds, depending on how large the files are in the folder, then if you type in “ls” and enter – you will see your new .tar.gz file created and sitting there in the folder for you! like this is what you SHOULD see:
Maildir logs tmp dev images css olddomain.com olddomain.tar.gz someotherdomain.com testdomain.com cgi-bin
See it there? YOU did that! er, we did that. or whatever. So now we want to move the file to the new server. I cannot tell you how much time this saves, you have to do it for yourself. If you are used to pulling down site files via FTP, then putting them up via the new FTP. you will be amazed that this does all of that for you within literally seconds. It takes around 1 minute to move like 1 GB of data. So here we will type something like the following command (your username, domain, and file path will be different but probably similar to that path – depending on the server structure) and then hit enter to initiate the move
scp olddomain.tar.gz [email protected]:../var/www/vhosts/newdomain.com/httpdocs/
This will then ask you for a password again, this time though for the username associated with the new server. So enter in the new server username password, just like you did before above, and hit enter. That will connect you to the new server, and move the files over! PRESTO! LIKE MAGIC! LIKE BUTTER! lol. Analyzing the commands, we are using the SCP command (secure copy) which basically copies the file to the new server. “scp” is the command – “olddomain.tar.gz” is the file we want to move, that is in the current working directory we are in (the one that we “cd”‘ed to) and then “[email protected]” is the login to the new server, just as if we were going to do “ssh [email protected]” to login – that points our command to the new server. then “:/var/www/vhosts/newdomain.com/httpdocs/” is just the path on that new server where we want to copy this file. Makes sense? if it doesn’t, dont worry about it. just come back to it after trying it a few times, it will make sense.
Ok now our olddomain.tar.gz is on our new server – now all we have to do is ssh into the new server, and find it, and unzip it and we are DONE! To do this, since I use terminal on Mac, I typically just hit CMD + N to open up a new terminal session (so I can be logged in at both places and do commands in both back and forth working on new and old server). So go ahead and hit CMD + N and open a new terminal window – then type this in
It will ask you for the password just like you’ve done before, enter it and press Enter. Then you will be in the top level of the new servers file system, just like before, so go ahead and do a “cd ../” (hit enter) and an “ls” (hit enter) to see where you are in the new servers file system, navigate to your root web files (typically “cd /var/www/vhosts/newdomain.com/httpdocs/” or public_html, whatever your server structure is) and do another “ls” (hit enter) and you will see a list of files there and you should see the new tar.gz file, like this:
Maildir logs tmp dev images css olddomain.tar.gz cgi-bin
Woooo its there! (obviously your files look different, just an example – I HOPE yours is there as well haha) Ok so now that the olddomain.tar.gz file is there, we need to unzip (or untar it) with this command:
tar xvfz olddomain.tar.gz
Now our files are un-tarred! Basically what this does is “tar xvfz” is the command – and “olddomain.tar.gz” is the file that we are untarring – it then dumps the contents of the tar file into the current working directory, which in this case is exactly what we want to happen. Do a quick “ls” (hit enter) and you will see the old site files, are on the new server! TADA!
It is worth mentioning, that when doing this, it often changes the files to be “owned” by the server root (if you are using root) if not, it will be owned by your username – if you need to change the username (or group) ownership it is good to run this command:
chown -R username * chgrp -R group *
That will recursively go through all of the files in the current directory you are in, and change the username ownership to whatever you put as “username” (change that to be your actual username you need) and the group to whatever you put in “group” (change that to be your actual group that you want to use)(group is usually the same as the username). That will take care of any ownership/permission issues that arise. Also if you need to recursively (that means it affects all the current directory files, rather than just one) change the chmod permissions, you can easily do that for all the files/folders with this command:
chmod -R xxx *
That will change all the files/folders in the current folder to whatever you put in “xxx” that should be a permission number – such as “666″ or “777″ or “755″ etc. If you have no idea what that means just ignore that command you should be careful with that stuff. You don’t want to have everything “777″ because that is a security no no.
Now if you want you can delete the tar.gz file as we don’t need it anymore with this command:
“rm” is basically the Remove command, it will delete whatever you tell it to delete, so be careful! (you should always be careful on the command line, its like playing Diablo in Hardcore mode, one wrong move and its over! But you’ll be fine). And you’re done! That is it for this little example, you can dive into the resources below now! Remember, It looks like a lot of steps, which you may think is a lot of work, but it’s really very easy to do once you get the hang of it and it’ll save you a ton of time. A TON OF TIME. I SWEAR. no more waiting for files to download or upload anymore, use the high speed internet connecting servers!
Other Helpful SSH Commands
Here are a few other quick code snippets of SSH worth remembering – Especially if you need to export and move a MySQL database (if you are moving wordpress for example) – Use this command to export a mySQL Database straight from SSH – SO CONVENIENT. So first, SSH into your old server, then we are going to dump the mySQL database into a .sql file, using this command:
mysqldump database -uusername -ppassword > database.sql
Here you see “mysqldump” is the command, don’t change that, but you need to change “database” to the database’s name that you want to dump SQL for. Then change -uusername to the username (NOTE: you must keep “-u” so it looks kind of funny, it would be like “-uThisISthePassword” make sense?) same goes with -ppassword (would be “-uThisIsThePassword”) then keep “>” but change “database.sql” to “whateverYouWantToNameThisFile.sql” – and hit enter – and it will create that file with all that juicy data in it (in SECONDS!)
Awesome right? so then you just use the commands we talked about above, to scp the .sql file over to the new server – then you want to do this on the new server – this will IMPORT the newly moved .sql file into a new database on the new server (database should be empty). First, Navigate to the directory that has the .sql file in it – then Use this command to import the SQL via SSH on the new server:
mysql -uusername -ppassword database < database.sql
so again, pretty much just like the last command, but opposite - keep the "-u" and the "-p" intact, change "database" to be your database name and use the "mysql" command instead of "mysqldump" to import (mysqldump is for exporting) and then just do "<" whateverTheSQLFileIsCalled.sql and your done Imports in secondsss! so quickly! YOU ARE NOW AN SSH MASTER! (not really, but you are doing better than most!)
Ok so now you can dive into the resources below, I have listed a lot of useful tutorials and resources to do everything from get you started using SSH, all the way up to command line code snippets, and advanced tutorials on using different commands to perform advanced procedures via SSH. I hope that this will be a good resource for you to come back to when you are using SSH (it is almost impossible to remember all of this stuff really, always need a reference - of course Google is a thing). One thing to note, a lot of Linux Commands for the command line are in posts that are dated like 2002, 2003, all the way up to 2012 - as Web Designers and Developers we are taught to stay on the cutting edge of information - and things change CONSTANTLY - but that isn't really the case with archaic server systems as well as the new ones running linux - those old command line commands in the 2002 posts are just as reliable as the newer posts - not much has changed, thats kind of hard to grasp, but the command line is an oldie AND a goodie! Old Yeller will get the job done.
Enjoy the resources!
Intro to Linux Commands
This, is actually the perfect place for you to start (especially for visual learners) if you have no idea about anything command line related. This will show you some cool features of the command line, and get you up to speed on how to just use the command line, which you need to know to be able to get around to even using SSH. It gives you a nice overview of the command line tool as well as takes you through some basic commands to get you up to speed. Really great I recommend EVERYONE watch this as a great foundational knowledge builder for the command line.
Man, if ever something was too nerdy for words, I believe that this list would be it. Just so much geek. Line after line of command line commands. Only a true geek could love this. BUT This list is actually awesome - it is a really good reference for when you are stuck in the command line island, and need some code-fu to get you through. Now that you are familiar with Linux and the command line - Dive into some of these commands, break stuff, you will LEARN!!
This is just awesome if you are running a Mac and have NO IDEA WHAT YOU ARE DOING when it comes to using the Terminal - it isn't scary, it is super efficient (and sexy). Trust me.
This basically just walks you through the basics of setting up SSH - If you are on a mac that has Terminal already, I would just ignore this, unless you are curious of the underlying inner workings of SSH. (If it is too complicated looking, just skip it)
This is pretty cool, just walks you through installing SSH on your iPhone and how to connect to your phone via SSH on Windows or a Mac. Really neat stuff - good learning experience. It basically treats your iPhone like a Server that you can connect to and modify files and what not. Do SSH'ey stuff.
This Comes in handy if you want to access SSH on the go from your iPhone, iPad, etc.
Ok now that you have the tools, and can connect via SSH, lets do just that! (If you are on a Mac, you most likely had the tools already )
Another good example of how to connect to an iPhone via SSH - could come in handy for something, mostly practicing your SSH command line skills.
EVEN MORE SSH Clients for lots of different OS platforms - in case you just haven't found one that you really like (maybe try understanding SSH really well before worrying about this too much) but there are plenty of options (I personally really just like mac osx terminal - Putty on windows is my fav there also)
Once you are all settled with your favorite SSH Program - this is probably a good place to start as far as getting comfortable with playing around with it, and doing just a few basic commands (not to get overwhelmed). Get your feet wet!
More awesome Linux commands to help supplement your leap into SSH - If you couldn't tell, Linux commands and being familiar with the command line are pretty much the prerequisites to taking advantage of SSH. (and being a 1337 computer science hax0r) Make sure you are familiar with these commands in case you get stuck you can come back and use the reference to get un-stuck. (un-tarred? Bahahaha)
If you understand and grasp the basics, its pretty safe at this point to dive straight into SSH and use it This is a good getting started guide that will introduce you to some of the more advanced commands and topics as well.
Some more advanced (and useful) SSH Commands - scope out this list if you are feeling pretty confident in your command-line-fu
This is a little advanced of a topic, but its really quick and easy to do, and will save you some time if your on a mac and regularly using SSH (now that your hooked, cuz its awesome, your welcome)
Because some people running Ubuntu would like a specific tutorial on how to SSH - there ya go Linux geeks! (although if you are running linux on your desktop, you probably are already hip to the command line. jus sayin)
So that pretty much covers all of the Beginner stuff to really know about SSH and the Linux Command Line. Soooo here are the advanced resources Just dive into these links and learn all you want if this has piqued your interest. If it bored you to death, then you can go play with something less technical until you revisit this subject later !
Advanced Linux & SSH Commands & Configuration Resources
The Tar Command is great - it can get pretty complicated pretty quickly though with its options (like most linux commands) so this is a good resource to browse and bookmark.
This is where your geek meter goes over 9000 and you realize all the potential! the sky is the limit!
Great site to use to lookup linux commands, or browse around for new ssh commands that you hadn't thought of that are super useful!
This is good to know as you will eventually need to start/stop restart the SSH Service for some reason (if you are ever a system admin in IT especially)
Another advanced setup to save yourself a time! Good for a browse to learn that its possible, and know there is something there that you can reference back to at a later time when you need it!
This is where SSH and Linux in general just make you believe that servers and internet technology is black magic lol.
Really good overview of some advanced SSH Concepts, worth a gander!
You will eventually have to deal with Keys if you delve deep enough in this rabbit hole - good info to know.
If you are always wondering what Git and Github and all that stuff the fancy programmers are using in silicon valley - then you should probably brush up on this stuff and the concepts explained here.
Another good overview of Git - especially if your on a Mac running OS X.
Start your own Git server if you are a fancy pants programmer that could use this for a web app or something extremely nerdy!
More info on SSH, Git, and Linux.
Rsync is pretty cool - definitely worth reading up on
In case you want to be a grand master SSH jedi.
More SSH Jedi stuff.
NOW YOU ARE A LINUX SSH COMMAND LINE MASTER - YOUR LEGEND WILL NEVER BE FORGOTTEN.
What is your favorite use of SSH? Any good stories to share? any good resources we should know about? a better way to do things? Leave it in the comments! Thanks for reading!