Ubuntu – How to use SSH (ssh & rsync commands)

remotessh

How can I SSH with a remote server so I can browse files and edit them? I have a username and password to use.

Best Answer

SSH comes by default in all Ubuntu versions so you save in having to install it (+1 for having it already there ;) )

Except of course in the case where you want a SSH server for your Ubuntu server. In that case you would

sudo apt-get install openssh-server 

which should make your computer/server ready to be a ssh server.

To use it is fairly easy:

ssh USER@SITE for example if my username is cyrex and the site is ubuntu.com then you would do this:

ssh cyrex@ubuntu.com

Now lets say you want to copy a file called bubblegum.txt from your computer to your ubuntu site and want to leave that file at the /var/www folder in the ubuntu site. you can do this (assuming your user has enough permissions in the ubuntu site) by using SSH's Brother, SCP (Which also comes by default in Ubuntu):

scp bubblegum.txt cyrex@ubuntu.com:/var/www 

Notice the : between the USER@SITE and the folder where you want to copy it. It is the one that separates both elements.

Now lets say you are uploading some huge file with scp and then the worst happens, the world explodes. How can you keep on uploading that file to the server. Then you got SCP's big brother, rsync (Comes by default in Ubuntu). In many ways scp and rsync do the same thing but here are some big differences between them:

  • rsync can upload partial files left from rsync or scp after a disconnection or world domination.
  • rsync can show the progress much better than scp
  • rsync can optimize the upload in such a way that you can actually save seconds or minutes on the upload. It also shows at the end of the upload how much you have saved.

Anyway, in the case scp could not upload the entire file this is where rsync comes in to rescue. (Thanks to Marco Ceppi for the tip. Vote up his comment if you like cats.. and/or dogs)

Lets say you did the upload mention above from scp and it got to 60%. How can you continue in that 60% without losing your times worth for the upload. You would do this:

rsync --progress --partial bubblegum.txt cyrex@ubuntu.com:/var/www  

This tells rsync to show the progress in a nice human readable way with the --progress flag. It also tells rsync to check and continue from where the file bubblegum.txt got to with the --partial flag. You can also simplify the amount to write with the -P parameter which is the same as --progress and --partial, so the above would look like this:

rsync -P bubblegum.txt cyrex@ubuntu.com:/var/www  

You can even CTRL+C the upload and resume it by doing the command from rsync above. Very cool to have the ability to resume something anytime any amount of times.

For more info for rsync which comes by default with Ubuntu you can type the following terminal commands:

man rsync  
info rsync  
rsync --help

Now to specify a port for SSH. To specify it you can do it like this:

ssh cyrex@ubuntu.com -p PORT. For example: ssh cyrex@ubuntu.com -p 1234 to tell it to use Port 1234 for the SSH service. This has to be configured in the server first for it to work.

To configure it just open in the server the file ssh_config like this: nano /etc/ssh/ssh_config and change the line that says # port 22 to another port. Also remember to remove the comment from that line. It should look like this: port 1234 in the case for the example above.

Now just restart the ssh service in the server and you are done. To restart the service just do this:

sudo restart ssh

or

sudo /etc/init.d/ssh restart

NOTE - You can also use SSH from GUI tools like filezilla which offer the option to use ssh instead of ftp. ssh can also be accessed from within the Ubuntu menu:

enter image description here

which gives access to several options including Windows share, SSH and FTP:

enter image description here

For more information about SSH you can use one of the following commands in console in your Ubuntu box:

man ssh  
info ssh  
ssh --help
Related Question