vagrant login as root by default

前端 未结 10 2709
春和景丽
春和景丽 2020-12-12 09:34

Problem: frequently the first command I type to my boxes is su -.

Question: how do I make vagrant ssh use the root user by default?

相关标签:
10条回答
  • 2020-12-12 10:30

    If Vagrantfile as below:

    config.ssh.username = 'root'
    config.ssh.password = 'vagrant'
    config.ssh.insert_key = 'true'
    

    But vagrant still ask you root password, most likely the base box you used do not configured to allow root login.


    For example, the offical ubuntu14.04 box do not set PermitRootLogin yes in /etc/ssh/sshd_config.

    So If you want a box can login as root default(only Vagrantfile, no more work), you have to :

    1. Setup a vm by username vagrant(whatever name but root)

    2. Login and edit sshd config file.

      ubuntu: edit /etc/ssh/sshd_config, set PermitRootLogin yes

      others: ....

      (I only use ubuntu, feel free to add workaround of other platforms)

    3. Build a new base box:

      vagrant package --base your-vm-name
      

      this create a file package.box

    4. Add that base box to vagrant:

      vagrant box add ubuntu-root file:///somepath/package.box
      

      then, you need use this base box to build vm which allow auto login as root.

    5. Destroy original vm by vagrant destroy

    6. Edit original Vagrantfile, change box name to ubuntu-root and username to root, then vagrant up create a new one.

    It cost me some time to figure out , it is too complicate in my opinion. Hope vagrant would improve this.

    0 讨论(0)
  • 2020-12-12 10:32

    Adding this to the Vagrantfile worked for me. These lines are the equivalent of you entering sudo su - every time you login. Please notice that this requires reprovisioning the VM.

    config.vm.provision "shell", inline: <<-SHELL
        echo "sudo su -" >> .bashrc
    SHELL
    
    0 讨论(0)
  • 2020-12-12 10:37
    vagrant destroy
    vagrant up
    

    Please add this to vagrant file:

    config.ssh.username = 'vagrant'
    config.ssh.password = 'vagrant'
    config.ssh.insert_key = 'true'
    
    0 讨论(0)
  • 2020-12-12 10:40

    I had some troubles with provisioning when trying to login as root, even with PermitRootLogin yes. I made it so only the vagrant ssh command is affected:

    # Login as root when doing vagrant ssh
    if ARGV[0]=='ssh'
      config.ssh.username = 'root'
    end
    
    0 讨论(0)
提交回复
热议问题