Add SSH Public Key to Vagrant
2 minutes read
You can access your Vagrantbox via:
$ vagrant ssh
However if you want to access it via usual
ssh cli, how can you do that?
By default Vagrant binds your
2222 port to your Vagrantbox’s
So you can access your box by;
$ ssh -p 2222 [email protected]
It is same thing as
But before you can do that you need to add your
ssh pub_key to your vagrant
The best way to add your pub_key is to use
$ ssh-copy-id -i ~/.ssh/id_rsa.pub -p 2222 [email protected] /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/<username>/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password:
After you prompt your vagrant user password, pub_key will be appended.
Number of key(s) added: 1 Now try logging into the machine, with: "ssh -p '2222' '[email protected]'" and check to make sure that only the key(s) you wanted were added.
Default credentials are:
- user: vagrant
- password: vagrant
You can use some manual unix cli magic:
$ cat ~/.ssh/id_rsa.pub | ssh -i ~/.ssh/id_rsa.pub -p 2222 [email protected] 'cat >> .ssh/authorized_keys && echo "SSH key copied."'
Or, you can use Vagrant’s provision:
Vagrant.configure(“2”) do |config| config.vm.provision "shell", inline: <<-SHELL cat /home/<username>/.ssh/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys SHELL end
Now you can access your Vagrantbox via
ssh without password:
$ ssh -i ~/.ssh/id_rsa.pub -p 2222 [email protected]