Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to have 2 ssh keys in one Linux account?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

How to have 2 ssh keys in one Linux account?

I use ubuntu at home and I have ssh key to connect to my personal servers. But currently, I also need to connect to office servers, but I have a different generated ssh key for that also.

The only method I know for know, is to add another user on my desktop and switch user if I want to connect to my personal, or to office servers. Which is inconvenient.

Any ideas?

Comments

  • wychwych Member

    Its late, and I may have read that in reverse but wouldn't http://serverfault.com/a/221766 allow this?

    Thanked by 1jcaleb
  • @jcaleb said:
    I use ubuntu at home and I have ssh key to connect to my personal servers. But currently, I also need to connect to office servers, but I have a different generated ssh key for that also.

    The only method I know for know, is to add another user on my desktop and switch user if I want to connect to my personal, or to office servers. Which is inconvenient.

    Any ideas?

    Lookup "ssh config file". If I understood your question correctly, that should do it.

    Thanked by 1jcaleb
  • nunimnunim Member
    edited August 2015

    You could either create a bash alias i.e.

    ssh -i ~/.ssh/work.key [email protected] 

    Or use the ssh/config file to specify the key to be used with a specific host, i.e.

    ~/.ssh/config
    
    Host work
    HostName server.work.com
    User workuser
    IdentityFile ~/.ssh/work.key
  • jcaleb said: I use ubuntu at home and I have ssh key to connect to my personal servers. But currently, I also need to connect to office servers, but I have a different generated ssh key for that also.

    The simple thing to have done is to have given the office servers the same public key as you were already using for the personal servers rather than generating a new key. It adds no security to have multiple ssh keys under the same account in linux (unless some of them are password protected, in which case your inconvenience goes up tenfold however you slice it or dice it).

    Thanked by 1linuxthefish
  • DroidzoneDroidzone Member
    edited August 2015

    You can use an unlimited number of ssh keys per user.

    On the same account:

    Either specify the key for each ssh or scp command:

    For Server 1:

    ssh -i ~/.ssh/mypvtkey [email protected]

    For Server 2:

    ssh -i ~/.ssh/mypvtkey [email protected]

    Alternately, create ~/.ssh/config with the following entries:

    Host pvt
         Hostname pvtserver.com
         User root
         Identityfile /root/.ssh/id_rsa_mypvtkey
         Port 22
    Host work
         Hostname officeserver.com
         User root
         Identityfile /root/.ssh/id_rsa_myworkkey
         Port 9992
    

    Now, you can connect with:
    $ssh pvt and
    $ssh work

    You can even do:
    scp myfile pvt:/root/someplace/

  • I use Droidzone's solution.

  • vfusevfuse Member, Host Rep

    I'm using royal tsx (royal ts for windows), it can manage your different ssh keys.

  • thank you all for the solution. It seems my understanding is very shallow. Thanks for pointing that it can be solved using .ssh/config

    Thanked by 1netomx
  • Digitalocean 'library' has a nice howto on .ssh/config organization.

    Once you grasp how openssh parses the file, you can take your host/key/config-group organization to new levels of OCD.

    Happy times!

  • Same private key for all your servers?

  • ClouviderClouvider Member, Patron Provider
    edited August 2015

    @jcaleb you could use ssh-agent and load multiple keys for the session.

  • This is a bit of a necro, but is it possible to specify:

    Host xyz
        Hostname mywebsite.com
        User myusername
        Identityfile /home/jessie/.ssh/[email protected]
        Port 22
    

    "Host xyz" to be "Host myusername@mywebsite"? SSH seems to crap out on me if I do that.

    Host myusername@mywebsite
        Hostname mywebsite.com
        User myusername
        Identityfile /home/jessie/.ssh/[email protected]
        Port 22
    

    I better like organising my logins that way.

    Droidzone said: Awesome

  • singsingsingsing Member
    edited September 2015

    GM2015 said: "Host xyz" to be "Host myusername@mywebsite"? SSH seems to crap out on me if I do that.

    Try Host myusername@ mywebsite.com -- the space might to make ssh treat these as separate hostnames. Of course you would never be attempting to connect to myusername@.

  • GM2015 said: "Host xyz" to be "Host myusername@mywebsite"?

    The problem is that @ is the delimiter for username and hostname.

    Thanked by 1GM2015
  • elgselgs Member
    edited September 2015

    You can as well use the same key pair for unlimited servers. Normally they are at ~/.ssh/, id_rsa for private key and id_rsa.pub for the public key. Run this command from your local computer and you are good to go:

    cat ~/.ssh/id_rsa.pub | ssh -p22 root@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

    And here is a more comprehensive article about it:
    http://srcode.org/2014/05/07/login-ssh-without-password/

  • singsing said: Try Host myusername@ mywebsite.com -- the space might to make ssh treat these as separate hostnames. Of course you would never be attempting to connect to myusername@.

    Actually, if the goal is to potentially have different private keys for different accounts on the same host, using Match instead of Host is probably the thing to do.

    Something like (untested) Match user myusername host mywebsite or possibly originalhost instead of host.

  • The answer is simple you can have as many as you want really. If you do you not want to you the same key for every server all you have to do is issue the command ssh -i key_name root@serverip and you will have your answer.

    Of course you can name your keys anything you want my suggestion would be something to do with the server name so you do not get them confused. You may also wish to put them in their own folder as well to avoid clutter.

Sign In or Register to comment.