Adding /etc/hosts entry to host machine on vagrant up

前端 未结 4 1706
花落未央
花落未央 2021-01-30 17:11

Is it possible for one to modify files on the host machine during the vagrant up process? For example, adding an entry to the host machine\'s /etc/hosts

相关标签:
4条回答
  • 2021-01-30 17:13

    Previously I didn't know how to vagrant edit my etc/host file. But when i reinstalled window and vagrant, this feature disappeared.

    0 讨论(0)
  • 2021-01-30 17:18

    I don't want to depend on some plug in to vagrant. It should be standard feature in Vagrant!!!! Untill then I use a shell script to propagate VM's in my cluster of new VMs. The key lines are :

    # Obtain the hostkey based on the IP-address and add it to the known_host list 
    ssh-keyscan -t ecdsa ${START}.${OFFSET} >> /home/vagrant/.ssh/known_hosts
    
    # obtain the hostname, because you might not know it yet, with the IP address:
    EXTERNAL_HOSTNAME=`ssh ${START}'.'${OFFSET} 'hostname'`
    # obtain the key ot the new other VM based on hostname and also add to known_hosts
    ssh-keyscan -t ecdsa ${EXTERNAL_HOSTNAME} >>  /home/vagrant/.ssh/known_hosts
    # so now you have the IP address and the corresponding hostname
    # add to /etc/hosts without being asked for "yes/no"
    echo  ${START}'.'${OFFSET}' '${EXTERNAL_HOSTNAME} >> /etc/hosts
    

    Where IPADRRESS is the IP address of the master VM in the cluster with several slave node VM's with succeedding ip-addresses. (IPADDRESS=IPADDRESS + 1 untill no successfull ping)

    IPADDRESS=`ip addr show eth1 | grep 'inet ' | cut -d ' ' -f 6 | cut -d '/' -f1`
    START=`echo ${IPADDRESS} | cut -d '.' -f1,2,3`
    OFFSET=`echo ${IPADDRESS} | cut -d '.' -f4`
    

    And then I loop trough the next IP addresses until no more succesfull pings. I do not want to hardcode anything (ip-address or hostname), but to find out itself.

    Resulting /etc/hosts file (after sort /etc/hosts | uniq > /tmp/hosts.uniq && sudo sh -c 'mv /tmp/hosts.uniq /etc/hosts' :

    [vagrant@master ~]$ cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    127.0.0.1       master.RHEL70.local     master
    192.168.1.50 master.RHEL70.local
    192.168.1.51 node01.RHEL70.local
    192.168.1.52 node02.RHEL70.local
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    
    0 讨论(0)
  • 2021-01-30 17:23

    OK, so now the guy sitting next to you at the coffee shop can most likely ssh to port 2222 (EDIT: changed on newer versions of vagrant, unless you explicitly enable external access) on your computer, login as vagrant with the insecure key, modify your Vagrantfile, since it's mounted read-write and owned by the vagrant user, insert arbitrary ruby code to run in the host environment, and now it looks like they've got root access on the host environment as well. Brilliant.

    I hope people run firewalls on their development machines.

    EDIT:

    So after writing the above, I bugged the author of Vagrant, the default has been changed so that port 2222 is not open by default on the external interface. Big improvement (though still something to be careful of, since external access is often opened up for various reasons).

    So, having put in effort to get the situation fixed since making this comment, I'm now getting down votes, apparently because the comment is out of date. Damn. It was correct when written.

    EDIT:

    In response to Steve Buzonas, the point is that if there's any likelhihood of the virtual machine being compromised then giving the vagrant up process elevated permissions represents a serious risk to the security of the host environment, and also being able to modify the /etc/hosts environment file is dangerous, even without general root access. As I've pointed out, vagrant's approach to keeping the VM secure is not particularly rigorous.

    0 讨论(0)
  • 2021-01-30 17:34

    The solution is to use vagrant-hostsupdater

    vagrant plugin install vagrant-hostsupdater
    

    This plugin adds an entry to your /etc/hosts file on the host system.

    On up and reload commands, it tries to add the information, if its not already existant in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.

    On halt, suspend and destroy, those entries will be removed again.

    0 讨论(0)
提交回复
热议问题