Git & Terminal Cheatsheet

Opening a Port

Use this Git & Terminal reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Serve a Folder Instantly

python3 -m http.server 8080             # serve the current directory
python3 -m http.server 8080 --bind 127.0.0.1   # localhost only (safer)
npx serve .                             # Node alternative, SPA-friendly
php -S localhost:8080                   # PHP built-in server

Visit http://localhost:8080, press ctrl+c to stop. By default http.server binds all interfaces, so anyone on your network can reach it unless you --bind 127.0.0.1.

What Is Using a Port

The top port question in practice is "what is already ON this port". lsof answers it on both macOS and Linux:

lsof -i :8080               # process listening on 8080 (name, PID, user)
lsof -ti :8080              # PID only, script-friendly
lsof -iTCP -sTCP:LISTEN -n -P   # ALL listening TCP ports
ss -ltnp                    # Linux: listening TCP sockets with PIDs
netstat -anv | grep LISTEN  # macOS fallback

Free Up a Port

kill $(lsof -ti :8080)      # stop whatever is listening on 8080
kill -9 $(lsof -ti :8080)   # force it, if TERM was ignored
fuser -k 8080/tcp           # Linux one-liner

Listen on a Raw Port (netcat)

Useful for quick connectivity tests between two machines.

nc -l 8080          # LISTEN on 8080 (macOS/BSD and OpenBSD nc on most Linux)
nc -l -p 8080       # traditional/GNU netcat syntax (some Linux distros)
nc localhost 8080   # connect as a client, type to send
nc -zv host 8080    # just TEST whether a port is open (no data)
nc -zv host 20-25   # scan a small range

macOS/BSD netcat rejects -p together with -l (error: cannot use -p with -l). Use nc -l 8080 there, and nc -l -p 8080 only on traditional netcat builds.

Allow the Port Through a Firewall

On a Linux server the firewall may block the port until you open it:

sudo ufw allow 8080         # Ubuntu/Debian (ufw)
sudo ufw status             # list open ports
sudo firewall-cmd --add-port=8080/tcp --permanent   # RHEL/Fedora (firewalld)
sudo firewall-cmd --reload

Cloud VMs (AWS/GCP/Azure) ALSO filter at the security-group / firewall-rule level, opening the OS firewall alone is often not enough.

Tunnel a Port over SSH

ssh -L 8080:localhost:8080 user@server   # local 8080 -> server's 8080
ssh -L 5433:db-host:5432 user@bastion    # reach a private DB through a bastion
ssh -R 9090:localhost:3000 user@server   # reverse: expose YOUR 3000 on the server
ssh -N -f -L 8080:localhost:8080 user@server   # tunnel only, in the background

Quick Reference

TaskCommand
Quick HTTP serverpython3 -m http.server 8080
Who is on a portlsof -i :8080
Kill process on a portkill $(lsof -ti :8080)
All listening portslsof -iTCP -sTCP:LISTEN -n -P / ss -ltnp
Raw listenernc -l 8080
Test a remote portnc -zv host 8080
Open OS firewallsudo ufw allow 8080
SSH tunnelssh -L 8080:localhost:8080 user@host