Have you backed up recently? Please do so before using any of these commands... just in case. Also - If this is has help you, please help us help you:
To look at the current directory, there are 3 commands you can type. Depending on the server configuration and Operating System not all will work.
To change directory:
Or to go up a level:
Or you can chain these commands together:
You can go to the absolute root allowed by your host by typing:
Oh, oh and you can return to the previous directory you were at:
Finally the SSH terminal can predict what you’re going to type by pressing tab. So for example if you want to go to /var/www/vhosts/domainname.com/httpdocs but couldn’t remember which way round the www and vhosts were you could start by typing:
Then press the Tab key. If there’s a file or folder starting with w (and not more that one of it) it will auto complete. So to complete the example above you would type:
This displays as
Looking at the example above you should have noticed two things. First was that tab autofilled the forward slashes(/) for at the end of each directory, thus shorting the number of characters I typed. Second was that I had to press Tab twice, first after the letter h then after the letter d. That was because within the domainname.com/ directory. There were two folders the with similar names.
# httpdocs (the one I was after)
# httpsdocs (very similar but with an s in there)
As a result the Tab key autofilled ‘http’ but didn’t know which of the two directories I was autofilling. Pressing d then Tab again confirmed that I wanted httpdocs and not httpsdocs which would have required the letter ‘s’ instead as that’s the first letter after the autofilled item.
To copy a file with the same directory simply type:
cp filename-to-copy.txt new-file-name.txt
cp index.html index.back.html
To copy between directories
cp filename-to copy.txt ../../new-directory/filename-to-copy.txt
cp contactus.php ../contact/contactus.php
To copy all files from one directory to another, use the * character, which unofficially means all (except files that begin with . or ..):
cp images/* ../skin/
To copy all files including files that begin with . (1 dot) from one directory to another:
rsync -a ./ ../
There are additional elements you can add to the command called Flags. These add extra properties to the command that might be necessary but don’t happen by default.
Flags are easily spotted as they generally appear after the command tag, in the case above ‘cp’ and always start with a hyphen ‘-’.
The most commonly used flag for copying files (cp) is the -a Flag. This will copy the file or directory across retaining the permissions whilst retaining the permissions and ownership:
cp -a contactus.php ../contact/index.php
Flags can also be daisy chained to add multiple properties to one command. A great example is the compression of a directory below using the .tar.gz method.
To move a file simply type:
mv current-directory/existingfile.txt ../new-directory/existingfile
mv images/header.jpg ../httpdocs/
To rename a file, use the ‘cp’ command as before, but change the name of the file when stating the directory receiving the file.
mv oldfilename.txt newfilename.txt
mv index.php index.bac.php
Note: You must delete the original file from the server as essentially you’re not renaming the original file. Instead you’re creating a copy of the original file with a different name.
To create a zip file, simply type:
zip -r compressed-file-name.zip directoryname
zip -r website-backup-2010-11-31.zip httpdocs/*
The hypen -r ensures that the file and directories within the parent directory being compressed are also included.
To unzip a file:
Note: For simplicities sake, always place the .zip file in the directory you would like it’s contents to be unzipped to.
To create a tar.gz file, simply type:
tar czvf archivename.tar.gz directory-or-file-to-archive/
tar czvf website-backup-2010-11-31.tar.gz httpdocs/
Note the Flags czvf, they stand for:
Compress - Creates the new archive.
Zip - Compresses the file.
File - Implies that we have given the compressed file a name.
Verbose - Prints what the command line is doing, like a progress report.
tar -xzf archivename.tar.gz
tar -xzf website-backup-2010-11-31.tar.gz
Note: For simplicities sake, always place the .tar.gz file in the directory you would like it’s contents to be unzipped to.
To backup a database via ssh, type:
mysqldump -u database_username -p database_name > name_of_backup.sql
mysqldump -u wordpress_bob -p wordpress_blog > wordpress_blog_20101031.sql
To restore and import a database you first need to create the bank database then assign a user. Using these details you must replace the database name and user below:
mysql -u database_username -p database_name < name_of_backup.sql
mysql -u wordpress_bob -p wordpress_blog < wordpress_blog_2011-03-21.sql
Note the direction of the arrow. It’s very easy to get these the wrong way. Doing so could cause big problems.
To backup files, either use the compressing .tar.gz or .zip methods above. Then download the data for a local copy. I would always recommend compressing file’s using .tar.gz as opposed .zip as it results in much smaller file sizes.
Note: Always test your backups. I’ve been caught out after downloading a file that some how got corrupted whilst compressing. Always test your backups.
To change both the owner and group of a directory use the chown command. The numbers in the example below represent Owner:Group
chown 10000:505 directoryname/
The is by far on the best features of SSH, but not alot of shared hosting providers allow it. Once you’ve compressed the directories and files, normally you’ve download the large .zip or .tar.gz file then reupload it using an FTP client. Not anymore. You can actually get the new server to download the file from the old server using wget. Create your backup on server1 then make sure it’s accessible through a web browser. Login to server2 via SSH and navigate to the directory you wish to download the large back file to. Then type:
To change just the owner of the file to the user ‘root’ type:
chown root filename.txt
To change the owner to root, but the group to ‘superadmin’ type:
chown root.superadmin filename.txt
To change owner and group for all files and folders within the directory aswell, recursively, use the ‘-R’ Flag:
chown -R ftpname.psacln filename.txt
You may also wish to CHMOD a file to set them as writable, read-only etc. To do so type:
chmod -777 filename.txt
As a security measure, sometimes the superuser ‘root’ can only be used once you’ve logged in as a standard user. To change your user, type:
then type your password.
I’ve intentionally left this towards the end for obvious reasons. To delete a file simply type:
Alternatively if you wish to delete a directory, and all directories and files within that recursively, type:
This should ask you for confirmation, but never assume when deleting files that you will be asked to confirm the action. Always make sure you’re deleting the correct file before running the command.
This is more for advanced users and should never be used in a shared hosting environment. To restart the MySQL server using SSH, type:
service mysql restart
Note: This is 2 separate commands and both need to be executed.
This is more for advanced users and should never be used in a shared hosting environment. To restart Apache gracefully (allowing current connections to complete first) using SSH, type:
To restart Apache immediately type:
To see what the system processes in a human readable way, type:
ps aux --forest
To search for a phrase inside a site, type the following replacing 'PHRASE' with your phrase:
grep -H -r "PHRASE" * | grep -v filter | cut -d":" -f1 | sort -u
To work out how many active MySQL sessions you can have at any one time login to MySQL
mysql -uadmin -p`cat /etc/psa/.psa.shadow`
Then give this commands
show variables like 'max_connections';
To Increase the Max MySQL Connections type, you first need to open up the my.cnf file
Then add the following line beneath the socket declaration
Note: This will increase the max number from 100 (generally the default) to 250.
Next type the following to save and close (remember to come out of insert mode in vi editor first)
Finally restart MySQL
Linux SSH Commands
Learn a tool that will change the way you make websites forever. SSH is one of the many things necessary to complete any web designer’s toolbox. For those that know, it’s powerful functions allow you, the web designer to perform many simple and complex tasks at a fraction of the speed popular tools like FTP clients. And if there’s anything we all need, it’s to save time.
What is SSH?
SSH otherwise know as Secure Shell is a protocol that allows data to be transferred securely between two networked devices. To put it simply, it’s a way of connecting to and using another machine either next to you, or the other side of the world securely.
Generally you would use a client such as PuTTY (free download) for PC or Terminal (built in) for Mac. Using the command line interface, you connect, navigate and control files on another machine.
Literally speaking when using Putty or Terminal your telling a computer to perform an action for you. This action could be a copy action, paste action, a open file action, a change permission action, pretty much a action you perform in explorer or finder. You tell it to perform these actions by typing in commands.
It can be very daunting looking at the command line for the first time not understanding how this system works but the easiest way to think about it is like thinking your talking to a person vert simply.
For example if you wanted to copy a file from one place you another you would say copy original folder / file-a.txt to new folder/ file-a.txt.
When typing this you would would say cp originalfolder/file-a.txt newfolder/file-a.txt once you have logged in.
It’s important when performing these actions to understand that when you perform some actions you may not receive the desired result. That’s because depending on the action, the user account performing the changes (via SSH) copying over it’s ownership and group details to the effected file. Meaning if you’re logged in via ftp, you may not be able to delete a file you’ve worked with using SSH as the user root.
Why Use SSH?
So now I’ve got you interested, why would you as a web designer use SSH? I think it’s probably easier, and clearer to show you. Click the Commons SSH Commands Tab to view the actions.
NOTE: Although SSH is now commonly available with Shared Hosting packages, you generally need to request access before your user credentials work. Please contact your hosting provider to check.
WARNING: It’s very easy to make mistakes when using SSH. Always double check what you’re doing. And always make sure you have a backup to fall back on.