Sub topic 10
Remote access: SSH, Telnet and RDP
This chapter assumes you know what a port is, the difference between TCP and UDP, and how a client connects to a listening server socket. It also assumes you can run commands in a terminal and read short Python scripts. The chapter covers the three most common remote access protocols, their default ports, how they differ in security, and how to check and reason about them on a real system.
What remote access protocols do
A remote access protocol lets you operate a computer you are not sitting in front of. The server listens on a well-known port. The client connects, proves who the user is, and then sends input and receives output.
There are two broad styles:
- Terminal access gives you a text shell. Keystrokes go to the server, and text comes back. SSH and Telnet work this way.
- Graphical access gives you the remote desktop: screen images, mouse movement and keyboard events. RDP works this way.
| Protocol | Default port | Transport | Encrypted | Typical use |
|---|---|---|---|---|
| Telnet | 23 | TCP | No | Legacy devices, lab testing |
| SSH | 22 | TCP | Yes | Server administration, tunnels, file copy |
| RDP | 3389 | TCP and UDP | Yes (varies by configuration) | Windows remote desktop |
All three are in the ranges you already know. Telnet and SSH sit in the well-known range (0 to 1023), and RDP's 3389 sits in the registered range (1024 to 49151).
Telnet on port 23
Telnet is one of the oldest application protocols on the internet. It opens a TCP connection to port 23 and carries a stream of characters in both directions. The server runs a login prompt, and the client shows it to you.
Telnet has no encryption. Your username, your password and every command travel as readable bytes. Anyone who can capture traffic between the two hosts, for example on a shared Wi-Fi network or a compromised router, can read them.
Because of this, Telnet is no longer a good choice for logging in to systems. You still meet it in two situations:
- Old network equipment or embedded devices that offer nothing else.
- Quick tests. Telnet clients are often used to open a raw TCP connection to any port and type protocol text by hand.
The second use does not depend on port 23. The connection is just a TCP socket, so the same technique works against port 25 (SMTP) or port 80 (HTTP). The third code example in the topic plan does the same thing in Python: it sends a minimal HTTP request as plain bytes over a TCP connection.
import socket
request = (
"HEAD / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Connection: close\r\n\r\n"
).encode("ascii")
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request)
print(s.recv(1024).decode("latin-1"))A protocol is just agreed-upon bytes on a port. Telnet shows this most clearly, and it also shows the cost of sending those bytes without protection.
SSH on port 22
SSH (Secure Shell) is the standard replacement for Telnet. It also uses TCP, on port 22. Unlike Telnet, it encrypts the whole session, including the login.
An SSH connection goes through these stages:
- The TCP connection opens to port 22.
- Client and server agree on protocol version and encryption algorithms.
- The server proves its identity with a host key. The client compares it with the key it saw earlier.
- The user authenticates, by password or, preferably, by a public key.
- The encrypted channel carries a shell or other services.
Host keys and the first connection
The first time you connect to a server, your client has no record of its host key. It shows a fingerprint and asks whether to trust it. If you accept, the client stores the key in ~/.ssh/known_hosts. On later connections, the client compares the key it receives with the stored one.
If the key changes, the client prints a loud warning. A changed key can mean the server was reinstalled, or it can mean someone is intercepting the connection. Do not ignore the warning without checking which one it is.
More than a shell
SSH is a general encrypted channel. Several other tools reuse port 22:
scpand SFTP copy files over the same connection. SFTP is covered in the file transfer chapter.- Port forwarding sends other TCP traffic through the encrypted tunnel.
- Git over SSH uses port 22 for repository access.
A local forward looks like this:
# Reach a database on db.internal:5432 through a jump host,
# by listening on local port 15432
ssh -N -L 15432:db.internal:5432 [email protected]While this runs, a program connecting to localhost:15432 reaches db.internal:5432, and the traffic between your machine and the bastion is encrypted. The -N option tells SSH not to start a shell.
Connecting from the command line
# Default port 22
ssh [email protected]
# A server listening on a different port
ssh -p 2222 [email protected]The -p option is needed whenever the server does not use port 22. Many administrators move SSH to another port to reduce automated login attempts in logs. That reduces noise, but it does not replace real authentication. Chapter content on non-standard ports covers why.
Hardening basics
Internet-facing SSH servers receive constant automated login attempts. Common practice is:
- Use public key authentication and disable password login.
- Disable direct root login.
- Limit which users may connect.
- Restrict the source addresses that can reach port 22 in the firewall.
- Use a tool that blocks repeated failures, such as fail2ban.
RDP on port 3389
RDP (Remote Desktop Protocol) is Microsoft's protocol for remote graphical sessions. A Windows server listens on port 3389. Modern clients use TCP on 3389 and can also use UDP on the same port number to improve responsiveness on lossy links.
RDP carries more than the screen. It can redirect the clipboard, printers, drives, audio and USB devices between the client and the remote machine. That convenience is also a risk, because a compromised remote host may try to reach those redirected resources.
RDP encrypts its traffic, and current versions can use TLS and Network Level Authentication (NLA). NLA requires the user to authenticate before the server builds a full desktop session, which reduces the load an unauthenticated attacker can place on the server.
Why exposed RDP is dangerous
Port 3389 is among the most scanned ports on the internet. Servers with RDP open to the world face password guessing, and software flaws in RDP have led to serious attacks. Do not expose 3389 directly to the internet. Instead:
- Place RDP behind a VPN or a remote desktop gateway.
- Require NLA and strong, unique passwords with multi-factor authentication.
- Restrict allowed source addresses.
- Keep the operating system patched.
Non-Windows clients and servers
RDP is not limited to Windows clients. Clients exist for Linux and macOS, and some Linux desktops include RDP servers. Linux systems more often use SSH for administration, with VNC or RDP only when a graphical desktop is needed. VNC commonly starts at TCP port 5900.
Comparing the three
| Question | Telnet | SSH | RDP |
|---|---|---|---|
| Is the login protected? | No | Yes | Yes, with NLA and TLS |
| What do you get? | Text shell | Text shell, tunnels, file copy | Full desktop |
| Typical server OS | Network gear, old Unix | Linux, Unix, network gear, Windows | Windows |
| Should it face the internet? | No | Only when hardened | No, use a VPN or gateway |
Checking remote access ports
You can see which remote access services a host is running, and test whether they are reachable, with the commands from the inspection chapter:
# On the server: is anything listening on 22, 23 or 3389?
ss -tulnp | grep -E ':(22|23|3389)\b'
# From another machine: does the TCP port accept connections?
nc -vz 192.0.2.10 22A successful nc -vz means the TCP handshake completed. It does not prove the service behind the port is SSH. To check, read the first bytes the server sends. An SSH server starts the conversation with a banner such as SSH-2.0-OpenSSH_9.6.
import socket
def read_banner(host, port, timeout=5):
with socket.create_connection((host, port), timeout=timeout) as s:
return s.recv(256).decode("latin-1").strip()
print(read_banner("192.0.2.10", 22))Use this only against hosts you own or are authorized to test. Replace 192.0.2.10 with your own address, since that address is reserved for documentation and will not answer.
Firewall reasoning for remote access
A firewall rule for remote access should name the port, the protocol and the allowed sources. For an SSH server that only the office network needs to reach:
- Allow TCP port 22 from the office address range.
- Deny TCP port 22 from everywhere else.
- Block port 23 entirely, because nothing should use Telnet.
- Allow RDP only from the VPN's address range, never from the whole internet.
Allowing "everything from anywhere to port 22" is the usual mistake. Limiting the source is often more effective than changing the port.
Common errors
- Connection refused: the host is reachable but nothing listens on that port, or the service is stopped. Check with
sson the server. - Connection timed out: packets are dropped, usually by a firewall or a wrong address. Check the firewall rules and cloud security groups.
- Host key verification failed: the server's key differs from the stored one. Confirm the reason before removing the old entry from
known_hosts. - Permission denied: the network path works, but authentication failed. Check the username, key file permissions and server settings.
- RDP connects, then closes: NLA or credential settings often do not match between client and server.
Exercise
A colleague says, "We moved our SSH server to port 2222, so it is safe from scanners and we can allow it from anywhere." Name two things wrong with this reasoning, and give the command a user needs to connect as alice to host 192.0.2.10.
Show answer
First, changing the port only hides the service from the simplest scans. Scanners probe many ports and can identify SSH from its banner, so the server still needs strong authentication (public keys, no root login). Second, allowing the port from anywhere removes the most effective control, which is limiting the source addresses in the firewall. The connection command is ssh -p 2222 [email protected].
Exercise
nc -vz 192.0.2.10 3389 reports that the connection succeeded. What does that prove, and what does it not prove? Which similar protocol uses the same port number over UDP?
Show answer
It proves that a TCP handshake to port 3389 completed, so the host is reachable and something is listening there. It does not prove that RDP is the service, that the user can log in, or that NLA is configured. RDP can also use UDP on port 3389.
Exercise
Why is Telnet unsuitable for logging in over an untrusted network, and for what purpose is a Telnet client still useful?
Show answer
Telnet sends the username, password and all commands as unencrypted bytes, so anyone capturing the traffic can read them. A Telnet client is still useful for opening a raw TCP connection to any port and typing protocol text by hand to test a service, though tools like nc do this as well.