109

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?

Version: vagrant 1.6.5

Mike D
  • 5,984
  • 4
  • 31
  • 31
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Feb 26 '17 at 22:39

12 Answers12

167

This is useful:

sudo passwd root

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

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Ed Williams
  • 2,447
  • 3
  • 15
  • 21
  • 1
    Saved my life. Do you know if this has any repercussions with how vagrant acts? – Matt Jan 20 '15 at 19:48
  • 2
    Lesson here is that if you create a base box, follow the conventions suggested by Hashicorp and set your root passwd to 'vagrant'. see http://docs.vagrantup.com/v2/boxes/base.html – Mike D Jun 24 '15 at 00:15
  • Are there any potential security issues with a server that has a really easily guessed root password lying around on your personal computer, though? I imagine the network settings help with this, but I wonder. – Nathan Basanese Aug 20 '15 at 20:55
  • 1
    Thank you for the hint. It turns out that Ubuntu's vagrant boxes set a random password for the root user (See https://groups.google.com/d/msg/vagrant-up/xMWKkKW24xs/f5OaV5QWAp4J). So one always has to set a root password first. – asmaier Nov 03 '15 at 14:09
131

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:

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Mike D
  • 5,984
  • 4
  • 31
  • 31
  • Could there be any reason to getting the password prompt when running `vagrant ssh`? (after making the described changes) Vagrant version 1.7.2 – roign Jul 10 '15 at 09:58
  • @RobertIgnat possibly that you need to set your root passwd to match what you have in your `config.ssh.password` value. you should be able to comment out the newly added `config.ssh.*` lines to access your box and then change the root password via `sudo passwd root`. – Mike D Jul 10 '15 at 21:15
  • 12
    For a machine which has already been provisioned, do the above, and do `mkdir -m 700 /root/.ssh && cp ~vagrant/.ssh/authorized_keys /root/.ssh` – Felipe Alvarez Jul 31 '15 at 03:19
  • 2
    doesn't work for a fresh Vagrant install, probably because when one uses e.g. ubuntu/xenial, one HAS to use the user logins baked there. (see also https://stackoverflow.com/a/40478402/605463) – axd Jan 03 '18 at 13:50
  • 4
    For anyone else using `centos/7`: This box sets a random root password; one can `vagrant ssh` and then `sudo su` to root, but (from what I can tell) the config settings don't work on a fresh `vagrant up` using this VM. – mhulse Mar 25 '18 at 22:11
45

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.

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Thyag
  • 1,217
  • 13
  • 14
16

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.

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Mithril
  • 12,947
  • 18
  • 102
  • 153
11

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

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

PermitRootLogin yes
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
Andrew Coding
  • 121
  • 1
  • 5
  • Welcome to SO. Please add enough details and context to answers. Especially when answering old question with some good answers – Jayan Jan 30 '16 at 08:48
  • this was more useful for me than the main solution. ssh doesn't let you login as root by default. – bmbigbang Jun 11 '19 at 10:56
8

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.

artronics
  • 1,399
  • 2
  • 19
  • 28
  • instead of adding the option to the end, I would prefer `sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config` – rubo77 May 02 '21 at 13:42
7

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
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
fpereiro
  • 71
  • 1
  • 1
2

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.

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
1

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
jxmallett
  • 4,087
  • 1
  • 28
  • 35
0

I used vagrant putty with the vagrant multi putty plugin, it took me directly to root.

aMaklad
  • 41
  • 1
0

this may help along answering how to allow root access, as well as password based access to vagrant.

the official image by debian (latest stable debian 12 bookworm) does not include permission for root login as well as password based authentication.

there are a few configurations that will affect connectivity, such as insert key (not needed; default is fine), the ssh.username and ssh.password should not be set as it will default to password based auth when trying to spin up the instance. Contrary to the above, it seems like newer vagrant versions do not timeout; we will get the following error instead.

    single: Warning: Authentication failure. Retrying...
    single: Warning: Authentication failure. Retrying...
    single: Warning: Authentication failure. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
...

the following is configured in Vagrantfile.

# note that the following few settings (insert key, username, password) can/needs to be omitted.
# config.ssh.insert_key = false
# config.ssh.username = 'vagrant'
# config.ssh.password = 'vagrant'

config.vm.box = "debian/bookworm64"
config.vm.provision "shell", inline: <<-'SHELL'
  sed -i 's/^#* *\(PermitRootLogin\)\(.*\)$/\1 yes/' /etc/ssh/sshd_config
  sed -i 's/^#* *\(PasswordAuthentication\)\(.*\)$/\1 yes/' /etc/ssh/sshd_config
  systemctl restart sshd.service
  echo -e "vagrant\nvagrant" | (passwd vagrant)
  echo -e "root\nroot" | (passwd root)
SHELL

hope it helps.

with kudos to a similar issue reference : How to enable password ssh authentication for Vagrant VM?

mirageglobe
  • 2,446
  • 2
  • 24
  • 30
-11
vagrant destroy
vagrant up

Please add this to vagrant file:

config.ssh.username = 'vagrant'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'
Nicolas BADIA
  • 5,612
  • 7
  • 43
  • 46
eric more
  • 64
  • 3