Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH onto Vagrant Box With Different Username

Tags:

ssh

vagrant

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?

like image 257
Kevin Meredith Avatar asked Mar 25 '14 18:03

Kevin Meredith


People also ask

How do I switch users on vagrant?

one way to do that is you could start a box, copy the home of the vagrant as your user, set proper permissions, create the new user, and package the box.


2 Answers

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

like image 168
Terry Wang Avatar answered Sep 20 '22 17:09

Terry Wang


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.

like image 31
Benny K Avatar answered Sep 17 '22 17:09

Benny K