2

I have a sinple Vagrant file, which create 3 instances :

config.vm.define "node1" do |subconfig|
    subconfig.vm.box = "ubuntu/trusty64"
subconfig.vm.network "private_network", ip: "192.168.33.10"
end

config.vm.define "node2" do |subconfig|
    subconfig.vm.box = "ubuntu/trusty64"
subconfig.vm.network "private_network", ip: "192.168.33.11"
end

 config.vm.define "master" do |subconfig|
     subconfig.vm.box = "ubuntu/trusty64"
subconfig.vm.network "private_network", ip: "192.168.33.9"
subconfig.vm.provision :shell, path: "install.sh"
end

On master node, i want to generate ssh-key , then copy it to 2 other nodes. My provision shell ( install.sh ) :

apt-get install sshpass -y
ssh-keygen -t rsa -b 4096 -C "tuananh93nguyen@gmail.com" -N "" -f /home/vagrant/.ssh/id_rsa
sudo chmod -R 755 /home/vagrant/.ssh
sshpass -p vagrant ssh-copy-id -o StrictHostKeyChecking=no vagrant@192.168.33.10
sshpass -p vagrant ssh-copy-id -o StrictHostKeyChecking=no vagrant@192.168.33.11

But i got this error when running 2 last line :

> master: /usr/bin/ssh-copy-id: ERROR: No identities found

But if i ssh into master node, then run install.sh again, everything is fine . I think it may be about user permission when run on vagrant. How can i fix it ?

voxter
  • 853
  • 2
  • 14
  • 30
  • My guess is that `install.sh` is running as a privileged user (default) and is looking for `/root/.ssh/id_rsa` instead. Specify the identity file with `-i /home/vagrant/.ssh./id_rsa.pub`. – fernandezcuesta Sep 06 '17 at 08:11
  • 1
    This is wrong `sudo chmod -R 755 /home/vagrant/.ssh`. You open the permissions of your `id_rsa` too much. And ssh won't work. Also if the user is root then by default ssh-copy-id will look for .ssh in root's home folder – Tarun Lalwani Sep 06 '17 at 08:11

1 Answers1

0

You have an issue that you're running your script with root user but pushed in vagrant folder, so you're confusde.

Add privileged: false in your Vagrantfile script:

subconfig.vm.provision :shell, path: "install.sh", privileged: false

so it will be run with vagrant user rather than root and you do not need then to change the permission of the key (remove sudo chmod -R 755 /home/vagrant/.ssh)

small note: if you're using the same box for all 3 machines, you can just have a common variable and remove all lines of subconfig.vm.box = "ubuntu/trusty64":

config.vm.box = "ubuntu/trusty64"

config.vm.define "node1" do |subconfig|
    subconfig.vm.network "private_network", ip: "192.168.33.10" 
end
...
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139