4.12. Remote Logins and Command Execution
You are probably connected to a network, either within your own home or office, or through dial-up to the Internet. Sometimes you want to log in to another system, or copy a file to or from another system.
If you need help setting up networking, check Chapter 13 and the following chapters. In this section we assume you are on the network already. If you can view a web page in a browser, you're connected and can carry out the commands in this section. We'll use a package called SSH that's installed on most, if not all, Linux distributions.
SSH stands for Secure Shell, and denotes the project developers' focus on protecting your communications from snooping and hijacking. SSH has become an extremely respected and popular protocol for communicating between systems, and is supported on many different types of systems, such as the Putty graphical interface for Windows (http://www.chiark.greenend.org.uk/~sgtatham/putty).
Linux uses OpenSSH, a free software implementation (http://www.openssh.com). It rarely has bugs (although one will turn up once in a while, so for security reasons you should keep up with your distribution's updates), and it supports the latest standard, SSH protocol version 2. If you decide to do some heavy internetworking with SSH, you can get quite deep into it through SSH, The Secure Shell: The Definitive Guide (O'Reilly).
This section lays out the four or five commands you'll use most often. Suppose you have an account named mdw on a remote system called eggplant. You can log in as follows:
$ ssh -l mdw eggplant
The -l specifies the account on the remote system. Another syntax with identical effects is:
$ ssh mdw@eggplant
If your account name is the same on the local and remote systems, you can omit the name and just enter:
$ ssh eggplant
Each time you start an ssh session, it prompts for the password of the account on the remote system.
During the session, if you have to do something on your local machine, you don't have to log out or switch windows. Suspend the remote login session by entering a tilde character (~) followed by Ctrl-Z. (Sometimes the tilde is not caught by SSH; if it isn't, try again. You're successful if the tilde is not displayed.) To pick up the session you suspended, use fg as for local programs you suspend.
You might want to run a single command instead of starting up a shell on the remote system; if so, just enter the command after the hostname:
$ ssh -l mdw eggplant rm logfiles/temp_junk
Or, if you can omit your name:
$ ssh eggplant rm logfiles/temp_junk
Filenames such as logfiles/temp_junk are interpreted as if you were located in your home directory (the directory you'd be in if you logged in). Use absolute pathnames (such as /home/mdw/logfiles/temp_junk) if you want to make sure you're naming the right file in the right location.
The manpage for ssh can fill in interesting details, such as how to run X Window System graphical programs over SSH, and how to eliminate the annoyance of having to enter your password at each invocation of the command. (To be really robust, though, you may have to play around with configuration files beyond what the manpage tells you.)
You can copy files using another command from the SSH suite, scp. The following copies a file from your local system to eggplant:
$ scp logfiles/temp_junk mdw@eggplant:
Once again, the username and @ can be omitted if it's the same on both systems. (But the -l syntax doesn't work on scp; it uses a different -l option for a different purpose.)
Be sure to include the final colon; without it, you simply copy the file to a new file named eggplant on your local system. On eggplant, the default directory is your home directory (as with ssh). You can specify that the file be copied to any directory you have access to, with a path relative to the home directory or with an absolute pathname.
To do the reverse operationcopy a file from the remote system to your ownenter:
$ scp mdw@eggplant:logfiles/temp_junk .
We used a single dot here to denote the local directory where you're executing the command. Any relative or absolute pathname could be specified instead.
To copy a directory, add the -r option:
$ scp -r mdw@eggplant:logfiles .