SSH Commands Cheat Sheet: Connect, Copy Files and Port Forwarding
SSH, short for Secure Shell, is how most developers connect to a remote server, deploy code, check logs, and move files without exposing passwords in plain text. Once you learn a small set of commands, you can do almost all daily server work from your own terminal. This ssh commands cheat sheet gathers the patterns you will actually reuse: basic connections, key-based login, copying files with scp and rsync, interactive transfers with sftp, and port forwarding for databases and internal dashboards.
You do not need to memorise every flag. Keep the structure in mind instead. Most commands follow the same shape: the tool name, a few options, a user and host, and then a path or port. If you work through the examples below in order, you will cover the full workflow from first login to a tidy, repeatable setup. If you also spend time in the terminal for other jobs, pair this page with our guide to linux commands for developers so file and process commands feel just as familiar.
Connecting to a Server for the First Time
The simplest SSH connection needs only a username and a hostname or IP address. SSH uses port 22 by default, encrypts the whole session, and asks you to confirm the server fingerprint the first time you connect. That fingerprint check matters. It protects you from connecting to a different machine pretending to be your server.
ssh username@server.example.com ssh username@192.0.2.10 ssh -p 2222 username@server.example.com
The first line connects as username to the named host on the default port. The second line does the same thing with an IP address instead of a hostname, which is handy for new servers before DNS is set up. The third line adds the -p flag to use a custom port, 2222 in this case. Many hosts move SSH to a non-standard port, so check your provider panel if a normal connection is refused. After a successful login, commands like whoami, hostname, and pwd confirm which machine and account you are on before you change anything.
Adding -v turns on verbose output when a connection fails. You will see which key files were tried, which port was contacted, and where authentication stopped. A second -v or -vv adds even more detail. Verbose mode is the fastest way to tell whether a problem is DNS, a closed port, a rejected key, or a wrong username.
Key-Based Login With ssh-keygen and ssh-copy-id
Typing a password on every login gets old, and password login is also easier to attack. A key pair solves both problems. You keep a private key on your own machine and place the matching public key on the server. The server then lets you in when you can prove you hold the private key, without sending a password across the network.
ssh-keygen -t ed25519 -C "you@example.com" ssh-copy-id username@server.example.com ssh username@server.example.com
The first command creates a new Ed25519 key pair, saving the private key as ~/.ssh/id_ed25519 and the public key as ~/.ssh/id_ed25519.pub by default. The -C flag adds a comment, usually your email, so you can recognise the key later. Use a strong passphrase when prompted for extra protection. The second command, ssh-copy-id, appends your public key to the server file ~/.ssh/authorized_keys after one final password login. The third command should now log you in with the key. On Windows or on systems without ssh-copy-id, you can copy the public key text manually and paste it into authorized_keys on the server.
Protect the private key. Never share it, never commit it to Git, and keep its permissions restricted. On Linux and macOS, chmod 600 ~/.ssh/id_ed25519 is the usual setting, and the .ssh folder itself is normally chmod 700. If permissions are too open, SSH will refuse to use the key and will tell you in the error message.
Using an SSH Config File
When you manage several servers, remembering usernames, ports, and key files for each one is error-prone. The config file at ~/.ssh/config lets you give each host a short nickname and store the details once. After that, a single short command opens the connection.
Host production
HostName server.example.com
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
Host staging
HostName staging.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519_staging
Each Host block starts with the nickname you will type, such as production. HostName is the real address, User is the login name, and Port is included only when it is not 22. IdentityFile points to the private key for that host, which is useful when different servers use different keys. With this file saved, ssh production replaces the long command, and tools like scp and rsync understand the same nicknames. Developers who switch between branches and servers all day often combine this with the workflow in our git branching workflow notes to keep deploys predictable.
Running a Single Command Without a Full Session
Sometimes you only need one piece of information, such as disk space or the last few log lines. You can pass a command at the end of the ssh call and SSH will run it, print the output, and disconnect. This is perfect for quick health checks and for small scripts.
ssh production "df -h" ssh production "tail -n 50 /var/log/app.log" ssh production "systemctl status nginx --no-pager"
The quoted text is executed on the remote machine, not locally. The first example reports disk usage in human-readable sizes. The second shows the last 50 lines of an application log, which is often enough to spot a recent error. The third checks the status of the nginx service without opening an interactive pager. Quote carefully: variables inside double quotes may be expanded by your local shell first, so use single quotes around the remote command when it contains dollar signs you want evaluated on the server.
Copying Files With scp
The scp command copies files over the same encrypted connection. Its syntax mirrors the familiar cp command: source first, destination second. The remote side is written as user@host:path. A colon is what tells scp that one side is remote.
scp notes.txt username@server.example.com:/home/username/ scp username@server.example.com:/home/username/report.pdf ./downloads/ scp -r ./project username@server.example.com:/var/www/ scp -P 2222 file.zip username@server.example.com:/tmp/
The first line uploads a local file to the user home folder on the server. The second line reverses the direction and downloads report.pdf into a local downloads folder. The -r flag in the third line copies a whole directory recursively, including subfolders. The fourth line uses capital -P for a custom port; note that scp uses -P while ssh uses lowercase -p, a classic source of confusion. Paths after the colon are relative to the remote home directory unless they start with a slash, in which case they are absolute.
Syncing Folders With rsync Over SSH
For repeated uploads, rsync is better than scp. It compares source and destination, transfers only changed parts, and can resume an interrupted transfer. It runs happily over SSH and respects your config file nicknames, so it fits naturally into deploy routines alongside commands from our git basics commands reference.
rsync -avz ./site/ production:/var/www/site/ rsync -avz --dry-run ./site/ production:/var/www/site/ rsync -avz -e "ssh -p 2222" file.tar.gz username@server.example.com:/tmp/
The flags -a, -v, and -z mean archive mode, verbose output, and compression. Archive mode preserves permissions, timestamps, and folder structure, which is what you want for website files. Notice the trailing slash on ./site/ in the first line: with the slash, rsync copies the contents of the folder; without it, it copies the folder itself into the destination. The –dry-run option in the second line lists what would change without moving any data, an excellent safety check before a big sync. The third line shows how to pass a custom SSH port with -e when you are not using a config nickname. Add –delete only when you truly want files removed from the destination to mirror the source, and test it with a dry run first.
Interactive Transfers With sftp
The sftp tool gives you an interactive prompt for browsing remote folders and moving files one by one. It feels like a small FTP client, but the connection is encrypted like SSH. It suits one-off transfers when you are not sure of exact remote paths.
sftp username@server.example.com sftp> ls sftp> cd /var/backups sftp> get backup-2026-11-01.tar.gz sftp> put local-notes.txt sftp> exit
After connecting, ls lists the remote folder and cd changes it, while your local folder is controlled with lls and lcd, where the extra l means local. The get command downloads a file and put uploads one. Tab completion often works for remote paths, which saves typing. When you finish, exit closes the session. For scripted transfers, scp and rsync remain easier to automate, but sftp is friendly for careful manual work.
Local Port Forwarding for Databases and Dashboards
Port forwarding is the feature that surprises people most. It tunnels a connection through SSH so a service that only listens on the server becomes reachable on your own machine. Database servers, admin panels, and internal tools often bind to localhost on the server for safety. A local forward lets you reach them without exposing them to the public internet.
ssh -L 3307:localhost:3306 production ssh -L 8080:localhost:80 production ssh -N -L 5433:localhost:5432 production
In -L 3307:localhost:3306, the first number is the port opened on your machine, the middle localhost is the target as seen from the server, and 3306 is the target port, here the standard MySQL port. After running the first command, connecting your database client to localhost port 3307 actually talks to MySQL on the server. The second example forwards a web service so http://localhost:8080 in your browser shows the site running on the server. The -N flag in the third line says do not open a remote shell, just forward ports, which is tidy when the tunnel is all you need. Keep the terminal open while you use the tunnel, and close it when you are done.
Remote and Dynamic Forwarding
Remote forwarding works in the opposite direction. With -R, a port on the server forwards back to a service on your machine. Teams use it to show a local development site to a colleague through a shared server, or to reach a device behind a firewall that can dial out but cannot accept connections. Use it carefully, because it can expose a local service more widely than you expect.
ssh -R 9000:localhost:3000 production ssh -D 1080 production
The first command makes your local app on port 3000 reachable on the server at port 9000, subject to the server setting GatewayPorts and firewall rules. The second command starts a dynamic SOCKS proxy on local port 1080. Applications configured to use that proxy route their traffic through the server, appearing to come from the server network. Dynamic forwarding is useful for reaching internal web tools from outside the office network, provided your organisation allows it. Always follow your team policy for tunnels and proxies, and close forwards you no longer need.
Keeping Sessions Alive and Troubleshooting
Long connections sometimes drop when a network or firewall decides they are idle. Adding keepalive options to your config helps. ServerAliveInterval 60 tells SSH to send a small check every 60 seconds, and ServerAliveCountMax 3 ends the connection only after three missed checks. For sessions that must survive a laptop sleep or network switch, developers often run work inside tmux or screen on the server, so the program keeps running even if SSH disconnects, and they simply reattach later.
Most connection problems fall into a few buckets. Permission denied (publickey) usually means the public key is not in authorized_keys, the private key path is wrong, or file permissions are too open. Connection timed out points to a wrong address, a stopped server, or a firewall blocking the port. Connection refused often means SSH is not running on the server or you used the wrong port. Host key verification failed appears when a server was rebuilt or its key changed; verify the new fingerprint through your provider console before removing the old line with ssh-keygen -R hostname. Work through verbose output top to bottom, fix the first real error, and test again. That calm order will solve nearly every issue in this ssh commands cheat sheet, and pairing it with habits from git undo changes keeps recovery work just as methodical.
Conclusion
SSH rewards a small amount of setup. Learn plain connections first, add key login and a config file, then use scp for quick copies, rsync for repeated syncs, sftp for browsing, and port forwarding for private services. Keep your private key safe, verify fingerprints, and test risky transfers with a dry run. With those habits, this cheat sheet covers the remote work most developers do every week.
Tagged: linux remote access port forwarding scp command ssh cheat sheet ssh commands ssh keys



One thought on “SSH Commands Cheat Sheet: Connect, Copy Files and Port Forwarding”