I'm trying to sync files between two hosts using rsync over SSH with a private key that's restricted to only allow the command '/usr/bin/rsync --server -slHDtprze.iLsfxCIvu'. This setup works for syncing, but I'm facing an issue. When I attempt to connect to the host using the same key but not running rsync, the connection hangs indefinitely. Is there a way to specify a timeout for rsync when using the --server option, or is there another solution to manage this?
2 Answers
When you set that SSH key to only trigger rsync, actually logging in with that key while expecting a shell will cause it to hang because the SSH client is expecting a shell session, but the server just sends out rsync commands. It's normal for forced-command keys to behave this way.
To manage this, you could configure the client rsync command with a timeout. For example:
```
rsync --timeout=60 -e "ssh -o ConnectTimeout=10 -o ServerAliveInterval=15 -o ServerAliveCountMax=2" ...
```
Alternatively, you could wrap the forced command with a `timeout` so that it automatically terminates after a certain period. But in short, that key's purpose is only for rsync, so accessing it via SSH will always appear like it's stuck.
What you're experiencing is typical when using a forced command. When you SSH, it tries to set up an interactive session, but the SSH connection is already locked into the rsync protocol command which just waits there.
You’ve got a couple of options:
1. Add options like `no-pty,no-agent-forwarding,no-X11-forwarding,no-port-forwarding` to the key settings to fail fast when trying to establish an interactive session.
2. Create a small wrapper script for your forced command, so it allows only rsync to run. If someone tries to do something else, it exits right away.

Related Questions
Can't Load PhpMyadmin On After Server Update
Redirect www to non-www in Apache Conf
How To Check If Your SSL Cert Is SHA 1
Windows TrackPad Gestures