4

I have Ubuntu server with two users

root #root account of the system
myuser #user added to sudo group 

Under root user I generated ssh keys which was added into. ssh directory

/root/.ssh 
         |__ id_rsa
         |__ id_rsa.pub

After it i run ssh-agent under the same root user

eval "$(ssh-agent -s)"

Than added my private key to it

ssh-add .ssh/id_rsa

It was properly added than i added my public key to github repo.

After it i switched to my sudo user copied id_rsa and id_rsa.pub to my sudo user home directory /home/myuser/.ssh/ and tried to access repo which was configured under root account and system prompt me that i hasn't access to it. I didnt understand why so. Can anyone guide me to properly setup access to github repo by the same private keys by two system users

3 Answers3

3

First, if possible, don't generate or do anything as root. root should be for system tasks only.

Second, if you add a passphrase-protected SSH key to an ssh-gent running as root, the user might not have access to said agent once you are using that same SSH key with sudo user.
Try and run the eval "$(ssh-agent -s)" with the sudo user in order to run an agent as user.

But more generally, that all sequence should be done as the one local account user directly.
Copying private SSH keys around is not a good practice.

I have one account and want to set it up to use the same github account under two linux users

Then:

  • under local account user user1, generate an ssh key:

    ssh-keygen -t rsa -P "" -m PEM
    

(defaults to ~user1/.ssh/id_rsa/id_rsa.pub)

  • under local account user user2, generate an ssh key:

    ssh-keygen -t rsa -P "" -m PEM
    

(defaults to ~user2/.ssh/id_rsa/id_rsa.pub)

Add both SSH public keys to your one unique GitHub account: see "Adding a new SSH key to your GitHub account".
You can add as many key you want.

You will then be able to authenticate as that GitHub account from your local user1 or user2, each one using their own SSH keys (no need to share private SSH keys across local accounts).

No need for a ~/.ssh/config in that case.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for good advice about using root. How i can check that ssh key was generated with passphrase ? – Наглый Спамер Feb 11 '20 at 06:57
  • Do you still have the command you used to generate those SSH keys? A simpler approach, for testing, would be to generate ones as user directly, without passphrase: `sudo user ssh-keygen -t rsa -P "" -m PEM` – VonC Feb 11 '20 at 06:58
  • Why you said that 'Copying private ssh keys around is not good practice' as i understand in github i can add only one ssh pub key – Наглый Спамер Feb 11 '20 at 07:41
  • @НаглыйСпамер You can generate as many ssh keys as you want, see https://stackoverflow.com/a/12066973/6309 – VonC Feb 11 '20 at 07:43
  • @НаглыйСпамер I have edited the answer to illustrate how. Key point: forget root. You don't need root. I show how those "aliases" are used. – VonC Feb 11 '20 at 07:48
  • You gived me good advices about root using but i understand you didn't understand me properly. I haven't multiple github accounts i have one account and want to set it up to use the same github account under two linux users – Наглый Спамер Feb 11 '20 at 08:03
  • @НаглыйСпамер I have rewritten the answer to address your setup (two local account, one Github remote account) – VonC Feb 11 '20 at 08:45
  • So easy solution. Complexity in my case was took place because i was thinking that in github can be added only one ssh private key. Thank you for your time spending i will upvote your answer – Наглый Спамер Feb 11 '20 at 08:50
  • @НаглыйСпамер Thank you. Not only can you add more than one, but... there is *no* actual limit to the number of SSH keys you can add!! See https://stackoverflow.com/a/48552578/6309. – VonC Feb 11 '20 at 08:53
1

I solved problem by following steps. •

  • Deleted all git ssh keys under root user as Vonc adviced

  • Than generated new ssh key pair under sudo user. Than added it to ssh-agent and added pub key to github account

  • Than repeted all steps from previous step

Now I have two sudo user on my server each of this user have their ssh key pairs in their /home/{USER}/.ssh directories and pub keys of each Linux user added to github account. And it's work fine. Thank you for everyone who was helped to find solution

-1

the easiest way is to create ssh configuration file

Create an SSH config file

When you have multiple identity files, create an SSH config file mechanism to create aliases for your various identities.

You can construct an SSH config file using many parameters and different approaches.

The format for the alias entries use in this example is:

Host alias 
  HostName github.com 
  IdentityFile ~/.ssh/identity

To create a config file for two identities (workid and personalid), you would do the following:

Open a terminal window.
Edit the ~/.ssh/config file. 

If you don't have a config file, create one.
Add an alias for each identity combination for example:

Host workid
HostName github.com 
IdentityFile ~/.ssh/workid

Host personalid
HostName github.com 
IdentityFile ~/.ssh/personalid

Important:

Don't forget to load the keys to your GitHub account.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • As i understand the important thing in ssh config is `alias` i have several ssh key pairs. First for accessing my server and second is ssh keys for accessing github. How i should configure alliases for this mentioned about entities ? – Наглый Спамер Feb 11 '20 at 07:32