53

Rather than ssh-ing onto my Vagrant virtual machine with a "vagrant" user-name and password, I'd like to use kevin/kevin.

I modified my Vagrantfile to include:

config.ssh.username = "kevin"

Then, I ran vagrant reload.

The following output showed up:

[default] Waiting for machine to boot. This may take a few minutes...
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. This can
mean a number of things.

However, I could still ssh onto my vagrant box using vagrant/vagrant, yet I I couldn't ssh onto the box with a user-name and password of kevin/kevin or kevin/vagrant.

Note that I also tried this answer (https://stackoverflow.com/a/9924122/409976), but I could only ssh onto the box with user-name vagrant, not kevin (even though it's specified in the Vagrantfile).

How can I configure my Vagrantfile so that I can ssh using user-name kevin?

1
  • 1
    If you can ssh in yourself using vagrant, but not Kevin, then it sounds like you need to setup the kevin user in the guest image. The Vagrantfile configuration only establishes the user to use, it does not setup users on the guest.
    – zanerock
    Mar 25, 2014 at 19:20

5 Answers 5

70

You can ssh to the box using vagrant but NOT kevin, that's expected.

Most Vagrant base boxes have only 2 users with SSH access, root and vagrant. They both use vagrant as password, in addition, vagrant is configured for public key authentication using the insecure (why? see Vagrant insecure by default?) key pair provided in the Vagrant project on GitHub.

To be able to login as kevin, you'll have to ssh into the box and create the user (useradd -m -s /bin/bash -U kevin) first, configure public key authentication (many ways e.g ssh-copy-id, I'll leave it to you.)

You should be able to ssh into the box after creating the user using vagrant ssh if you properly set config.ssh.username in Vagrantfile.

Of course you can manually ssh into the box by (assume NAT is in use)

ssh -p 2222 kevin@localhost

or (on Linux)

ssh -p 2222 -i /opt/vagrant/embedded/gems/gems/vagrant-1.5.1/keys/vagrant.pub vagrant@localhost

6
  • 1
    Note that I was able to simply create a user (useradd ...), destroy & spin up my VM, and then successfully SSH with putty using kevin/kevin log-in. But, I'm a bit confused since, as you had mentioned in "Vagrant insecure by default?," shouldn't vagrant and root only be able to SSH without keys explicitly configured? Mar 26, 2014 at 15:16
  • 2
    You can ssh into the box with user vagrant, become root and then copy the file /home/vagrant/.ssh/authorized_keys to /home/kevin/.ssh/authorized_keys. Make sure the ownership and the permission of /home/kevin/.ssh/authorized_keys are OK (600 and owner kevin:group of kevin).
    – Saule
    Nov 19, 2014 at 15:26
  • @Saule 's comment is the best answer. Thread is old, but the search on this topic brought me here.
    – rickb
    Jun 23, 2017 at 16:08
  • I also thought this was a good writeup ermaker.github.io/blog/2015/11/18/… Mar 23, 2018 at 7:24
  • That link Rob gave is now dead, so use this one, courtesy of the Internet Archive 😉 Dec 14, 2020 at 16:34
29

Another solution, after adding user to Vagrant via your provisioning script:

## add kevin
useradd -m -s /bin/bash -U kevin -u 666 --groups wheel
cp -pr /home/vagrant/.ssh /home/kevin/
chown -R kevin:kevin /home/kevin
echo "%kevin ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/kevin

add this to your Vagrant file:

VAGRANT_COMMAND = ARGV[0]
if VAGRANT_COMMAND == "ssh"
  config.ssh.username = 'kevin'
end

Now Vagrant will use default vagrant user to provision your VM, but once it's up, you can use simple vagrant ssh to log in as kevin via default Vagrant ssh-key.

This way you can ship your desired Vagrantfile and users just say vagrant up and kevin automatically becomes ready to use.

1
  • 2
    Do you know how could one store the password as well? Doing config.ssh.password doesn't seem to be doing the trick. Thanks!
    – jpruiz114
    Apr 25, 2019 at 15:41
10

Create a vagrant file like the one below. Note that we are bootstrapping the vagrant user to immediately change to the 'kevin' user that we created.

bootstrap = <<SCRIPT
  useradd -m kevin --groups sudo
  su -c "printf 'cd /home/kevin\nsudo su kevin' >> .bash_profile" -s /bin/sh vagrant
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.host_name = "kevin"
  config.vm.provision "shell", inline: "#{bootstrap}", privileged: true
end

now ssh into the vm:

$ vagrant ssh
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-83-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.


To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

kevin@kevin:/home/vagrant$
1
  • Nice, I like the self-sufficient nature of this. Dec 14, 2020 at 16:31
2

A bit of a hack, but you could add the line sudo su - kevin to your .bash_profile file and add kevin to the sudoers file with no password.

This will change the current user to kevin when logging in as the vagrant user from the command line.

The advantage of this approach is that NAT does not need to be enabled.

1
  • this is such a hack! but it is simple and i like it.
    – aidanmelen
    Aug 12, 2017 at 23:36
1

i find a easy way to achieve this by copy the .ssh to the user, such as your user name is alice

sudo su -
cp -r /home/vagrant/.ssh/ /home/alice/
chown -R alice:alice  /home/alice/.ssh

Then add config in the Vagrantfile:

# ...
config.ssh.username = 'alice'
# ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.