The User rc File

The shell script /etc/ssh/sshrc is invoked by the SSH server for each incoming SSH connection. [5.6.3] You may define a similar script in your account, ~/.ssh/rc (OpenSSH) or ~/.ssh2/rc (Tectia), to be invoked for every SSH connection to your account. If this file exists, /etc/ssh/sshrc isn’t run.

The SSH rc file is much like a shell startup file (e.g., ~/.profile or ~/.cshrc), but it executes only when your account is accessed by SSH. It is run for both interactive logins and remote commands. Place any commands in this script that you would like executed when your account is accessed by SSH, rather than an ordinary login. For example, you can run and load your ssh-agent in this file:

    # ~/.ssh/rc, assuming your login shell is the C shell
    if ( ! $?SSH_AUTH_SOCK  ) then
      eval `ssh-agent`
      /usr/bin/tty | grep 'not a tty' > /dev/null
      if ( ! $status ) then
        ssh-add
      endif
    endif

Like /etc/ssh/sshrc, your personal rc file is executed just before the shell or remote command requested by the incoming connection. OpenSSH always uses the Bourne shell (/bin/sh) for ~/.ssh/rc, as it does for /etc/ssh/sshrc. In contrast, Tectia uses your login shell for ~/.ssh2/rc, unlike /etc/ssh2/sshrc.

Get SSH, The Secure Shell: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.