ftp Command: Tutorial & Examples
Transfer files over the File Transfer Protocol (FTP)
The ftp command is a classic command-line utility used to transfer files between a local system and a remote FTP server over a network. It provides an interactive interface for connecting to FTP servers, navigating remote directories, uploading and downloading files, renaming and deleting files, and managing remote file systems. While FTP itself is an older protocol largely replaced by more secure alternatives such as SFTP and FTPS, the ftp command remains relevant for legacy environments and specific use cases requiring FTP. This article explores how the ftp command works, its usage, common parameters, practical examples, troubleshooting, and security considerations.
How ftp Works
The ftp command implements the File Transfer Protocol, a standard network protocol designed for transferring files between a client and server over TCP/IP networks. FTP typically uses port 21 for control commands, while the actual file data is transferred over a separate data connection, which can operate in one of two modes: active or passive.
Active Mode: The client opens a random port and sends the port information to the server via the control connection. The server then initiates a connection back to the client’s specified port for data transfer. This mode can cause issues if the client is behind a firewall or NAT, as incoming connections to the client port may be blocked.
Passive Mode: The server opens a random port and sends its address and port number to the client. The client initiates the data connection to the server’s specified port. This mode is more firewall and NAT friendly and is often the default mode for modern FTP clients.
When you run the ftp command, it acts as a client connecting to an FTP server. After successful authentication, you can issue FTP commands to navigate directories, transfer files, and manage remote files.
Typical ports used by FTP:
- Port 21: Control connection (commands and responses)
- Port 20: Data connection (in active mode for some implementations)
- Random high ports: Data connection (in passive mode)
You can switch between active and passive modes within the ftp session or using command-line parameters.
What ftp Does
The ftp command allows you to:
- Connect to FTP servers by specifying hostname or IP address.
- Authenticate using a username and password.
- Navigate remote directories with commands like cd and pwd.
- List files and directories using ls or dir.
- Download files from the server using get or mget.
- Upload files to the server using put or mput.
- Rename and delete files on the server.
- Create and remove directories on the remote system.
- Switch between ASCII (text) and binary transfer modes to prevent file corruption.
- Close the FTP session gracefully.
This functionality is essential for transferring website files, backups, software packages, and other data across systems.
Why ftp Is Important
FTP is one of the earliest protocols designed specifically for file transfer and remains important because:
- Many legacy systems, embedded devices, and network appliances support only FTP.
- FTP servers often provide anonymous access for public file sharing.
- It is widely supported across operating systems, networking equipment, and software.
- Understanding FTP and the ftp command helps manage legacy infrastructures.
- FTP is sometimes used in controlled or isolated environments where security concerns are minimal.
Despite its importance, FTP lacks encryption, so it is generally recommended to use secure alternatives such as sftp or FTPS when possible.
How to Use ftp
To start an FTP session, run:
ftp hostname_or_ip
You will be prompted for a username and password unless anonymous login is enabled on the server. After logging in, you will see an interactive ftp prompt where you can issue commands.
Example basic workflow:
ftp ftp.example.com
Name (ftp.example.com:user): username
Password:
ftp> ls
ftp> cd /path/to/directory
ftp> get file.txt
ftp> put localfile.txt
ftp> bye
The bye
or quit
command terminates the FTP session and exits the client.
Common Command Line Parameters
The ftp command supports several options that modify its behavior:
- -v : Enable verbose mode to display server responses and detailed information.
- -n : Do not automatically attempt to log in upon connection; useful for scripting.
- -i : Disable interactive prompting during multiple file transfers (mget, mput).
- -g : Disable filename globbing (wildcard expansion) to treat wildcards literally.
- -p : Enable passive mode for data connections (recommended behind firewalls or NAT).
- -A : Use active mode explicitly (disables passive mode).
- -d : Enable debugging output useful for troubleshooting connection issues.
- -s:filename : Read FTP commands from a file for batch processing (Windows ftp).
Example connecting with passive mode enabled:
ftp -p ftp.example.com
Practical Examples Using ftp
Example 1: Download a Single File
ftp ftp.example.com
Name (ftp.example.com:user): anonymous
Password: user@example.com
ftp> cd /pub/files
ftp> binary
ftp> get example.zip
ftp> bye
Sample Output:
200 PORT command successful.
150 Opening BINARY mode data connection for example.zip (1024000 bytes).
226 Transfer complete.
Example 2: Upload Multiple Files Without Confirmation
ftp -i ftp.example.com
Name (ftp.example.com:user): user
Password:
ftp> binary
ftp> mput *.txt
ftp> bye
Example 3: Use FTP Commands in a Script
Create a file named ftp_commands.txt:
open ftp.example.com
user user password
binary
cd /upload
put file1.txt
put file2.txt
bye
Run the script non-interactively:
ftp -n < ftp_commands.txt
Example 4: Recursive Download Using mget
Note: The ftp command does not support recursive directory transfers natively. You may need to download files directory by directory or use tools like wget
or
lftp
for recursive downloads.
Example 5: Scripting with Here-Document
ftp -n ftp.example.com <<END_SCRIPT
user username password
binary
cd /files
get data.tar.gz
bye
END_SCRIPT
Common Errors and Troubleshooting
- Connection Refused or Timeout: Check if the FTP server is reachable and listening on port 21, and verify firewall settings.
- Authentication Failed: Ensure correct username and password; some servers require anonymous login with a valid email as a password.
- Data Connection Failures: Often caused by firewall or NAT blocking active mode data connections; try enabling passive mode using the
-p
flag orpassive
command. - Transfer Corrupted Files: Occurs when transferring binary files in ASCII mode; always switch to binary mode before transferring binaries.
- Filename Issues: Filename case sensitivity varies between servers; be cautious with letter casing.
- Command Not Supported: Some FTP servers may not support certain commands; check server documentation.
- Timeouts: Increase timeout settings on server or client if transfers abort prematurely.
Use the -d
flag to enable debugging and see detailed connection logs for diagnosis.
Potential Problems and Pitfalls
- Security Risks: FTP transmits credentials and data in plaintext, exposing them to interception. Avoid using FTP over untrusted or public networks.
- Firewall and NAT Issues: Active mode requires the server to connect back to the client, often blocked by firewalls. Passive mode mitigates this problem.
- Transfer Mode Mistakes: Transferring binary files (images, archives, executables) in ASCII mode corrupts the files.
- Server Compatibility: Different FTP servers may behave differently or support different features.
- No Encryption: FTP lacks built-in encryption; use FTPS or sftp for secure transfers.
Tips and Best Practices
- Switch to binary mode (
binary
command) before transferring non-text files. - Use passive mode (
passive
command or-p
flag) behind firewalls or NAT devices. - Avoid using FTP on untrusted networks; prefer secure alternatives.
- Automate repetitive tasks using scripting and the
-n
non-interactive option. - Use checksums (
md5sum
) to verify file integrity after transfer. - Use verbose mode (
-v
) to troubleshoot transfers. - Store FTP credentials securely in
.netrc
files with strict permissions to enable unattended logins. - Always exit FTP sessions cleanly with
bye
orquit
. - For recursive transfers, consider dedicated FTP clients or other tools like
lftp
orwget
.
Customization and Configuration
You can store FTP login credentials in a .netrc
file in your home directory to automate logins without entering passwords interactively. The .netrc
file
format:
machine ftp.example.com
login username
password secret_password
Ensure .netrc
file permissions are set to 600 to prevent unauthorized access:
chmod 600 ~/.netrc
You can also configure default settings like passive mode or transfer type in your FTP client configuration or scripts.
See Also
Further Reading
- SSH, The Secure Shell by Daniel J. Barrett, Richard E. Silverman, Robert G. Byrnes (partner link)
- SSH Mastery: Openssh, Putty, Tunnels and Keys by Michael Lucas (partner link)
- Linux for Hackers by Mark Reed (partner link)
- How Linux Works by Brian Ward (partner link)
- Linux for Beginners by Jason Cannon (partner link)
- Expert Linux Administration Guide by Vishal Rai (partner link)
As an Amazon Associate, I earn from qualifying purchases.