Skip to content
Tutorials Center
My learning

Topics

Lesson 55 of 79 ยท Application Layer: Names and Web

FTP, SSH, DHCP-adjacent and Other Protocols

A tour of the supporting protocols behind real networks: FTP, SFTP, SSH, telnet, NTP time sync, SNMP monitoring, syslog, plus SMB file sharing, TFTP and NNTP.

Besides the web and email, a working network runs on a crowd of smaller protocols. Administrators log into remote machines, copy files, keep every clock in agreement, collect logs and ask routers how they feel. Each of these protocols solves one narrow problem, and most of them are older than the web. Knowing them helps you read firewall rules, troubleshoot services and understand why some old designs have been replaced.

This lesson covers file transfer (FTP, FTPS, SFTP, TFTP), remote login (telnet and SSH), time synchronization (NTP), monitoring (SNMP) and logging (syslog). Most of it is explained with protocol exchanges you can read, and two small Python programs let you compute the same numbers the real protocols compute: an NTP clock offset and the addresses inside FTP's passive-mode reply.

FTP: the File Transfer Protocol

FTP (RFC 959, 1985) is among the earliest Internet protocols. It has an unusual design: it uses two TCP connections.

  • The control connection (server port 21) carries commands and replies as text lines, much like SMTP: USER, PASS, CWD, LIST, RETR, STOR, QUIT, with three-digit reply codes (230 logged in, 550 file unavailable).
  • A separate data connection is opened for every directory listing and every file transfer, and closed afterwards.

Who opens the data connection depends on the mode, and this is the source of FTP's famous trouble with firewalls and NAT.

Active mode. The client listens on a port and tells the server about it with PORT h1,h2,h3,h4,p1,p2. The server then connects back to the client from its port 20. This fails behind NAT and client firewalls, because an unsolicited incoming connection arrives.

Passive mode. The client sends PASV. The server opens a random port and replies 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). The client connects to that address, so every connection is outgoing from the client, which is firewall-friendly. Passive mode is the default today. But now the server must allow a wide range of ports, and firewalls and NAT devices often include an FTP-aware helper that reads the control channel and opens the right port dynamically. (EPSV is the extended form that works with IPv6 and sends only a port number.)

sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: control connection to port 21
    S-->>C: 220 ready
    C->>S: USER alice / PASS ...
    S-->>C: 230 logged in
    C->>S: PASV
    S-->>C: 227 Entering Passive Mode (192,0,2,10,195,80)
    C->>S: new data connection to 192.0.2.10 port 50000
    C->>S: RETR report.txt (on the control connection)
    S-->>C: file bytes on the data connection
    S-->>C: 226 Transfer complete (control)

Passive mode: commands travel on the control connection, bytes on a second connection the client opens.

The port is sent as two numbers, because each is one byte of the 16-bit port: the port is p1 * 256 + p2. This small program parses a 227 reply and builds an active-mode PORT command, so you can see the arithmetic.

Python Decode PASV replies and build PORT commands

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why plain FTP is obsolete

FTP sends the username, password and all data in clear text, so anyone on the path can read them (see network security fundamentals). It also has the two-connection firewall problem, no integrity protection and no resume across sessions in the original design. Two secure replacements exist, and they are different things:

FTPS SFTP
What it is FTP with TLS added A completely different protocol that runs inside SSH
Ports 21 (explicit TLS via AUTH TLS) or 990 (implicit) plus data ports One port: 22
Firewall friendliness Poor: still two connections Excellent: one connection
Authentication Passwords or client certificates Passwords or SSH keys

Do not confuse SFTP with "simple FTP" or with FTP over SSH. For new work, prefer SFTP, or scp/rsync over SSH, or HTTPS-based transfers (object storage, signed URLs).

TFTP and the DHCP connection

The title promises "DHCP-adjacent" protocols, and the best example is TFTP, the Trivial File Transfer Protocol. It runs over UDP port 69, has no login and no directory listing, and sends a file in tiny numbered blocks with stop-and-wait acknowledgement (exactly the idea from flow control and ARQ). It is too simple to be safe on its own, but simple enough to fit in a chip's firmware. So a computer that boots from the network (PXE) first uses DHCP to get an IP address plus the name of a boot file and the address of a TFTP server in DHCP options, and then downloads its boot loader with TFTP. IP phones and switches fetch configuration the same way. DHCP's ancestor, BOOTP, did the same job with fewer features.

Telnet and SSH: remote login

Telnet (1969) gives you a text terminal on a remote machine over TCP port 23. It sends every keystroke, including your password, as readable bytes. It was fine on a trusted research network and is dangerous today. You still meet it as a handy test tool: telnet host 25 opens a raw TCP connection so you can type an SMTP or HTTP conversation by hand, although nc (netcat) is the modern tool for that.

SSH (Secure Shell, TCP port 22) replaced it and became the universal tool for administering servers. It is an encrypted, authenticated channel with several uses on top:

  • Remote shell: a login session whose traffic cannot be read or changed on the way.
  • File transfer: SFTP and scp over the same channel.
  • Port forwarding (tunnels): ssh -L 8080:db.internal:5432 jump.example.org makes a local port that leads, through the server, to a private service. -R does the reverse, and -D makes a SOCKS proxy.
  • Jump hosts and agent forwarding, and the transport for git over SSH.

How SSH connects:

  1. The client and server agree on algorithms and run a key exchange (a Diffie-Hellman variant), producing fresh session keys. The server proves its identity by signing the exchange with its host key. Everything afterwards is encrypted and integrity protected.
  2. The client checks the host key. The first time, SSH asks "are you sure you want to continue?" and shows the key's fingerprint, then stores it in known_hosts. On later visits a different key triggers a loud warning: either the server was reinstalled or someone is impersonating it (a man-in-the-middle attack). This model is called trust on first use.
  3. The user authenticates, ideally with a key pair instead of a password: the client proves it holds the private key matching a public key listed in the server's authorized_keys, without ever sending the private key.
  4. The session opens one or more multiplexed channels (shell, forwarded ports, file transfer) over the single TCP connection.

The fingerprint you compare is just a hash of the public key blob, Base64-encoded. The next program computes one in the same format that OpenSSH prints (the key itself is made up), and also decodes syslog priorities, which the next section explains.

Python SSH fingerprints and syslog priorities

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

NTP: agreeing on the time

Logs, certificates, Kerberos tickets, distributed databases and financial records all depend on clocks that agree. Computer clocks drift (seconds per day for cheap crystals), so hosts continuously correct themselves with the Network Time Protocol (NTP), UDP port 123. NTP can keep machines within a few milliseconds over the Internet, and far better on a local network.

Time is organized in strata. Stratum 0 devices are reference clocks (atomic clocks, GPS receivers). Stratum 1 servers are directly connected to them. A stratum 2 server synchronizes to stratum 1, and so on, up to 15. Clients pick several servers and discard outliers.

The core measurement uses four timestamps from one request/response:

  • T1: client's clock when it sends the request.
  • T2: server's clock when the request arrives.
  • T3: server's clock when it sends the reply.
  • T4: client's clock when the reply arrives.

From these, the client computes:

TEXT
offset = ((T2 - T1) + (T3 - T4)) / 2
delay  = (T4 - T1) - (T3 - T2)

The offset is how far the client clock is behind the server (positive means behind), and the delay is the network round-trip time minus the server's processing time. The offset formula averages the apparent one-way differences, so the real network time cancels out if the two directions take equally long. NTP cannot detect an asymmetric path, which is its basic accuracy limit. The program below simulates both cases.

Python NTP offset and delay from four timestamps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When the offset is small, clients usually slew the clock (run it slightly faster or slower) rather than jumping, so that time never goes backward. Large errors are stepped. In practice you rarely run this yourself: operating systems ship a client (chrony, systemd-timesyncd, Windows Time), and you only choose which servers to trust. NTP over plain UDP can be spoofed, so secured variants such as NTS (Network Time Security) add authentication.

NTP time is UTC-based and has no time zones. Zones and daylight-saving rules are applied later by the operating system, which is why servers should keep their clocks in UTC.

SNMP: managing network devices

Switches, routers, printers and UPS units need to be monitored, but they are not general computers. The Simple Network Management Protocol (SNMP) gives them a standard way to expose their state. There are three parts:

  • A manager (the monitoring system) that asks questions.
  • An agent running on each device that answers them.
  • A MIB (Management Information Base): a tree of named values, each identified by an OID (object identifier), a dotted number such as 1.3.6.1.2.1.1.5.0 (sysName, the device name). Standard MIBs cover interfaces, IP statistics and more, and vendors add their own branches.

Messages travel over UDP: queries to port 161, and traps to port 162. The main operations are:

Operation Purpose
GET / GETNEXT Read a value, or the next one in OID order (used to walk a table).
GETBULK Read many values in one request (v2c and v3).
SET Change a value, for example shut down an interface.
TRAP / INFORM The agent reports an event on its own: a link went down, a fan failed.

A typical use is polling interface counters (ifInOctets, ifOutOctets) every minute and graphing the difference to show bandwidth, and sending a trap to raise an alert when a link goes down. Note that these counters only grow, so a monitor computes rates from two readings and must handle the counter wrapping around.

Security is the weak spot. SNMP v1 and v2c authenticate with a community string, a plain-text password that defaults to public on many devices, and anyone who learns a writable community can reconfigure the device. SNMP v3 adds real user authentication and encryption; use it and block SNMP from untrusted networks. Many modern systems also expose metrics through HTTP APIs and streaming telemetry, but SNMP remains everywhere in networking equipment.

SNMP versions and security levels

SNMP exists in three main versions. SNMPv1 (1988) is the original, with 32-bit counters and a community string for security. SNMPv2c adds GETBULK, 64-bit counters and better error reporting, but keeps the community string, which is sent in clear text. SNMPv3 adds a real security model called USM with three security levels:

Level Authentication Encryption Meaning
noAuthNoPriv username only none Identifies a user but proves nothing
authNoPriv HMAC with a password-derived key none Messages are authenticated and tamper-proof, but readable
authPriv yes yes (for example AES) Authenticated and encrypted; use this one

The message types in v2 and v3 are GetRequest (read one value), GetNextRequest (read the next one, used to walk the MIB), GetBulkRequest, SetRequest (write), Response, Trap (unacknowledged alert from the agent) and InformRequest (an alert that the manager must acknowledge, so it is more reliable than a trap). Limitations are that UDP gives no delivery guarantee, polling large networks creates traffic, and SNMP is complex to configure securely.

Syslog: collecting logs

Every device produces log lines: logins, interface changes, firewall drops. Syslog (RFC 5424) is the standard way to send them to a central server so they are searchable in one place and survive when a device is wiped. Classic syslog uses UDP port 514 (lossy, so messages can silently disappear); TCP and TLS variants (port 6514) are more reliable.

Each message starts with a priority value in angle brackets: PRI = facility * 8 + severity. The facility says which part of the system produced it (kern, auth, mail, local0 to local7 for your own applications), and the severity ranges from 0 emerg down to 7 debug. You decoded some in the program above: <34> is facility 4 (auth), severity 2 (crit). The rest of the message has a timestamp, a host name, an application name and free text. Filtering by severity and facility on the collector (rsyslog, syslog-ng, or modern log pipelines) keeps noise down and routes alerts to the right people. Consistent time stamps depend on NTP, which is why these protocols are usually deployed together.

SMB, TFTP and NNTP: more file and message protocols

Three more protocols are worth knowing, one you will meet on every office network, one that boots devices, and one that shows how an old Internet service worked.

SMB: file and printer sharing

SMB (Server Message Block, also called CIFS in its old form) is the protocol behind Windows file shares. When you open \\fileserver\accounting in Explorer, or mount a share on macOS or Linux, you are using SMB. The server exposes shares (folders and printers), the client authenticates (NTLM or Kerberos), and then reads, writes, locks and lists files as if they were on a local disk. On Linux, the Samba project implements both sides. The competing Unix protocol is NFS.

  • SMB runs on TCP port 445. The older NetBIOS transport used ports 137 to 139 and survives only for legacy systems.
  • SMB 1 is obsolete and dangerous: the 2017 WannaCry ransomware spread through an SMB 1 flaw. Disable it. Current dialects are SMB 2.x and SMB 3.x, which add pipelining of requests, signing (to stop tampering), encryption (AES), multiple channels per session and, in recent Windows Server editions, SMB over QUIC.
  • Never expose port 445 to the Internet. Keep file sharing inside the network or behind a VPN.

TFTP: the minimal file transfer

TFTP (Trivial File Transfer Protocol, UDP port 69) is FTP stripped to the bone: no login, no directory listings, no encryption. It can only read or write one named file. Its size is its strength. It is simple enough to be built into a network card's firmware, which is why it is used to boot diskless machines over the network (PXE), to load firmware onto routers and phones, and to back up device configurations on a trusted management network.

Because it runs over UDP, TFTP must provide its own reliability, and it does it in the simplest way: stop and wait. A file is cut into 512-byte blocks numbered from 1. The sender sends one DATA block and waits for an ACK with the same number before sending the next. A block shorter than 512 bytes marks the end. If nothing arrives before a timer expires, the sender repeats the last packet. The packet types are RRQ (1, read request), WRQ (2), DATA (3), ACK (4) and ERROR (5). The server answers from a new, random port, which is called the transfer ID, so port 69 is free for the next request.

Python A TFTP download with packet loss

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

TFTP's stop-and-wait design is slow on long links, since only one block is in flight at a time, and it has no security at all. Anyone who can reach the server can read its files, so restrict it to a management VLAN and serve only a directory of harmless files.

NNTP: network news

NNTP (Network News Transfer Protocol, TCP port 119, or 563 with TLS) carries Usenet, one of the oldest discussion systems on the Internet, in which people post articles to hierarchical newsgroups such as comp.lang.python. News servers copy articles to each other, and readers connect to one server to browse groups and read or post. Usenet has largely been replaced by web forums and social media, but NNTP is still used by some communities and mailing-list gateways, and it is a nice example of the text-based request and numbered reply style shared by SMTP, FTP and early HTTP. The client sends a command line; the server answers with a three-digit status code, and a multi-line response ends with a line that contains only a dot.

Python A tiny NNTP session

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The status codes follow a pattern you also see elsewhere: 2xx means success, 4xx means a failure of this particular request, and 5xx means a command the server does not understand or will not do. A real server also supports LIST (show groups), POST, OVER (fast article summaries) and STARTTLS.

Older file and print protocols

A few protocols from earlier generations still appear in exams and in old networks.

  • FTP details. FTP has data types: ASCII (text, with line endings converted), image/binary, and the old EBCDIC type for IBM mainframes. In a command-line client, get downloads one file and mget *.txt downloads several files that match a pattern, while put and mput upload. Forgetting to switch to binary mode corrupts images and archives.
  • Novell NetWare. In the 1980s and 1990s NetWare was a leading network operating system for file and print sharing on LANs. It used its own protocol suite: IPX (network layer), SPX (transport) and the NetWare Core Protocol (NCP) for file, print and directory requests. Its directory service, NDS (later eDirectory), organized users and resources in a tree, much like the LDAP directories used today. TCP/IP and Windows file sharing replaced it.
  • AFP (Apple Filing Protocol). The file-sharing protocol of classic Mac OS and macOS before SMB took over. It handled Mac-specific features such as resource forks and ran over TCP port 548. Apple deprecated it and uses SMB today.
  • LPD (Line Printer Daemon). A simple protocol for sending print jobs to a printer or print server over TCP port 515. It has no encryption, and modern networks use IPP (Internet Printing Protocol) instead.
  • SMB dialects. SMB has gone through versions called dialects: the old SMB 1 (CIFS), then SMB 2.0/2.1 (fewer commands, pipelining), SMB 3.0, 3.0.2 and 3.1.1 (encryption, multichannel, pre-authentication integrity). Client and server negotiate the highest dialect both support, which is why turning off SMB 1 on a server is safe for modern clients. Samba is the open-source implementation of SMB for Linux and Unix: you install its package, describe each share in the smb.conf file, add users with smbpasswd, and restart the service, after which Windows machines can use the Linux server.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In FTP passive mode, which side opens the data connection?

    In passive mode the client makes every connection, which works through NAT and client-side firewalls. In active mode the server connects back.
  2. 2. A PASV reply ends with (192,0,2,10,195,80). What is the data port?

    The port is p1 * 256 + p2 = 195 * 256 + 80 = 50000.
  3. 3. What is SFTP?

    SFTP is the SSH File Transfer Protocol. FTPS is the one that is FTP with TLS. SFTP needs only the one SSH port.
  4. 4. An NTP client has T1 = 10.000, T2 = 10.150, T3 = 10.151, T4 = 10.061. What is its offset?

    offset = ((T2 - T1) + (T3 - T4)) / 2 = ((0.150) + (0.090)) / 2 = 0.120 s. The client clock is 120 ms behind the server.
  5. 5. A syslog message starts with <165>. What are its facility and severity?

    165 = 20 * 8 + 5, so facility 20 (local4) and severity 5 (notice).

Summary

  • FTP uses a control connection on port 21 plus a separate data connection per transfer. Active mode makes the server connect back (bad for NAT); passive mode makes the client connect out, with the port given as p1 * 256 + p2.
  • FTP sends everything in clear text. Use SFTP (inside SSH, one port) or FTPS (FTP plus TLS) instead. TFTP is a tiny UDP protocol used for network booting after DHCP.
  • Telnet is unencrypted; SSH replaced it with key exchange, host-key checking (fingerprints, trust on first use), public-key login and tunnels.
  • NTP synchronizes clocks over UDP 123 using four timestamps: offset = ((T2 - T1) + (T3 - T4)) / 2, delay = (T4 - T1) - (T3 - T2). Asymmetric paths limit accuracy.
  • SNMP lets managers query and set values (OIDs in a MIB) on agents over UDP 161/162; use v3 for security.
  • Syslog centralizes logs, and its PRI value packs facility and severity as facility * 8 + severity.
  • SMB (TCP 445) shares files and printers on Windows networks; disable SMB 1 and never expose it to the Internet. TFTP (UDP 69) moves one file in 512-byte stop-and-wait blocks for booting and firmware. NNTP (TCP 119) carries Usenet with text commands and numbered replies.

Next, move from wired services to the radio world in Wi-Fi and 802.11.