Thursday, October 22, 2009

The Basics of Using SSH

If you are already familiar with the basic use of SSH, you might want to just skim this section. If, on the other hand, you are an SSH novice, you are in for quite a surprise. SSH is very easy and efficient to use and can help with a wide variety of tasks.

The commands in this section work fine without any setup (assuming you have the SSH daemon running on the remote host). If nothing has been configured, all of these commands use password authentication just like Telnet; except with SSH, the password (and all traffic) is sent over an encrypted connection.

To initiate a connection to any machine as any user and to start an interactive shell, use this command:

% ssh user@host

In addition to connecting to remote hosts, I often use SSH to log in as root on my local machine because it is quicker then using ssh-agent, as discussed later in this chapter.

You can also execute any command in lieu of starting an interactive shell. This displays memory usage information on the remote host:

% ssh user@host free
total used free shared buffers cached
Mem: 126644 122480 4164 1164 29904 36300
-/+ buffers/cache: 56276 70368
Swap: 514072 10556 503516

Finally, there is the scp command that allows you to copy files between hosts using the SSH protocol. The syntax is very similar to the standard cp command, but if a filename contains a colon, it is a remote file instead of a local file. Like the standard ssh command, if no username is specified on the command line, your current username is used. If no path is specified after the colon, the user's home directory is used as the source or destination directory. Here are a few examples:

% scp local_file user@host:/tmp/remote_file
% scp user@host:/tmp/remote_file local_file
% scp user1@host1:file user2@host2:

The last example copies the file named file from user1's home directory on host1 directly into user2's home directory on host2. Since no filename is given in the second argument, the original filename is used (file, in this case).

No comments: