A quick and easy guide to SSH & useful Linux commands for those who want to try but don’t know where to start!
For those of us who aren’t geeks and who don’t know the syntax for changing the owner of a file/folder in Linux but who want to make use of the power of the command line, this is for you. It’s also for me so I have a reference point for all those commands I use and then forget…
This is not meant to be a complete guide on SSH clients, the setup and use of them or a comprehensive guide on all available switches (the things that you add to the commands to make them do things in a certain way) and I advise caution when messing with your server via SSH, especially when logged in as root! You may want to try this on a non production environment if you are completely new to SSH, you have been warned!
So first off SSH (Secure SHell) is a great way to access your web server securely and perform tasks quickly, some of which are impossible without SSH and others that you just wouldn’t want to do any other way (especially once you have experienced the power of SSH for yourselves!).
You’ll need to get yourselves a SSH client if you don’t already have one, the simplest and the best is PuTTY and it’s free.
Other prerequisites are; a user account that has permission to access the server via SSH and the root username and password, ask your host provider if you’re not sure and bear in mind that shared servers will probably not let you have SSH and certainly not root access (you can mess with everyone’s sites then).
You save the IP address to your server in the PuTTY client, give it a name so you can just double click it next time and then you will be presented with the no frills black and white console.
Logging in
Start off by entering your username and password, you won’t see your password as you type, it’s just blank but it is working.
Right you’re in! lets have a look around with some simple commands:
- ls will list all files in the directory, depending on where you are, you may not see anything with this without typing:
- ls -al (the -al is a switch, ‘a’ means all files including hidden and ‘l’ means long form (basically show me a list of everything with all the details)
You may not see any directories here and if you want to get into your website(s) files you’ll have to go through the root path which on most servers begins with /var. So lets get into /var:
- cd /var (note that cd var without the / will not work, the / takes you to root and then to var)
- ls -al
Now you can see some files and directories that you may start to recognise, type ls if you want to see less detail.
A useful tip: start typing part of a file name or directory name and then hit the TAB key on your keyboard, it will fill in the rest of the name automatically, particularly useful for long directory names and file names. (eg cd my[TAB] will fill in cd mysite)
From here you will then want to go into www or home depending on your configuration, again if you don’t know the root path to your website(s) ask your host or upload a phpinfo.php file to one of your sites and browse to it to see a host of useful info.
- cd www or cd home (note no / putting /www or /home will attempt to take you to that folder in the root not the sub folder of where you are)
- ls will show you where you are
Now you will have a directory vhosts or you will be straight into your websites(s) directory(s) or somewhere else depending on your configuration.
You’re looking for the directories named after your website(s) like mysite, someonessite, etc.
- cd vhosts
- cd mysite OR (don’t type the OR)
- cd mysite
- ls
Now you should see the directories that you would expect if you logged in to ftp for the site like public_html or httpdocs.
- cd httpdocs OR (don’t type the OR)
- cd public_html
- ls
OK now you’re looking at the files in your web directory.
So that’s all well and good but what can we actually do now that’s useful?
Well one thing is you can pull down install files for applications such as Wordpress. If you have ever installed Wordpress by downloading the install zip file, unzipped it and then ftp’d it up you will know how long it takes ftp to upload ALL the files and if you loose connection part way through you have to start again and could end up with a corrupt install. This is also true for other web applications such as Magento.
However if you are not in the home directory of the SSH login account then you will get restriction errors with the next commands so the easiest way to perform the next tasks is to type:
- su -
This command switches user to the root account and can also be used to switch back to your account for eg:
- su mduma
Switching to the root user will require the root user’s password.
Be careful as you are now in as root and you could delete the whole server if you type the correct command and you will not be warned by the server, you are presumed to know what you are doing logged in as root.
Now you can start to see the benefits of SSH and the command line. We’re going to grab the compressed install file directly from Wordpress, just replace the url in the next command for any other web app you might want to be installing or any file you want to download to your server (note we are getting the .tar.gz file, that’s the one you want for a Linux server):
- wget http://wordpress.org/latest.tar.gz
(Useful tip: you cannot use ctrl+v to paste into PuTTY but if you copy the command you need and then right click into PuTTY it will paste it in)
Now extract all the contents to the current directory
- tar xvzf latest.tar.gz
Nice and fast!
Setup your database and then start the web install, easy! A couple of seconds compared with much more time downloading, extracting and ftp’ing.
Another really useful function is backing up your web directories by compressing them first and then just downloading the compressed files for a local copy or just leaving the compressed file on the server for future un packing should you need to restore back to a pre existing state, say if you were doing a lot of work directly on the directory of a site. We will use tar again (Tape ARchive, from the days of tape backups).
- tar cvzf mytar.tgz mydirectory
Now you may want to move this file somewhere else or move all the contents from one directory to another:
- mv -f mytar.tgz ../
The ../ moves the file to the directory above where you currently are, useful.
I also find the tar’ing really useful when downloading a web application install directory to run on your local wamp setup, rather than downloading all the thousands of files, which can take hours, again you just tar up the whole directory and download one tar file, great!
Ok so we are starting to do some work on the server now via SSH and quite a bit using the root account. You need to be aware that the owner of the files you are moving and extracting will be the root account and not the actual account owner or the ftp account owner. This will cause you some issues when trying to move, update and delete files back in your ftp client because you will not have permission to perform those tasks.
Not to worry another useful command we can use will change the owner of the directory and/or files. This can also be useful if your web application downloads and installs files to your server and you then can’t work out why you don’t have permission to do anything with those files, the owner will be a different account on the server in some cases, this happened recently on one of the Magento installs I did. Luckily I knew this handy command:
- chown -R myftpuser:myftpuser public_html
So what we are doing here is chown’ing (changing the owner) recursively (note the capital R) (recursively just means all files and folders within the directory) to myftpuser and the group myftpuser of the public_html folder. You could just change the user and not the group:
- chown -R myftpuser public_html
If you aren’t sure of the user it’s a good chance it will be the ftp user name and if you’re still not sure then you should be able to see the owner in your ftp client.
Breathe….
Another useful command is similar to the above and its used to change permissions for files and folders, its much faster than doing it in your ftp client if you are recursively changing lots of files and folders:
- chmod -R 0777 public_html
Now you should know about file permissions and if you do you will probably know that you shouldn’t enable the above permissions recursively (or not) on the web root directory! But you get the idea…
I find this particularly useful on a Magento installation when you want to use the Magento Connect facility that requires write permission on you Magento install directory to work. You can then go back in and re set the permissions in another quick command like:
- chmod -R 0755 public_html
Or even more restricted:
- chmod -R 0644 public_html
I’m going to leave you with a really handy tool that should be installed on your server, if not go here and look for how to install the rpm binary for your server. It’s Midnight Commander!
- mc
Yes, that’s it! Two little letters and so much joy
For those of us who prefer a little GUI with our SSH this is the perfect tool for browsing around your server, it has a text editor built in and it can do pretty much all of the simple commands above. I don’t know about you but I can’t be doing with learning, and remembering, commands for saving, editing and quitting out of a text editor, give me a bit of point and click every time!


