vagrant login as root by default

前端 未结 10 2708
春和景丽
春和景丽 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:14

    Solution:
    Add the following to your Vagrantfile:

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

    When you vagrant ssh henceforth, you will login as root and should expect the following:

    ==> mybox: Waiting for machine to boot. This may take a few minutes...
        mybox: SSH address: 127.0.0.1:2222
        mybox: SSH username: root
        mybox: SSH auth method: password
        mybox: Warning: Connection timeout. Retrying...
        mybox: Warning: Remote connection disconnect. Retrying...
    ==> mybox: Inserting Vagrant public key within guest...
    ==> mybox: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> mybox: Machine booted and ready!
    

    Update 23-Jun-2015: This works for version 1.7.2 as well. Keying security has improved since 1.7.0; this technique overrides back to the previous method which uses a known private key. This solution is not intended to be used for a box that is accessible publicly without proper security measures done prior to publishing.

    Reference:

    • https://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html
    0 讨论(0)
  • 2020-12-12 10:18

    I know this is an old question, but looking at the original question, it looks like the user just wanted to run a command as root, that's what I need to do when I was searching for an answer and stumbled across the question.

    So this one is worth knowing in my opinion:

    vagrant ssh servername -c "echo vagrant | sudo -S shutdown 0"
    

    vagrant is the password being echoed into the the sudo command, because as we all know, the vagrant account has sudo privileges and when you sudo, you need to specify the password of the user account, not root..and of course by default, the vagrant user's password is vagrant !

    By default you need root privileges to shutdown so I guess doing a shutdown is a good test.

    Obviously you don't need to specify a server name if there is only one for that vagrant environment. Also, we're talking about local vagrant virtual machine to the host, so there isn't really any security issue that I can see.

    Hope this helps.

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

    This works if you are on ubuntu/trusty64 box:

    vagrant ssh
    

    Once you are in the ubuntu box:

    sudo su
    

    Now you are root user. You can update root password as shown below:

    sudo -i
    passwd
    

    Now edit the below line in the file /etc/ssh/sshd_config

    PermitRootLogin yes
    

    Also, it is convenient to create your own alternate username:

    adduser johndoe
    

    Wait until it asks for password.

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

    Note: Only use this method for local development, it's not secure. You can setup password and ssh config while provisioning the box. For example with debian/stretch64 box this is my provision script:

    config.vm.provision "shell", inline: <<-SHELL
        echo -e "vagrant\nvagrant" | passwd root
        echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
        sed -in 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
        service ssh restart
    SHELL
    

    This will set root password to vagrant and permit root login with password. If you are using private_network say with ip address 192.168.10.37 then you can ssh with ssh root@192.168.10.37

    You may need to change that echo and sed commands depending on the default sshd_config file.

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

    This is useful:

    sudo passwd root
    

    for anyone who's been caught out by the need to set a root password in vagrant first

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

    Dont't forget root is allowed root to login before!!!

    Place the config code below in /etc/ssh/sshd_config file.

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