ftp (1/2)
The ftp command-line client talks to FTP servers: a control connection on TCP 21 plus a separate data connection. Part 1 covers Windows ftp.exe (active only, script-driven) and Linux/macOS tnftp (passive by default, auto-fetch URLs, retries, upload).
148.1 What it does, where it runs
| Platform | Client | Notes |
|---|---|---|
| Windows | ftp.exe (built in) | Active mode only, no TLS |
| Linux | tnftp (apt install tnftp) | Passive by default, URL fetch, resume |
| macOS | Removed in 10.13+ | brew install tnftp |
- Lab: client 192.168.168.20 (tnftp), lab-ftp 192.168.168.10 (vsftpd), lab-web (python http server).
148.2 How it works: passive vs active
- Step 1
1.Client opens the control connection to lab-ftp TCP 21
- Step 2
2.USER / PASS over the control connection -> 230 Login successful
- Step 3
3.For a file the client sends EPSV
- Step 4
4.Server replies 229 with a high port
- Step 5
5.Client opens the data connection itself and downloads
- Step 1
1.Client sends EPRT |1|192.168.168.20|57205| (its IP + port)
- Step 2
2.Server replies 200 EPRT command successful
- Step 3
3.Server opens a new data connection to the client's port
- Step 4
4.Behind a firewall/NAT this incoming connection is blocked
- Step 5
5.That is why passive is the default today
148.3 Windows ftp: command-line options
- On Windows -v turns server replies OFF, -A is anonymous login, -n disables auto-login, -i disables mget/mput prompts.
- -s:file runs FTP commands from a UTF-8 file (no space after -s:); use it instead of < redirection.
- -d debug, -g no globbing, -a any local interface, -x/-r/-b/-w buffer sizes; host is the last parameter.
C:\Windows\System32\ftp.exe -? -v Suppresses display of remote server responses. -n Suppresses auto-login upon initial connection. -s:filename Specifies a text file containing FTP commands; the commands will automatically run after FTP starts. -A login as anonymous. - mget and mput commands take y/n/q for yes/no/quit.Script example (Microsoft Learn docs, not run on the office PC):
REM resync.txt (UTF-8, one command per line)open ftp.example.microsoft.comuser User1binaryget test.txt test1.txtbye ftp -n -s:resync.txt148.4 Windows ftp: interactive commands
- 54 command forms in four groups: connection/folders/listing, transfers/files, modes/toggles, raw/help.
- Default type is ASCII: always run binary before a zip/exe/image or the file can get corrupted.
- mget/mput ask y/n/q per file; literal/quote send raw commands; ! opens a local shell.
open host [port] connect (default port 21)user name [pass] loginclose / disconnect end session, stay at ftp>bye / quit end session + exitcd, lcd, pwd, dir, ls, mdir, mlsget / recv, put / send, mget, mput, appenddelete, mdelete, rename, mkdir, rmdirascii (default) / binary / typeprompt, glob, hash, bell, verbose, debug, trace, statusliteral SYST / quote PASV raw command to serverremotehelp, help / ? [cmd], ! local shell148.5 tnftp command-line options
- -n disables auto-login (login comes from a user line); in scripts tnftp stays quiet.
- -v shows all replies (220, 230...); -d debug shows every command with ---> and masks the password as XXXX.
- Passive is default (EPSV, 229); -p is an old option for the same; -A forces active (EPRT, 200).
- -i turns off mget prompts: without it the prompt ate the next script line and mget aborted with EOF received.
- -r 2 retries the connection every 2 s; -P port or host port; -a anonymous login (lab showed only pub/).
ftp -n lab-ftp <<'EOF'user student lab123pwdbyeEOFftp -n -v lab-ftp <<'EOF' # same 3 linesftp -d -n lab-ftp <<'EOF' # user, ls, bye$ ftp -d -n lab-ftp <<'EOF' ...---> USER student---> PASS XXXX---> SYST---> FEAT---> EPSV---> LIST-rw-r--r-- 1 1000 1000 29 Nov 09 05:28 report.txt---> QUITftp -d -v -n lab-ftp <<'EOF'user student lab123get report.txtbyeEOFftp -p -d -v -n lab-ftp <<'EOF' # same (passive, EPSV)ftp -A -d -v -n lab-ftp <<'EOF' # same (active, EPRT)$ ftp -A -d -v -n lab-ftp ...---> EPRT |1|192.168.168.20|57205|200 EPRT command successful. Consider using EPSV.---> RETR report.txt150 Opening BINARY mode data connection for report.txt (29 bytes).226 Transfer complete.ftp -i -v -n lab-ftp <<'EOF'user student lab123cd logsmget *.logbyeEOFlocal: mon.log remote: mon.log226 Transfer complete.local: tue.log remote: tue.log226 Transfer complete.local: wed.log remote: wed.log226 Transfer complete.$ lsmon.log tue.log wed.logtimeout 20 ftp -r 2 -v -n -P 2121 lab-ftp <<'EOF'user student lab123byeEOFftp -v -n lab-ftp 2121 <<'EOF' # sameftp -a -v lab-ftp <<'EOF' # anonymouspwdlsbyeEOFConnected to lab-ftp.220 n148 lab FTP server230 Login successful.Remote directory: /229 Entering Extended Passive Mode (|||30000|)150 Here comes the directory listing.drwxr-xr-x 2 0 0 4096 Nov 09 05:28 pub226 Directory send OK.221 Goodbye.| Option | tnftp meaning |
|---|---|
| -4 / -6 | IPv4 only / IPv6 only |
| -N file | Use this file instead of ~/.netrc |
| -q 60 | Quit after a 60 s stall |
| -R | Restart (resume) an auto-fetch |
| -s IP | Source IP address |
| -T get,100000 | Rate limit in bytes/s |
| -V / -t / -x | Verbose+progress off / packet trace / socket buffer |
| -o file / -u URL file | Output file for auto-fetch / upload |
148.6 tnftp auto-fetch URLs
- ftp://user:pass@host/path: login, download and exit in one command.
- host:path is the classic form (anonymous); a wildcard works like mget; a trailing / stays interactive in that folder.
- ;type=A fetches in ASCII (CRLF became LF); ;type=I binary; %2F starts the path at the root (/etc/motd).
- http:// and https:// work too (https: no cert check); -o file saves, -o - prints; a 404 gives rc=1.
- -T get,100000 limited a 300000-byte file to exactly 3 s; -u uploads to a URL folder.
ftp -v ftp://student:lab123@lab-ftp/report.txtftp -v lab-ftp:pub/readme.txtftp 'ftp://student:lab123@lab-ftp/logs/*.log'ftp 'ftp://student:lab123@lab-ftp/notes.txt;type=A' # CRLF -> LFftp -o motd.txt ftp://student:lab123@lab-ftp/%2fetc/motdftp -v -o out.html http://lab-web/index.htmlftp -v http://lab-web/missing.txt; echo rc=$?time ftp -v -T get,100000 http://lab-web/app.tar.gz # 300000 bytes: 3 sftp -v -u ftp://student:lab123@lab-ftp/upload/ notes-client.txt$ ftp -o motd.txt ftp://student:lab123@lab-ftp/%2fetc/motd$ head -1 motd.txtWelcome to Alpine!$ ftp -v http://lab-web/missing.txt; echo rc=$?Requesting http://lab-web/missing.txtftp: Error retrieving file `404 File not found'rc=1[srv]$ ls -l /home/student/upload-rw-r--r-- 1 student student 16 Nov 9 05:29 notes-client.txt148.7 tnftp interactive: connection and login
- open host [port] connects (ftp host is a synonym); user u p logs in.
- A wrong password gives 530 Login incorrect; user can be retried on the same connection.
- account sends ACCT (vsftpd answers 502).
- close / disconnect end the session but stay at ftp>; bye / exit / quit leave; gate toggles TIS gate-ftp mode.
ftp -n -v <<'EOF'open lab-ftpuser student wrongpassuser student lab123account acct1pwdcloseftp lab-ftp 2121user student lab123disconnectexitEOFConnected to lab-ftp.220 n148 lab FTP server (port 2121)331 Please specify the password.230 Login successful.221 Goodbye.148.8 Reminder: the FTP login is clear text
timeout 8 tcpdump -i eth0 -p -n -l -A \ 'tcp port 21 and host 192.168.168.10' > /tmp/cap.txt &ftp -n lab-ftp <<'EOF'user student lab123byeEOFgrep -a -E 'USER|PASS' /tmp/cap.txtIP 192.168.168.20.45878 > 192.168.168.10.21USER studentIP 192.168.168.20.45878 > 192.168.168.10.21PASS lab123148.9 Common mistakes
- Assuming ftp -A means the same everywhere: Windows -A = anonymous, tnftp -A = active (-a = anonymous).
- Thinking PASS XXXX in debug means it is hidden: masking is screen-only, the wire carries PASS lab123.
- Transferring binaries in ASCII mode on Windows ftp.exe.
- Scripted mget without -i: prompts swallow the next commands.
- Using active mode behind NAT/firewall: the server's incoming data connection is blocked.
148.10 Interview tips
- FTP uses TCP 21 for control and a separate data connection; passive (EPSV/PASV) = client opens data, active (EPRT/PORT) = server opens data.
- Automating: Windows ftp -i -n -s:script.txt; Linux tnftp URL fetch, ftp -u URL file, -r for retries.
- Plain FTP sends credentials in clear text: prefer SFTP or FTPS in production.
- Always set binary mode for non-text files.