I'd like to rsync some files to a remote server.

I tried

rsync -rav --delete -e 'ssh -p "$SSH_PORT" "$SSH_USERNAME"@"$SSH_HOST"' ./ /home/"$SSH_USERNAME"/src/

I receive the error

rsync: [Receiver] mkdir "/home/SSH_USERNAME/src" failed: No such file or directory (2)

I'm using the -e 'ssh -p format because that seems to be the recommended way to specify non-standard ssh port for a given server.

However, I'm concerned based on other answers that this might be causing permission weirdness preventing rsync from writing.

On the remote server, the given location does indeed exist, apparently with proper permissions:

on remote server

drwxr-xr-x 6 SSH_USERNAME SSH_USERNAME 4096 Jun 14 03:44 .
drwxr-xr-x 4 root       root       4096 Jun 13 23:18 ..
-rw------- 1 SSH_USERNAME SSH_USERNAME  221 Jun 14 03:44 .bash_history
-rw-r--r-- 1 SSH_USERNAME SSH_USERNAME  220 Apr  4  2018 .bash_logout
-rw-r--r-- 1 SSH_USERNAME SSH_USERNAME 3771 Apr  4  2018 .bashrc
drwx------ 2 SSH_USERNAME SSH_USERNAME 4096 Jun 14 00:44 .cache
drwx------ 3 SSH_USERNAME SSH_USERNAME 4096 Jun 14 00:44 .gnupg
-rw-r--r-- 1 SSH_USERNAME SSH_USERNAME  807 Apr  4  2018 .profile
drwxrwxr-x 3 SSH_USERNAME SSH_USERNAME 4096 Jun 14 19:53 src
drwx------ 2 SSH_USERNAME SSH_USERNAME 4096 Jun 14 00:52 .ssh
drwxr-xr-x  4 root       root       4096 Jun 13 23:18 .
drwxr-xr-x 22 root       root       4096 Jun 13 22:55 ..
drwxr-xr-x  5 caleb      caleb      4096 Jun 14 01:15 caleb
drwxr-xr-x  6 SSH_USERNAME SSH_USERNAME 4096 Jun 14 03:44 SSH_USERNAME

How can I rsync a directory to a server with a non-standard SSH port?

Just in case:

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:    18.04
Codename:   bionic

1 Answer 1


Even absent any other changes, your rsync syntax is a tad off. I'd suggest changing to:

rsync -rav --delete -e 'ssh -p "$SSH_PORT"' ./ "$SSH_USERNAME"@"$SSH_HOST":/home/"$SSH_USERNAME"/src/

Also, read man 5 ssh_config regarding how to construct SSH "aliases" that tie a particular host address, username, port (and possibly other parameters like an SSH identity key) to a convenient nickname. Then whenever you SSH to that nickname, ssh will use those particular parameters for the connection.

In your ~/.ssh/config, add:

Host Nickname
  Hostname $SSH_HOST
  Port $SSH_PORT


Nickname is a convenient name of your choice
$SSH_HOST is the proper name, or perhaps an IP
$SSH_PORT is the port number
$SSH_USERNAME is the desired username

For example:

Host foobar
  Hostname foobar.example.com
  Port 2222
  User myusername

Then your rsync syntax becomes:

rsync -rav --delete ./ foobar:/home/"$SSH_USERNAME"/src/

If the home directory of user $SSH_USERNAME on host foobar is already /home/$SSH_USERNAME, then you can likely shorten the remote path of the rsync command:

rsync -rav --delete ./ foobar:src/
  • This is helpful, and if I need to go down this path using sed or something I will, so thank you for this. I forgot to mention I'm aware you can set port etc in ssh config, but this rsync command is meant to eventually run on a github action (like a deploy hook basically) which runs in a little vm thing they spin up, so I'd prefer something "inline" if possible. It's probably not a huge deal to modify the ssh config as part of the build process on every go though.
    – Caleb Jay
    Jun 14, 2023 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .