0

I have just installed VirtualBox and Vagrant on my coputer and initialized a ubuntu/bento-16.04 box. I have added a new user beside vagrant user and I have generated and copied the public key on the server in a file named authorized_keys. I also disbaled the possibility to authenticate with a paswword.

The problem is that i can connect to the server only if a provide to the ssh command the full path to my private key (located on the C drive of my computer), under -i flag.

If a try to connect just with ssh -p 2222 lucian@localhost i got this message: permission denied(public key)

Is there any way i cann connect by ssh without providing the full path to the private key located on my computer?

My impression is that the server does not know about the location of the private key.

I red the earlier threads related to this issue but i can figure out what am I suppose to do to make the ssh-agent provide to the server the private key automatically.

Thanks!

  • have you tried to type vagrant ssh? you have to have the private key of the vagrant user you just created in your computer as well and the vagrant guest should have your private key as well – Yien Nov 30 '17 at 15:38
  • Hi Yien!vagrant ssh works fine! I just want to connect to the server with the new user I have created, but without providing the full path to the private key file every time. – LTanase Nov 30 '17 at 15:45
  • after you generate the keys did you change directory where it should be restored? or just the default one? edit /etc/sshd_config and add the location where ssh needs to look into. – Yien Nov 30 '17 at 15:51
  • no I did not change it. what entry I should edit in sshd_config file? or i just need to add a new one specifying the location of the IdentityFile – LTanase Nov 30 '17 at 16:01
  • oh you can edit this one first, in ~/.ssh/config and add this one IdentityFile /path/to/identity – Yien Nov 30 '17 at 16:06
1

Without editing files, you can use ssh-add yourkeyfile, it won't survive a reboot though.

To configure it as a permanent setting you can create or edit the following file

nano /home/yourusername/.ssh/config

Add the following entry

Host serverIP/FQDN
IdentityFile /home/yourusername/.ssh/yourkeyfile (or other path)

Now every time you connect to that specific host it will use that file.

You can also add the port number and username in the above configuration so that you only need to type ssh remotehostname to connect.

To do that add

 User remote_username
 Port remote_port#
  • I have tried to do that and does not work.I received this message: could not resolve hostname. My impression is that i am not providing correctly the IP of the host. Is this the ip that appears in VagrantFile when i configured the access based on private network option? How can I find this IP? – LTanase Nov 30 '17 at 17:11
  • If a run vagrant ssh-config hostname appears as 127.0.0.1. – LTanase Nov 30 '17 at 17:14
  • Ah shoot, missed the vagrant tie in. Have you set config.ssh.private_key_path in the vagrantfile? vagrantup.com/docs/vagrantfile/ssh_settings.html – m_krsic Nov 30 '17 at 18:01
  • I did it now, but after running vagrant reload, vagrant cannot connect anymore to the box and the command vagrant ssh doe not work anymore. I can still connect with the other user (but only if i provide the full path to the private key). Do I need to have the private key file on ther server, too? – LTanase Nov 30 '17 at 18:24
  • You have to set all of the settings for the host in the same vagrantfile once you set one of the settings otherwise everything else will be going through the default that vagrant ships with. – m_krsic Nov 30 '17 at 18:50
-1

I have solved it by creating a config file stored on the local machine (~/.ssh/config) with the following content

Host myhost HostName 192.168.10.10 User username IdentityFile /path/to/identity file IdentitiesOnly yes

where

  • myhost is any name you choose to use together with ssh command for connecting to the server
  • HostName is the IP address set in the Vagrantfile for private network
  • user is the name of the new user created at the server level
  • IdentityFile is the path to the private key stored locally (public key being stored on the server in the authorized_keys file)

After that i can connect to the server from vagrant simply with the command ssh myhost.

  • You should add that "a config file stored on the local machine" is not some arbitrary config file but exactly ~/.ssh/config. – PerlDuck Dec 26 '17 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.