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.

  1. Home
  2. Bash Dictionary
  3. ssh / scp / rsync

ssh / scp / rsync

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.

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

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

ssh -L 8080:localhost:80 myserver

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 .