ssh-add add all private keys in .ssh directory

115

Solution 1

Slightly convoluted, but:

for possiblekey in ${HOME}/.ssh/id_*; do
    if grep -q PRIVATE "$possiblekey"; then
        ssh-add "$possiblekey"
    fi
done

You can also add all of your keys to your ~/.ssh/config each in their own IdentityFile directive outside of a Host directive:

# Global SSH configurations here will be applied to all hosts
IdentityFile ~/.ssh/id_dsa
IdentityFile ~/.ssh/id_project1
IdentityFile ~/.ssh/id_someotherkey

Host somespecifichost.example.com
    IdentityFile ~/.ssh/id_specifichostonlykey

The latter, honestly-better, method has the added perk of not suddenly picking up a new key that you've added without you explicitly adding it to the "keyring" as it were.

Solution 2

I have come up with the following command to do this:

find ~/.ssh/ -type f -exec grep -l "PRIVATE" {} \; | xargs ssh-add &> /dev/null

This will find all files in the .ssh directory that contain PRIVATE, passing the name / path of the private key file to ssh-add.

Solution 3

You don't really need find, you can just use a recursive grep using the l flag to send only matching filenames:

grep -slR "PRIVATE" ~/.ssh/ | xargs ssh-add

Share:
115

Related videos on Youtube

Travis Heeter
Author by

Travis Heeter

Updated on September 18, 2022

Comments

  • Travis Heeter
    Travis Heeter over 1 year

    My CSS file is located here: /Areas/Car/Views/Shared/CSS/css.css. I reference it in my code like this:

    <link href="@Url.Content("/Areas/Car/Views/Shared/CSS/css.css")" rel="stylesheet" type="text/css" />
    

    However, C# is throwing a 404 error for that link.

    I've tried adding a context.MapRoute:

    context.MapRoute(
        "CSS",
        "/Areas/Car/Views/Shared/{action}/{id}",
        new { controller = "Car", action = "CSS", id = UrlParameter.Optional }
    };
    

    And then accessing it with /Car/CSS/css.css (and all the variations I can think of)

    But I am still getting the same error. If, however, I reference a different css file - not in Areas - it works fine. What am I doing wrong?

    • SauceCode
      SauceCode over 7 years
      Is there a reason you need to add all these keys? If you specify the IdentityFile for each host inside your ~/.ssh/config file, ssh will use that file when you try to connect.
    • Matt Clark
      Matt Clark over 7 years
      Using Amazon AWS, my host IP address are ephemeral, and are constantly changing ( test stacks ). I can not rely on a config file to always be correct. Each layer of the stack has different user permissions.
  • Matt Clark
    Matt Clark over 7 years
    ohhh, good call. I did not actually know that IdentifyFile definitions could exist outside of a Host.
  • Malvineous
    Malvineous about 5 years
    Unfortunately this can prevent you from connecting to a server if you have many keys, claiming Too many authentication failures as it tries each key one by one for all servers, and some disconnect after a certain number of incorrect keys are tried.
  • Admin
    Admin almost 2 years
    This is the best way out of these answers if you have multiple keys with the same passphrase, as you will only have to enter it once.