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.

Watch: SSH Keys for Beginners: ssh-keygen, ssh-copy-id & Config (Live Demo)

Read the video transcript

SSH keys are safer and faster than passwords. In this video, you'll check your SSH version, create a key pair, tell the public key from the private one, and set up a short name in the config file. All live, on a real Ubuntu server, with SSH Command. We're connected, with the Shell view open. ssh -V prints the version of the SSH client. It is the quickest way to confirm that SSH is installed. ssh-keygen makes a key pair. Dash t picks the type, and ed25519 is the modern choice. Dash f names the file, and dash N with empty quotes means no passphrase, only for this demo. Use a passphrase for real keys. That made two files. The one ending in .pub is the public key, and you can share it. The other is the private key. Never share it; notice its permissions allow only you. ssh-keygen dash l f shows a key's fingerprint, a short summary you can compare to be sure it is the right key. To use the key, you put the public half on the server with ssh-copy-id. After that, you can log in without a password. We won't change this server's keys here. A config file gives a connection a short name. Host is the nickname, HostName is the address, and User is the login. Then ssh web is all you type. And in SSH Command, the search bar lists these commands with their options. Type ssh-keygen, and copy one. That's SSH keys: ssh-keygen to create, ssh-copy-id to install, and the config file to save time. The full guide is at sshcommand.com, under ssh commands. SSH Command is free for personal use. The link is in the description. Thanks for watching.

#ssh-keygen

Make a key pair

Syntaxssh-keygen [options]
  • ssh-keygen -t ed25519 -C "[email protected]"

    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-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

#sftp

Interactive file transfer

Syntaxsftp user@host
  • 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/ [email protected]:/var/www/site/

    Upload only what changed, compressed

  • rsync -avz --progress [email protected]:/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/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 [email protected]:/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