keepcalculator

06 Numbers and Powers

Mac Terminal as an FTP or SFTP Client

Card No. 06·75762 · Updated · Standard formula, shown below

Yes, you can use the Mac Terminal as a fully functional FTP and SFTP client—no extra software needed. The built-in ftp and sftp commands let you connect to remote servers, browse directories, upload and download files, and manage remote content directly from the command line. This guide explains how to use both, when to choose one over the other, and how to troubleshoot common issues.

Background: FTP vs. SFTP

FTP (File Transfer Protocol) has existed since the early days of the internet. It transfers files in plain text, including your username and password. SFTP (SSH File Transfer Protocol) is a completely different protocol that runs inside an SSH (Secure Shell) connection, encrypting all data in transit. Despite the similar name, SFTP is not a secure version of FTP—it is a separate protocol that uses the SSH port (usually 22) and the SSH authentication system.

macOS includes both ftp and sftp clients in Terminal. The ftp command is deprecated but still present in recent macOS versions; sftp is actively maintained and recommended for any connection where security matters. For legacy FTP servers that only support plain FTP, you can still use the ftp command, but be aware that your credentials and files travel unencrypted.

How to Connect Using SFTP

Open Terminal (found in /Applications/Utilities/) and type the following, replacing username and server.example.com with your actual credentials:

sftp username@server.example.com

If the server uses a non-standard SSH port (not 22), add the -P flag with the port number:

sftp -P 2222 username@server.example.com

You will be prompted for your password (or a passphrase if using SSH keys). Once connected, the prompt changes to sftp>. Here are the essential commands:

  • ls – list files on the remote server
  • cd directory – change directory on the remote server
  • pwd – show current remote directory
  • get filename – download a file to your local machine
  • put filename – upload a file to the remote server
  • mget *.txt – download multiple files matching a pattern
  • mput *.jpg – upload multiple files matching a pattern
  • rm filename – delete a remote file
  • mkdir directory – create a remote directory
  • exit or quit – close the connection

To manage local files while connected, prefix commands with l: lls (local list), lcd (local change directory), lpwd (local print working directory). This is useful when you need to see where a downloaded file went.

How to Connect Using FTP

For plain FTP, the syntax is similar but uses the ftp command:

ftp username@server.example.com

If the server requires an anonymous login, use ftp without a username and type anonymous when prompted. The FTP prompt is ftp> and most commands are the same as SFTP (ls, cd, get, put, etc.). One notable difference: FTP uses mget and mput for multiple files, but you may need to use prompt to disable confirmation for each file.

macOS also supports ftps (FTP over TLS) via the curl command, but the built-in ftp client does not support FTPS. If you need FTPS, consider using curl or a graphical client.

Choosing Between FTP and SFTP

For any new connection, choose SFTP. It encrypts your password and data, uses the same SSH keys you may already have, and works through firewalls that allow SSH. Plain FTP should only be used when connecting to a legacy server that does not support SFTP or FTPS, and even then, avoid transferring sensitive information. Many modern hosting providers offer both, but SFTP is almost always the safer default.

If you are unsure which protocol a server supports, check the control panel of your hosting provider or contact their support. You can also test by trying to connect with sftp first—if it fails with a “connection refused” or “not an SSH” error, try ftp.

Common Problems and Solutions

“Connection refused” – The server is not listening on the expected port. For SFTP, verify the SSH port (default 22). For FTP, the default is 21. If you are using a non-standard port, you must specify it with -P (SFTP) or by typing the port after the hostname in FTP (e.g., ftp hostname 2121).

“Host key verification failed” – This happens with SFTP when the server’s SSH key has changed or is unknown. If you trust the server, you can remove the old key from ~/.ssh/known_hosts using ssh-keygen -R hostname, then reconnect. Be cautious: this could indicate a man-in-the-middle attack.

“Permission denied” – Your username or password is wrong, or your SSH key is not accepted. Double-check credentials. If using a key, ensure it is added to the SSH agent with ssh-add ~/.ssh/id_rsa.

File transfer is slow – For many small files, SFTP can be slower due to encryption overhead. Consider compressing files into a single archive (e.g., tar czf files.tar.gz directory/) before transferring, then extract on the remote server.

Binary vs. text mode – FTP has separate modes for binary and ASCII. macOS’s ftp defaults to binary, which is correct for most files. If you transfer a text file that appears corrupted, try ascii before transferring. SFTP does not have this issue—it always transfers raw bytes.

Advanced Tips and Scripting

You can automate SFTP transfers by creating a batch file with commands and running sftp -b batchfile username@host. Each line in the batch file is an SFTP command. For example:

cd /www/uploads
put index.html
quit

This is useful for scheduled backups or deployments. You can also use SSH keys for passwordless authentication, which is more secure than storing passwords in scripts. Generate a key pair with ssh-keygen -t ed25519, then copy the public key to the server’s ~/.ssh/authorized_keys file.

For one-off transfers, you can pipe commands directly: echo "put file.txt" | sftp user@host. This avoids creating a batch file.

Alternatives to Terminal

If you prefer a graphical interface, macOS offers several free options. The built-in Finder supports connecting to FTP servers via Go > Connect to Server (Cmd+K) and entering ftp://hostname or sftp://hostname. However, Finder’s SFTP support is limited and may not work with all servers. Third-party apps like FileZilla, Cyberduck, or Transmit provide a more robust experience with drag-and-drop, queued transfers, and bookmark management. For command-line users, lftp (installable via Homebrew) supports FTP, SFTP, FTPS, and offers advanced features like parallel transfers and mirroring.

Frequently Asked Questions

Is the macOS Terminal FTP client secure?

No, the built-in ftp command is not secure—it sends data and credentials in plain text. Use sftp instead, which encrypts everything. If you must use FTP, avoid transferring sensitive files and use it only on trusted networks.

How do I upload a file to a remote server using Terminal?

Connect with sftp user@host, navigate to the target directory with cd, then use put /local/path/filename. To upload multiple files, use mput *.txt.

Can I use Terminal to download a file from an FTP server without interactive login?

Yes, you can use curl for one-off downloads: curl -u username:password ftp://host/path/file.txt -o file.txt. For SFTP, use sftp with a batch file or scp if you just need a simple copy.

Why does SFTP say “connection closed” immediately after login?

This usually means the server’s SSH configuration restricts the user to a specific subsystem or directory. Check with your hosting provider that your account is allowed SFTP access. It could also be that the server only allows SFTP with a password, not a key.

What is the difference between SFTP and FTPS?

SFTP is a file transfer protocol that runs over SSH (port 22). FTPS is FTP over TLS/SSL (usually port 990). They are incompatible. macOS’s built-in sftp supports SFTP only; for FTPS, use curl or a graphical client.

Can I resume an interrupted download with Terminal SFTP?

Yes, SFTP supports the reget command to resume a download, and reput to resume an upload. These are not available in the plain FTP client, but curl can also resume with -C -.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →