Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. ssh / scp / rsync

ssh / scp / rsync

Since: ssh / scp All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)
rsync All Linux
macOS(2005 Tiger)
Bash 1.0(1989)

ssh, scp, and rsync are commands for connecting to remote hosts, transferring files, and syncing directories. They are essential tools for server management and deployment workflows.

Syntax

Connect to a remote host via SSH.

ssh username@hostname

Connect using a specific port.

ssh -p port username@hostname

Run a remote command and exit.

ssh username@hostname "command"

Copy a file from local to remote (scp).

scp localfile username@hostname:remotepath

Copy a file from remote to local (scp).

scp username@hostname:remotefile localpath

Sync from local to remote (rsync).

rsync -avz localdir/ username@hostname:remotedir/

Commands and Options

Command / OptionDescription
ssh user@hostConnects to the specified host via SSH.
ssh -p portSpecifies the connection port (default is 22).
ssh -i keyfileSpecifies the private key file to use.
ssh -L localport:dest:portSets up local SSH port forwarding.
scp file user@host:pathCopies a file over SSH.
scp -r directoryRecursively copies a directory.
scp -P portSpecifies the port for scp (uppercase P).
rsync -aSyncs in archive mode, preserving permissions and timestamps.
rsync -vSyncs with verbose output.
rsync -zCompresses data during transfer.
rsync --deleteDeletes files at the destination that no longer exist at the source.
rsync --dry-runSimulates the transfer without making any changes.
rsync -e "ssh -p port"Specifies the SSH port used by rsync.

Sample Code

The following examples use this SSH connection configuration.

~/.ssh/config
Host myserver
    HostName 192.168.1.100
    User deploy
    Port 2222
    IdentityFile ~/.ssh/id_rsa

Connect via SSH using an alias. Defining entries in ~/.ssh/config keeps your commands short and simple.

ssh myserver

Run a command on the remote host and retrieve the output locally.

ssh myserver "df -h /"
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   23G   25G  48% /

Upload a local file to the server using scp.

scp -P 2222 ./config.json deploy@192.168.1.100:/var/www/app/

Recursively copy a directory.

scp -r ./dist/ deploy@192.168.1.100:/var/www/html/

Deploy with rsync. It is good practice to run --dry-run first to preview the changes before applying them.

rsync -avz --delete --dry-run ./dist/ deploy@192.168.1.100:/var/www/html/
sending incremental file list
./
index.html
css/style.min.css
js/common.min.js
deleting old-page.html

sent 234 bytes  received 45 bytes  186.00 bytes/sec
total size is 1,234,567  speedup is 4,426.69 (DRY RUN)

If the dry-run output looks correct, remove --dry-run to perform the actual sync.

rsync -avz --delete ./dist/ deploy@192.168.1.100:/var/www/html/
sending incremental file list
./
index.html
css/style.min.css
js/common.min.js

sent 45,678 bytes  received 92 bytes  18,308.00 bytes/sec
total size is 1,234,567  speedup is 26.99

Sync a backup into a date-stamped directory.

sample_backup_sync.sh
date_dir=$(date +%Y%m%d)
rsync -az /var/www/html/ /backup/$date_dir/

Run the following command:

bash backup_sync.sh

Port forwarding is a mechanism that allows you to securely access ports on another server through an encrypted SSH tunnel.

Use SSH port forwarding to tunnel local port 8080 to port 80 on the remote host.

ssh -L 8080:localhost:80 myserver

Common Mistakes

Common Mistake 1: rsync --delete without --dry-run deletes files unintentionally

The --delete flag removes files at the destination that do not exist at the source. Running it without previewing first can permanently delete files on the server.

rsync -avz --delete ./dist/ deploy@server:/var/www/html/
(files present on server but absent from ./dist/ are deleted immediately)

Always run with --dry-run first to preview what will be deleted.

rsync -avz --delete --dry-run ./dist/ deploy@server:/var/www/html/
rsync -avz --delete ./dist/ deploy@server:/var/www/html/

Common Mistake 2: Missing trailing slash on rsync source changes behavior

With rsync, a trailing slash on the source means "sync the contents of this directory." Without it, the directory itself is copied as a subdirectory of the destination.

rsync -av ./dist  deploy@server:/var/www/html/
rsync -av ./dist/ deploy@server:/var/www/html/

The first command creates /var/www/html/dist/ at the destination. The second syncs the files directly into /var/www/html/. Check the intended destination carefully before running.

Notes

For deployments, rsync -avz --delete is the standard choice. The --delete flag removes files at the destination that have been deleted at the source. Always verify with --dry-run before applying changes to production.

Storing your SSH connection details in ~/.ssh/config simplifies your commands and makes them easier to manage. Make sure your private key has the correct permissions (chmod 600).

If you find any errors or copyright issues, please .