What if you want to transfer files to and from the remote computer? The ssh program offers no way of copying files to the local computer's hard disk. You're limited to working within the confines of the remote machine. You could feasibly use the mail command-line program to e-mail the file to yourself, but that would hardly be the most elegant solution.
To transfer files, you need to use the sftp program. If you've ever used a shell-based ftp program, you'll feel right at home, because sftp isn't very different.
You can initiate a session by typing something similar to the following:
sftp knthomas@192.168.0.11
The same rules as logging in with ssh apply, both in terms of formatting the login command and also confirming the encryption key if this is the first time you've logged in.
The sftp commands are fairly basic. For example, to copy a file from the remote machine, simply type this:
get filename
This will copy the file into the directory you were in on the local machine before you started the sftp session.
By specifying a path after the filename, the file will be copied to the specified local directory:
get filename /home/knthomas/downloaded_files/
Sending files from the local machine to the remote machine is just as easy:
put filename
By specifying a path after the filename, you can ensure the file is saved to a particular remote path.
One useful thing to remember is that any command preceded by an exclamation mark (!, called a bang in Linux-speak) is executed on the local machine as a shell command. So, if you wanted to remove a file on the local machine, you could type:
!rm -rf filename
Simply typing a bang symbol on its own starts a shell session on the local machine, so you can perform even more tasks. When you're finished, type exit to return to the sftp program.
For a list of popular sftp commands, see Table 34-1.
Command | Function |
---|---|
cd | Change the remote directory |
lcd | Change the local directory |
get | Download the specified file |
ls | List the remote directory |
lls | List the local directory |
mkdir | Create a directory on the remote machine |
lmkdir | Create a directory on the local machine |
put | Upload the specified file to the remote machine |
pwd | Print the current remote directory |
rmdir | Delete the remote directory |
rm | Delete the remote file |
exit | Quit sftp |
!command | Execute the specified command on the local machine |
! | Start a temporary local shell session (type exit to return to sftp) |
Help | Show a list of commands |