How can I set a timeout for rsync with a forced SSH command?

0
11
Asked By CuriousCactus88 On

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

Answered By TechSavant42 On

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.

Answered By LinuxNinjaX On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.