`security unlock-keychain` from a bash script

15.6k Views Asked by At

On MacOSX, if I ssh in and run the command:

security -v unlock-keychain -p <password> <keychain_path>

The keychain unlocks and all is well with the world.

If I put that same command into a bash script and run

bash test.sh

I get prompted for the password.

How can I get around this?

2

There are 2 best solutions below

2
On BEST ANSWER

You need to explicitly let your script test.sh access your keychain.

  • Open the Keychain Access
  • Right click on the private key
  • Select "Get Info"
  • Select "Access Control" tab
  • Click "Allow all applications to access this item"
  • Click "Save Changes"
  • Enter your password
  • Enjoy

Credits: Running xcodebuild from a forked terminal

0
On

In a weird twist, running security unlock-keychain from within screen or tmux might fail if some kind of security token has expired. I attempted to resolve this by modifying matching environment variables, but this failed. I had to ultimately abandon the screen session and start a new one.

An incomplete answer, but might be useful to others with a similar problem.