You are not logged in.

#1 2007-07-24 19:15:28

sessy
Member
Registered: 2006-01-20
Posts: 104

How to (auto) run ssh add only once?

I'd like to have a script that will start the ssh agent (and add my key) if it's not running already (run ssh-add automagically for the first time i try to use my key). But let me explain a bit more.

Here's how i login to my server now:

- start KDE [i dont know if it's important or not]
- run a script in konsole: eval ssh-agent; sh-add ~/.ssh/the-key
- type in my password
- now i can ssh to the server like this: ssh serveralias -t screen -RD [i have an alias set in .ssh/config] 
The problem with this is that i have to remember to run ssh-add before i try to run ssh.

Here's what i'd like to have:

- run ssh serveralias -t screen -RD
- be asked for the key's password only once in the whole KDE session
So the goal is to run ssh-add automagically for the first time i try to use my key.

Is is possible?

TIA!

Offline

#2 2007-07-24 19:24:16

skymt
Member
Registered: 2006-11-27
Posts: 443

Re: How to (auto) run ssh add only once?

Yes, it's possible. Add this to whatever script you use to start KDE (probably .xinitrc or .xsession):

eval `ssh-agent`

Note the backticks, they're important. Then you should be able to run ssh and it will automatically add the key to the agent the first time you use it.

The important thing is to run ssh-agent before KDE starts. That way the environment variables it sets get propagated to all the processes KDE starts.

Offline

#3 2007-07-24 19:25:53

nogoma
Member
From: Cranston, RI
Registered: 2006-03-01
Posts: 217

Re: How to (auto) run ssh add only once?

I use keychain (in [extra]) to deal with this for me. Then I have the following in my .zshrc:

if [[ -f ~/.keychain/$HOST-sh ]] && [[ $UID != "0" ]]
then
    keychain ~/.ssh/id_dsa
    . ~/.keychain/$HOST-sh > /dev/null
fi

So, the first console I get after login, I put in my passphrase, and that's it for the rest of the login.


-nogoma
---
Code Happy, Code Ruby!
http://www.last.fm/user/nogoma/

Offline

#4 2007-07-24 20:52:28

sessy
Member
Registered: 2006-01-20
Posts: 104

Re: How to (auto) run ssh add only once?

Thanks for both replys!

Somehow the xinitrc/xsession way did not work. But keychain did the trick! smile
Now i use

alias server="keychain ~/.ssh/keyfile -q; ssh host"

Thanks!

Offline

#5 2007-07-26 02:13:57

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: How to (auto) run ssh add only once?

You can use pam_script in /etc/pam.d/kdm to execute commands on login or logout during kde-session authentication.

Offline

#6 2007-07-26 07:41:04

sessy
Member
Registered: 2006-01-20
Posts: 104

Re: How to (auto) run ssh add only once?

T-Dawg wrote:

You can use pam_script in /etc/pam.d/kdm to execute commands on login or logout during kde-session authentication.

Yup, but my ssh key requires a password and i can't type it this way.

Offline

Board footer

Powered by FluxBB