Search code examples
sshcronrsync

how do you load an ssh_config file within a cron job


ssh_config files allow you to configure an ssh client

You can specify aliases, default users and identity files for different ssh hosts, amongst other things

The docs state that the ssh_config options are loaded magically by the ssh client in the following order:

  1. command line options
  2. user-specific file (~/.ssh/config)
  3. system-wide file (/etc/ssh/ssh_config)

However, these configuration options aren't automatically available/respected within a cron job context

how can you load an ssh configfile such as ~/.ssh/config for a crontab context or in a specific cronjob?

Update: issue was this: https://superuser.com/questions/508408/public-key-always-asking-for-password-and-keyphrase


Solution

  • ssh --help says that there is a -F configfile option. However, I think ssh should still be checking in ~/.ssh/config and /etc/ssh/ssh_config, even when run via cron.

    When run from cron, the HOME environment variable is set to point to your normal home directory, so ssh has all the information it needs to locate the standard configuration files.

    I tested this by putting the following cron job in place:

    * * * * * strace -o /tmp/trace -f -s 80 ssh localhost uptime > /tmp/trace
    

    And inspecting /tmp/trace after the job has run, I see:

    29079 open("/home/lars/.ssh/config", O_RDONLY) = 3
    29079 open("/etc/ssh/ssh_config", O_RDONLY) = 3
    

    Update

    On my OS X machine (OS X 10.10.3), I set up the following ~/.ssh/config file:

    Host stackoverflow
      Hostname fileserver.house
      IdentityFile fileserver_rsa
    

    I created the following cron entry:

    * * * * * ssh stackoverflow uptime > $HOME/output
    

    The only way that would work would be if ssh were reading my ~/.ssh/config file...and it works just fine. What leads you do believe that things aren't working?