SSH is
a secure replacement for Telnet and rsh. All communications
between the client and server are encrypted. To access an SSH
client (usually OpenSSH) in most Unix OSs, type ssh
user@host.com in a terminal window. If you don't specify the
username, the user that entered the command ($USER)
will be used. In Windows, you will need to download a 3rd-party
utility such as PuTTY or Cygwin. Find more
information in the ssh(1) man page.
Contents |
SSH is actually so much more than just a way to access a remote shell securely. It can be used for lots of other ways to transfer information securely. It includes a neat utility "scp", which stands for secure copy, which is a great way to copy files between machines. It works almost exactly like your default unix cp command. scp also allows you to copy a file from a remote host to a remote host. An example of scp:
scp user@host.com:~/files/ . which means copy files
from user's home directory on the host.com machine from his files/
directory (it will copy ALL files from the files/ directory to the
CWD (current working directory)).
Another great use is to use it to encrypt the transport of any data from one machine to another. As an extreme example, you can use SSH to remotely move a disk from one machine to another (akin to ghost, but securely). This may not be the best use of SSH, or the fastest way to transfer data from one machine to another over a network, but it shows you how powerful SSH can be.
Another great feature is port forwarding. This allows you 'redirect' communication to and from a local application through SSH to another host. So, with SSH you can secure otherwise insecure communications over an encrypted 'tunnel'.
The secure shell client is conveniently called
ssh.
ssh user@hostSFTP has nothing to do with FTP. SFTP merely works like FTP, meaning you use it as you would FTP. Using SFTP requires only the SSH server. The FTP server is irrelevant to SFTP. Files are transferred as binary by default.
sftp user@hostscp, aka Secure Copy, works just like
rcp. Don't know what rcp is, then don't
fret.
scp FILE_PATH user@host:REMOTE_PATHscp user@host:REMOTE_PATH LOCAL_PATHNote : If your filename contains spaces then, use scp like this
$scp
user@host:"/media/sda6/Tutorials/Linux\\
Unix/linux_book.pdf" /home/narendra/datascp /home/narendra/linux_book.pdf
user@host:"/media/Tutorials/Linux\\
Unix/"Note : If you want to copy the whole directory then use
scp -r user@host:"<source_dirname>"
<destination_dirname>Although SSH can be used with passwords, doing so is not recommended, and many servers will not allow password logins. Instead, use a key - this is more secure, and more convenient.
To create an SSH key...
Most modern Unix systems include the OpenSSH client. To generate a key, run:
$ ssh-keygen -t rsa
This will store your private key in $HOME/.ssh/id_rsa, and your public key in $HOME/.ssh/id_rsa.pub. You can use different filenames, but these are the default filenames, so it's easiest to not change them.
Because the security of your private key is so important, SSH will not work if file permissions are insecure. SSH will create files and directories with the appropriate permissions, but sometimes things will go wrong. To fix permission issues:
$ chmod 600 ~/.ssh/KEY ~/.ssh/KEY.pub $ chmod 700 ~/.ssh
To log into a remote server, you'll need to put the public key on that server.
The easiest way to do that is using ssh-copy-id.
This requires some alternate form of authentication, usually
password (since you haven't got a key on the server you cannot use
key authentication yet).
$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@host.net
ssh user@host "mkdir ~/.ssh && chmod 700
~/.ssh"scp ~/.ssh/KEY.pub user@host:.ssh/ssh user@hostcd ~/.sshcat KEY.pub >> ~/.ssh/authorized_keyscat ~/.ssh/KEY.pub | ssh user@host "cat >>
~/.ssh/authorized_keys"You don't need to set up a ~/.ssh/config file, but
it makes authentication easier. The important part is to specify
your user name and your private key - if this is specified in the
config file, you needn't provide it on the command line. Using
HostName, you can shorten the ssh command to:
$ ssh servername
#Specific configuration applied to one host
#This configuration applies specifically to a host which uses Windows Domain login
Host Short_Name
HostName some_host.com
User domain\username
Protocol 2
UseRsh no
IdentityFile ~/.ssh/KEY
# Generic configuration that I apply to all hosts, especially on my private LAN
# Of note, the options to forward X11 and the SSH Agent. X11 forwarding lets you
# tunnel and X session or programs via SSH.
Host *
User USERNAME
Protocol 2
ForwardX11 yes
ForwardAgent yes
UseRsh no
IdentityFile ~/.ssh/key_37_rsa
FallBackToRsh no
# In a pesky lab environment, add the following to your config
# CheckHostIP no
You can now ssh into some_host.com with just
ssh Short_Name.
This part assumes that you are not using a ssh client configuration file and that your keys are protected with a passphrase. An excellent BASH utility script called Keychain automates and simplifies the tedious use of ssh-agents. If your host does not have Keychain installed, ask your administrator. Alternatively you can download and unpack the script into your home directory from the Keychain website.
keychain - honestly you don't need to type this,
simply loading your keys causes this to happenkeychain --inherit any-oncekeychain ~/.ssh/KEYkeychain --clearkeychain --stopadd the following lines to ~/.bash_login and
~/.bashrc
keychainsource ~/.keychain/${HOSTNAME}-shThe most significant difference between SSH and Telnet & rsh is in the realm of security. SSH uses RSA or DSA for public-key cryptography.
Communication from the server to the client is also possible in the same way—the server encrypts using the client's public key and the client decrypts using it's private key.
PubkeyAuthentication yes PasswordAuthentication no
ssh-keygen -t rsa.If you can make an SSH connection, you can (most likely) use that connection as a SOCKS proxy, without any extra setup on the remote computer. Traffic is tunneled securely through the SSH connection. If you are on an unsecured wireless connection, you can use this to effectively secure all your traffic from snooping. You can also use this to bypass IP restrictions, because you will appear to be connecting from the remote computer. Note that DNS traffic is not tunneled.
Pick some big port number (bigger than 1024 so you can use it as
non-root). Here I choose 1080, the standard SOCKS port. Use the
-D option for dynamic port forwarding.
ssh -D 1080 user@host
That's it. Now as long as the SSH connection is open, your application can use a SOCKS proxy on port 1080 on your own computer (localhost). For example, in Firefox on Linux:
|
|