How to copy ssh-copy-id to vagrant guest os from host os

I’ve created a vagrant guest machine. Below is the Vagrantfile -

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/lunar64"

  config.vm.network "private_network", ip: "192.168.33.10"

   config.vm.provider "virtualbox" do |vb|
     vb.memory = "1024"
   end
end

When I do ping to the guest machine it works fine from host machine.

$ ping -c 3 192.168.33.10
PING 192.168.33.10 (192.168.33.10) 56(84) bytes of data.
64 bytes from 192.168.33.10: icmp_seq=1 ttl=64 time=0.606 ms
64 bytes from 192.168.33.10: icmp_seq=2 ttl=64 time=0.588 ms
64 bytes from 192.168.33.10: icmp_seq=3 ttl=64 time=0.301 ms

--- 192.168.33.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2031ms
rtt min/avg/max/mdev = 0.301/0.498/0.606/0.139 ms

Now I would like ssh-copy-id from host machine into guest os.
I know I can ssh into vagrant using command vagrant ssh. But I would like to ssh using IP address of the guest OS like ssh 192.168.33.10.

But I’m getting below error -

$ ssh-copy-id 192.168.33.10
The authenticity of host '192.168.33.10 (192.168.33.10)' can't be established.
ED25519 key fingerprint is SHA256:Ultxy8OXuoD+ftDsJvy19fYbJNBS/znIukm+39GNdLE.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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: 2 key(s) remain to be installed -- if you are prompted now it is to install the new keys
rajkumar@192.168.33.10: Permission denied (publickey).

I would like do this way because I’m learning ansible playbooks using vagrant.
How can I fix this issue?

Hi @rajcspsg,

Welcome to the HashiCorp Forums!

Instead of manually copying the SSH public key after provisioning, you can use the provisioners to do it for you by adding the following snippet to your Vagrantfile.

  config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/my_key.pub"

  config.vm.provision "shell", inline: <<-SHELL
    cat /home/vagrant/.ssh/my_key.pub >> /home/vagrant/.ssh/authorized_keys
  SHELL

I hope this helps.

Hi @Ranjandas

I’ve tried your suggestion.

Below is my Vagrantfile -

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/lunar64"

  config.vm.network "private_network", ip: "192.168.35.15"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # config.vm.synced_folder "../data", "/vagrant_data"

  # config.vm.synced_folder ".", "/vagrant", disabled: true

   config.vm.provider "virtualbox" do |vb|
     vb.memory = "2048"
   end
  
   config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/my_key.pub"

   config.vm.provision "shell", inline: <<-SHELL
    cat /home/vagrant/.ssh/my_key.pub >> /home/vagrant/.ssh/authorized_keys
    apt-get update
    apt-get install -y apache2
   SHELL
end

And I did vagrant up and still I get the same error

I attached the screenshot. still my requirement is not working.

Thanks again for your help!!!

Hi @rajcspsg,

You have to ssh as vagrant user as the authorized key is placed into the vagrant users profile. Try ssh vagrant@ and that should work.