SSH Command in Linux with examples

SSH and file transfer commands in Linux

Log in to servers, copy files between machines and set up passwordless keys.

#ssh

Log in to a server

Syntaxssh [options] user@host
  • ssh deploy@203.0.113.10

    Log in as deploy

  • ssh -p 2222 deploy@example.com

    Use a different port

  • ssh -i ~/.ssh/id_ed25519 deploy@example.com

    Use a specific key

  • ssh deploy@example.com "df -h"

    Run one command and come back

  • ssh -J jump@bastion.example.com deploy@10.0.0.5

    Go through a jump host

TipSSH Command does all of this with a Connect form. Type a Jump host in the dialog instead of using -J.

#ssh-keygen

Make a key pair

Syntaxssh-keygen [options]
  • ssh-keygen -t ed25519 -C "me@example.com"

    Create a modern key (recommended)

  • ssh-keygen -lf ~/.ssh/id_ed25519.pub

    Show a key's fingerprint

  • ssh-keygen -R example.com

    Forget a server's saved fingerprint

#ssh-copy-id

Install your key on a server

Syntaxssh-copy-id user@host
  • ssh-copy-id deploy@example.com

    Copy your public key so you stop typing a password

#ssh-agent / ssh-add

Remember key passphrases

Syntaxssh-add [key]
  • eval "$(ssh-agent -s)"

    Start the agent

  • ssh-add ~/.ssh/id_ed25519

    Unlock the key once per session

  • ssh-add -l

    List loaded keys

#scp

Copy files over SSH

Syntaxscp [options] source destination
  • scp report.pdf deploy@example.com:/home/deploy/

    Upload a file

  • scp deploy@example.com:/var/log/app.log .

    Download a file to the current folder

  • scp -r site/ deploy@example.com:/var/www/

    Copy a whole folder

  • scp -P 2222 file deploy@example.com:~

    A different port (capital P)

#sftp

Interactive file transfer

Syntaxsftp user@host
  • sftp deploy@example.com

    Open a session

  • put local.txt

    Upload (inside sftp)

  • get remote.txt

    Download (inside sftp)

TipInside sftp, ls, cd and pwd work on the server; add an l (lls, lcd, lpwd) for your own computer.

#rsync

Smart copying and syncing

Syntaxrsync [options] source destination
  • rsync -avz site/ deploy@example.com:/var/www/site/

    Upload only what changed, compressed

  • rsync -avz --progress deploy@example.com:/backups/ ./backups/

    Download with progress

  • rsync -avz --delete src/ dest/

    Make dest an exact mirror (deletes extra files)

  • rsync -avzn src/ dest/

    Dry run: show what would happen

CarefulA trailing slash matters. src/ copies the contents of src; src copies the folder itself.

#ssh tunnels

Forward ports

Syntaxssh -L / -R / -D
  • ssh -L 5432:127.0.0.1:5432 deploy@example.com

    Reach the server's database at localhost:5432

  • ssh -R 8080:localhost:3000 deploy@example.com

    Show your local site on the server

  • ssh -D 1080 deploy@example.com

    Make a SOCKS proxy at localhost:1080

#~/.ssh/config

Shortcuts for servers

SyntaxHost name
  • Host web

    A line in ~/.ssh/config

  • HostName 203.0.113.10

    Then add the address below it

  • User deploy

    and the user; now just type: ssh web

#sshfs

Mount a remote folder

Syntaxsshfs user@host:path mountpoint
  • mkdir -p ~/remote

    Create an empty folder to use as the mount point

  • sshfs deploy@example.com:/var/www ~/remote

    Work on server files as if local

  • fusermount -u ~/remote

    Unmount

Run these commands with SSH Command

SSH Command is a desktop app for Mac, Windows and Linux that puts a real terminal, SFTP file panes, a sudo switch and a searchable Linux command finder in one window. Type what you want to do, and send the command to your server with one click.

Download free Windows commands with examples SFTP & FTP client