Exchanging Files with LinuxUnix Hosts Using ftp


Exchanging Files with Linux/Unix Hosts Using ftp

Sometimes you need to transfer files over the network between two computer systems. The command traditionally used to transfer files between two Linux or Unix systems is ftp, which stands for file transfer protocol.

As with remote logins, you can use an ftp client only with systems that allow incoming ftp connections with an ftp server. If you find, while following the instructions in this section, that your connect attempts fail with an error message, it is likely that the remote system does not accept file transfer protocol connections.

Starting ftp and Logging In

Full use of the ftp command also requires that you connect to a remote system and then log in with an account and password; you thus have to be known on the remote system before you can connect via ftp.

To connect to a remote system with ftp, enter the ftp command at the command prompt supplying the domain name of the system you want to connect to as an argument. You are then prompted for your account and password before being delivered to the file transfer prompt. In this example, a connection is made with a system having the hostname archive3.mycompany.com:

 [you@workstation20 ~]$ ftp archive3.mycompany.com Connected to archive3.mycompany.com (10.6.2.2). 220 archive3 FTP server (Version wu-2.6.1-20) ready. Name (10.6.2.2:you): jackhenry 331 Password required for jackhenry. Password: 230-Please read the file README.TXT 230- it was last modified on Fri Aug 2 15:43:13 2002 230 User jackhenry logged in. Remote system type is UNIX. Using binary mode to transfer files. ftp> 

After you log in, you are sitting at the ftp> TRansfer prompt, from which ftp commands can be entered to list, upload, or download files.

Use sftp Whenever Possible Instead of ftp

Like telnet, ftp does not encrypt data as it is sent, meaning that ftp is best used behind a firewall.

An enhanced alternative command, sftp, works the same way but provides encryption just as ssh does. Remote systems that support ssh also usually support sftp. Consider using sftp under these circumstances.


Navigating an ftp Login

You will find that being logged in to a remote system via ftp is not unlike sitting at a remote system's command prompt in some ways. As is the case with a "normal" command prompt, you can display a present working directory with pwd. You can list files and directories with ls, and you can use the cd command to navigate to other directories that you have permission to visit.

There is, however, one major difference between using the ftp screen and a remote system command prompt. When logged in to ftp, you have not one present working directory, but twothe one on the local system and the one on the remote system. These dual working directories facilitate the transfer of files between the two systems; files are normally copied between the local and remote present working directories. The process of transferring files therefore involves the following steps (see the commands to implement them in Table 24.1 and a sample session in the next section):

1.

Set the remote present working directory to the location where the source file lies or where the destination file is to be created.

2.

Set the local present working directory to the location where the source file lies or where the destination file is to be created.

3.

Copy the file from one system to another using get (to copy from the remote present working directory to the local one) or put (to copy from the local present working directory to the remote one).

Table 24.1. Common ftp Commands

Command

Meaning

ls

List files in the remote present working directory.

!ls

List files in the local present working directory.

pwd

Print the remote present working directory.

!pwd

Print the local present working directory.

cd path

Change the remote present working directory to path (may be an absolute path or a relative path).

lcd path

Change the local present working directory to path (may be an absolute path or a relative path; note the l rather than the exclamation mark!).

get file

Transfer file from the remote present working directory to the local present working directory.

put file

Transfer file from the local present working directory to the remote present working directory.

close

Close the current connection to the remote server.

open server

If no connection currently exists, open a new connection to server.

quit

If no connection currently exists, exit ftp and return to the command prompt.


Some of the more fundamental commands that you can use in an ftp session are shown in Table 24.1.

Because this process and command set can seem much more complicated than it actually is, you may find a sample session helpful for illustration purposes.

Working Through a Sample ftp Session

Suppose you have just successfully logged in to your account on a remote ftp server. In most cases, your local present working directory is the same present working directory you were using when you started ftp. At the same time, your remote present working directory often begins as /home/youraccount on the remote system (in this case, /home/jackhenry).

In this session, the goal is to copy files as follows:

  • /home/you/basicfile.txt (local) to /home/jackhenry/basicfile.txt (remote)

  • /home/you/textfiles/mypaper.tex (local) to /home/jackhenry/documents/mypaper.tex (remote)

  • /home/jackhenry/images/mypicture.gif (remote) to /home/you/mypicture.gif (local)

Because the first source file is located in the home directory of the local system and is to be copied to your home directory on the remote system, no changes in the present working directory need to be made. Simply use the put command to copy the first file:

 ftp> put basicfile.txt local: basicfile.txt remote: basicfile.txt 227 Entering Passive Mode (10,6,2,2,225,31) 150 Opening BINARY mode data connection for basicfile.txt. 226 Transfer complete. 115 bytes sent in 0.00075 secs (1.5e+02 Kbytes/sec) ftp> 

The first file has now been copied from the local system to the remote system. The next file resides in the textfiles directory on the local system and will be put into the documents directory on the remote system, so some changes to present working directories are required:

 ftp> lcd textfiles Local directory now /home/you/textfiles ftp> !pwd /home/you/textfiles ftp> cd documents 250 CWD command successful. ftp> pwd 257 "/home/jackhenry/documents" is current directory. ftp> 

Now that both present working directories are correct (as has been verified with !pwd and pwd), you can use put to copy the file:

 ftp> put mypaper.tex local: mypaper.tex remote: mypaper.tex 227 Entering Passive Mode (10,6,2,2,120,95)  150 Opening BINARY mode data connection for mypaper.tex. 226 Transfer complete. 101842 bytes sent in 0.014 secs (7.1e+03 Kbytes/sec) ftp> 

Finally, you must retrieve a file called mypicture.gif from a remote directory and store it in the local home directory:

 ftp> lcd /home/you Local directory now /home/you ftp> !pwd /home/you ftp> cd ../images 250 CWD command successful. ftp> pwd 257 "/home/jackhenry/images" is current directory. ftp> get mypicture.gif 227 Entering Passive Mode (10,6,2,2,232,161) 150 Opening BINARY mode data connection for mypicture.gif (220376 bytes). 226 Transfer complete. 220376 bytes received in 0.179 secs (1.2e+03 Kbytes/sec) ftp> 

All the files in the list have now been transferred; you can close the connection:

 ftp> close 221-You have transferred 322333 bytes in 3 files. 221-Total traffic for this session was 323337 bytes in 3 transfers. 221 Thank you for using the FTP service on archive3. ftp> quit [you@workstation20 ~]$ 

Are Your Files Becoming Corrupted in Transit?

If you find yourself transferring files that seem broken or corrupted afterward, you are likely not transferring in binary mode. To switch to binary mode after connecting to a server, enter the word binary at the ftp> prompt.

If you find that the rather verbose debugging output of the ftp client program bothers you, you can switch it off by entering the word verbose at the ftp> prompt.

For a list of additional ftp commands, enter help at the ftp> prompt; for a brief description of each command, enter help command at the ftp> prompt. For example, typing help binary displays the following output:

 binary     set binary transfer type 




    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net