7

I have a project which has all the id_rsa files and an SSH config in it, which has entries something like this:

Host projectname-server-1
  User root
  Hostname 26.139.18.47
  IdentityFile ./certificates/server-1/id_rsa

The idea is that the user can then include this config from their main SSH config file like this:

Include /home/myname/path-to-project/ssh-config

And then simply run ssh projectname-server-1 to connect to the server.

The issue is that this only works when I am in the directory /home/myname/path-to-project.

Is there any way to specify a path relative to the location of the config file?

2 Answers 2

5

I don't believe that such a "current directory" variable exists.

The ssh_config(5) - Linux man page lists this syntax which might help:

The file name may use the tilde syntax to refer to a user's home directory or one of the following escape characters: %d (local user's home directory), %u (local user name), %l (local host name), %h (remote host name) or %r (remote user name).

5

It sounds like what you're after is a way to store all of your ssh config and variables for your project in the project directory, and also, for paths to be processed relative to an included ssh config file, rather than the current path.

The short answer is no - AFAIK, ssh_config includes behave similarly to a C #include - you can treat them as expanding the referenced file into the current file.

That being said, you have have a few options to accomplish this in an extensible manner, with a degree of convention - I personally do version a lot of my ssh configs, My best suggestion is this:

Under your project root, store your ssh configs under one directory

my_project/
├── _config.yml
├── _ssh_config
│   └── server-1
│       ├── id_rsa
│       └── server-1.config
└── src

3 directories, 3 files

Write server-1.config like this:

Host projectname-server-1
  User root
  Hostname server-1.example.com
  # Our identity file is found in folder under ~/.ssh
  IdentityFile ~/.ssh/config.d/server-1/id_rsa

Under ~/.ssh/ make a new folder, config.d

mkdir -p ~/.ssh/config.d

To reference your project specific ssh files, you can symlink your my_project/_ssh_config/server-1 into config.d

ln -s path/to/my_project/_ssh_config/server-1 ~/.ssh/config.d/server-1

Update your Include directive in ~/.ssh/config:

# Relative "Include" paths are assumed to be relative to ~/.ssh/
Include config.d/server-1/server-1.config

Now, your ~/.ssh looks like this:

.ssh/
├── config
└── config.d
    └── server-1 -> /path/to/my_project/_ssh_config/server-1/
        ├── id_rsa
        └── server-1.config

2 directories, 3 files

The upshot of this approach is that you can easily extend it to server-2, 3, 4 etc, or to other projects. Just symlink path/to/server-x into config.d, and you can use globs in your user-level Include to take care of it:

Include config.d/**/*.config

I'd suggest that storing ssh keys in the project sounds unconventional, potentially dangerous, and not particularly useful. If your goal is portability for your config, I'd suggest making an exception for the path of your private key.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .