Email and Ports

How to send and receive information over a network.

Ports

To connect computers to each other, you need a socket and a port. A socket connects at ports and allows data to transfer.

Some default privileged ports (only connectable by root-owned processes):

  • 21: FTP
  • 22: SSH
  • 80: HTTP
  • 443: HTTPS

Privileged ports are less than 1024. Unprivileged ports are greater than 1024 and can be managed by any user. For example, most database servers run on an unprivileged port under their own user.

  • 3306: MariaDB aka MySQL (user: mariadb)
  • 5432: PostgreSQL Database (user: postgres)
  • 27017: MongoDB (user: mongodb)

Protocols

Communicating over a port is done with a protocol. For example, to interact with port 80, you use HyperText Transfer Protocol http://. To interact with a PostgreSQL Database, you use psql:///.

Email

  • The default port for email is 25. The protocol is SMTP, which stands for “Simple Mail Transfer Protocol.”
  • The default port for a mail client to access mail is 993. The protocol is IMAP, which stands for “Internet Message Access Protocol.”
  • The default port for downloading emails from a server to a local device (e.g. to your phone) is 995. The protocol is POP3, which stands for Post Office Protocol 3.

NOTE: Port 25 does not encrypt data. Typically, ports 465 or 587 are used for secure mail transfer, and connections will reroute to those ports if available. Messages sent from port 25 unencrypted will typically be marked as spam.

Setup (already done on the class server)

  1. Install a MTA (Mail Transfer Agent) called postfix. The process attaches to port 25. It runs as a daemon (background, long-running process).
sudo dnf install postfix
sudo systemctl enable postfix --now

postfix is now running with a PID of 771610 (main process) on port 25.

[dbuckley@localhost ~]$ sudo lsof -i :25
COMMAND    PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
master  771610 root   13u  IPv4 3872838      0t0  TCP localhost:smtp (LISTEN)
master  771610 root   14u  IPv6 3872839      0t0  TCP localhost:smtp (LISTEN)
[dbuckley@localhost ~]$ ps aux | grep postfix
root      771610  0.0  0.1  38188  4536 ?        Ss   13:20   0:00 /usr/libexec/postfix/master -w
postfix   771612  0.0  0.2  47368  8840 ?        S    13:20   0:00 qmgr -l -t unix -u
postfix   771616  0.0  0.3  48884 11716 ?        S    13:20   0:00 tlsmgr -l -t unix -u
postfix   772533  0.0  0.2  47324  8792 ?        S    14:59   0:00 pickup -l -t unix -u
dbuckley  772959  0.0  0.0 221672  2492 pts/0    S+   15:22   0:00 grep --color=auto postfix
  1. Install a mail client called mutt. When run, it uses the IMAP and POP3 protocols to interact with an inbox.
sudo dnf install mutt
  1. Install a CLI called swaks to test email sending.
curl -LO https://www.jetmore.org/john/code/swaks/files/swaks-20240103.0.tar.gz
tar -xzf swaks-20240103.0.tar.gz
cd swaks-20240103.0
sudo install -m 0755 swaks /usr/local/bin/swaks

Send and Receive Mail!

SWAKS

Send your first email via the command line!

# TODO: set these variables 
sender=your_username_here
receiver=their_username_here

# you may modify the subject and body
swaks --helo localhost --to $receiver --from $sender@localhost --server localhost --header "Subject: Test email" --body "Hello, this is a test."

Using mutt as a mail client

mutt -f /var/mail/$(whoami)

By default, mutt uses vi (or vim) keybindings for composing emails.

i    = Enter insert mode (typing mode)
ESC  = Exit insert mode
h    = Left
j    = Down
k    = Up
l    = Right
:q   = Quit without saving
:x   = Save and quit
:w   = Write (save)

More vi keybindings.

Extras

  1. Explore the /var/mail directory. What is it linked to? How could you scroll through all your mail?
  2. What is the output of ls -l /var/spool/mail? Which group has rw access to all mail?
  3. Here’s a great resource to check out more about swaks.