You can do so by adding the following parameter to your config file (/etc/ssh/sshd_config).
Forces the execution of the command specified by ForceCommand, ignoring any command supplied by the client and ~/.ssh/rc if present. The command is invoked by using the user's login shell
with the -c option. This applies to shell, command, or subsystem execution. It is most useful inside a Match block. The command originally supplied by the client is available in the
SSH_ORIGINAL_COMMAND environment variable. Specifying a command of “internal-sftp” will force the use of an in-process sftp server that requires no support files when used with
An other option is to use the .ssh/rc files on a per user basis.
To use the ForceCommand method you just add
ForceCommand /usr/bin/ownscript at the bottom of the file
/etc/ssh/sshd_config (on the server).
The script looks like this:
#Script file for ssh
#put your commands here
echo "test" > /tmp/test.txt
#exit by calling a shell to open for the ssh session
Don't forget to chmod the script
sudo chmod +x /usr/bin/ownscript