Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow vagrant ssh to login with different usernames #1295

Closed
ekohl opened this issue Jan 4, 2013 · 17 comments

Comments

Projects
None yet
10 participants
@ekohl
Copy link

commented Jan 4, 2013

In my environment I want to use multiple users inside the VM and while vagrant ssh is a very useful concept, I think it could be even better when you add the option log in using different usernames. A parameter for a username (-u) comes to mind, but fixing vagrant ssh -- -l <otheruser> (which doesn't work in 1.0.6) might be a different solution. The latter that wouldn't clutter the UI with extra parameters while still allowing power users to do what they want.

@d-mart

This comment has been minimized.

Copy link

commented May 15, 2013

I would also find this useful. I like using vagrant for deploy simulations but of course we don't deploy as 'vagrant'.

@mitchellh

This comment has been minimized.

Copy link
Member

commented Jul 17, 2013

The "-l" flag now works fine with vagrant ssh.

@mitchellh mitchellh closed this Jul 17, 2013

@kevinmeredith

This comment has been minimized.

Copy link

commented Mar 25, 2014

Is it possible to ssh onto a vagrant box with a different user-name without using vagrant ssh?

For example, what do I need to do if I'd like to spin up a VM and log onto it with kevin/kevin rather than vagrant/vagrant?

I posted a question on StackOverflow - http://stackoverflow.com/questions/22643177/ssh-onto-vagrant-box-with-different-username

@kikitux

This comment has been minimized.

Copy link
Collaborator

commented Mar 25, 2014

have you tried what is on the manual?

http://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html

config.ssh.username = "kevin"
config.ssh.password = "kevin"

If you tried, and failed, then please provide more information, and your
Vagrantile + logs.

config.ssh.username - This sets the username that Vagrant will SSH as by
default. Providers are free to override this if they detect a more
appropriate user. By default this is "vagrant," since that is what most

public boxes are made as.

config.ssh.password - This sets a password that Vagrant will use to
authenticate the SSH user. Note that Vagrant recommends you use key-based
authentiation rather than a password

Alvaro.

On Wed, Mar 26, 2014 at 7:29 AM, kman007us notifications@github.com wrote:

Is it possible to ssh onto a vagrant box without using vagrant ssh?

For example, what do I need to do if I'd like to spin up a VM and log onto
it with kevin/kevin rather than vagrant/vagrant?

I posted a question on StackOverflow -
http://stackoverflow.com/questions/22643177/ssh-onto-vagrant-box-with-different-username

Reply to this email directly or view it on GitHubhttps://github.com//issues/1295#issuecomment-38602470
.

@ekohl

This comment has been minimized.

Copy link
Author

commented Mar 25, 2014

@kman007us to login without vagrant ssh, you can use vagrant ssh-config to spit out the SSH config and add that to your ~/.ssh/config but you should be sure that the IP doesn't change. To log in as a different user, see what @kikitux wrote.

If I read your stackoverflow post, I think you should ensure the user exists in your base box as is normally done for the user vagrant. I don't think this is possible through provisioning because vagrant uses SSH and the configured user to provision.

Given a provider that implements cloud-init and a base image that supports it, in theory it should be possible through the Vagrantfile. I'm unaware of a provider that does this, but haven't really looked for it either.

@kikitux

This comment has been minimized.

Copy link
Collaborator

commented Mar 25, 2014

yes, you need to provide more information like if the user kevin/kevin
exist or no.

youmay hack a bit the initial provision of the box to create users and from
the 2nd boot going on, use other user, but is a bit of not doing what
romans do.. that vagrant use vagrant user to startup and setup the box

if inside the vm you need run scripts/software as kevin user, you can use
the shell provisioner to 'sudo su - kevin -c "command as kevin" ', for
example

so just provide more information.

On Wed, Mar 26, 2014 at 8:53 AM, Ewoud Kohl van Wijngaarden <
notifications@github.com> wrote:

@kman007us https://github.com/kman007us to login without vagrant ssh,
you can use vagrant ssh-config to spit out the SSH config and add that to
your ~/.ssh/config but you should be sure that the IP doesn't change. To
log in as a different user, see what @kikitux https://github.com/kikituxwrote.

If I read your stackoverflow post, I think you should ensure the user
exists in your base box as is normally done for the user vagrant. I don't
think this is possible through provisioning because vagrant uses SSH and
the configured user to provision.

Given a provider that implements cloud-inithttp://cloudinit.readthedocs.org/en/latest/and a base image that supports it, in theory it should be possible through
the Vagrantfile. I'm unaware of a provider that does this, but haven't
really looked for it either.

Reply to this email directly or view it on GitHubhttps://github.com//issues/1295#issuecomment-38612248
.

@kevinmeredith

This comment has been minimized.

Copy link

commented Mar 25, 2014

@kikitux - I added the following to my Vagrantfile:

  config.ssh.username = "kevin"
  config.ssh.password = "kevin"

Then, the following error shows up after running vagrant reload:

There are errors in the configuration of this machine. Please fix
the following errors and try again:

SSH:
* The following settings shouldn't exist: password

Edit I added the kevin user, however I still got the above error after running vagrant reload.

I'm trying to set up the private_key per @ekohl's and @kikitux's posts.

@ekohl

This comment has been minimized.

Copy link
Author

commented Mar 25, 2014

Vagrant authenticates through keys. Also, we already told you that you need to ensure the user exists and examples have been given. I think that this question is unrelated to the original issue, which has been solved for some time.

@kikitux

This comment has been minimized.

Copy link
Collaborator

commented Mar 26, 2014

@kman007us

  1. Why you want to use kevin/kevin
  2. Why you can't use Vagrant?

On Wed, Mar 26, 2014 at 10:08 AM, Ewoud Kohl van Wijngaarden <
notifications@github.com> wrote:

Vagrant authenticates through keyshttps://github.com/mitchellh/vagrant/tree/master/keys.
Also, we already told you that you need to ensure the user exists and
examples have been given. I think that this question is unrelated to the
original issue, which has been solved for some time.

Reply to this email directly or view it on GitHubhttps://github.com//issues/1295#issuecomment-38620921
.

@kevinmeredith

This comment has been minimized.

Copy link

commented Apr 8, 2014

Hey @kikitux. Sorry for the delay.

1 - I'm curious how to do it
2 - I could :), but I'm still curious

@ekohl - you're right. My mistake for failing to read your note & apply your advice before posting a follow-up question.

@kikitux

This comment has been minimized.

Copy link
Collaborator

commented Apr 14, 2014

My Friend, You need to practice and report back when it fail, since seems you haven't tried on your own.

First, let me tell you that works, here I created a box, without Vagrant user, but with kevin user and the vagrant key:

alvaros-mini:kevin alvarom$ vi Vagrantfile
alvaros-mini:kevin alvarom$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2202.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2202 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2202
default: SSH username: kevin
default: SSH auth method: private key
default: Error: Connection timout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => /Volumes/1tb/Dropbox/vagrant_mini/kevin
alvaros-mini:kevin alvarom$ 

Minimal vagrant file looks like:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "oracle65-kevin"
  config.ssh.username = "kevin"
end 

Follow the instructions from here:
http://docs.vagrantup.com/v2/boxes/base.html

The user you want to use, MUST exits, otherwise, it won't work. This was said above at some point.

So, you create a vm, create a kevin user, test it works, package it, test it with vagrant.

It will work, just try to do it, and report back what fails.

If you need more information on what steps you should do it, the same, write down what you think you will do it, and we can tell you if looks ok or not, but if I read this lengthy issue, is not clear yet what you have tried what failes, since it should work, and as I have shown you, it works.

Alvaro.

@Rito1901

This comment has been minimized.

Copy link

commented Jul 12, 2014

Hi There ,
Can someone Help in the below :
I am trying to configure a VM without the default user. However, though i see it gets booted up after sometime, the pre-configure script is not working. ( this works fine if i remove the ssh username ).
reading the above post - where do the user must exist ?
can you tell me where i am going wrong :
My Vagrant file :
Vagrant.configure("2") do |config|

config.vm.box = "hashicorp/precise32"
config.ssh.username = "produser"
config.vm.network "private_network", ip: "192.168.100.100"
config.vm.provider "virtualbox" do |vb|
     vb.customize ["modifyvm", :id, "--memory", "512"]
end
 config.vm.provision :shell, :path => "bootstrap.sh"

config.vm.network :forwarded_port, host: 8000, guest: 80

end

doing a vagrant up i am getting the below output :

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Setting the name of the VM: VagrantEnv_default_1405161908450_95115
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 80 => 8000 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: produser
default: SSH auth method: private key
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...

@canausa

This comment has been minimized.

Copy link
Contributor

commented Jul 12, 2014

There are a few things going on here.

First vagrant assumes the user you are trying to login as is already baked
into the image. If your bootstrap file is creating "produser" then it will
never login and finish provisioning. You are using the hashicorp/precise
box which only has the user "vagrant".

Secondly if you are insistent on using "produser" you will need to build
your own box with the produser already configured.

Third, vagrant assumes that what ever user you are login in as has the
default vagrant ssh key. If your produser does not have that key setup you
will need to tell vagrant how to login either with a different key or
password.

Finally I recommend you do not use vagrant as a production tool and limit
it to a development / testing tool.

-Terrance
On Jul 12, 2014 6:52 AM, "Rito1901" notifications@github.com wrote:

Hi There ,
Can someone Help in the below :
I am trying to configure a VM without the default user. However, though i
see it gets booted up after sometime, the pre-configure script is not
working. ( this works fine if i remove the ssh username ).
reading the above post - where do the user must exist ?
can you tell me where i am going wrong :
My Vagrant file :
Vagrant.configure("2") do |config|

config.vm.box = "hashicorp/precise32"
config.ssh.username = "produser"
config.vm.network "private_network", ip: "192.168.100.100"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "512"]
end
config.vm.provision :shell, :path => "bootstrap.sh"

config.vm.network :forwarded_port, host: 8000, guest: 80

end

doing a vagrant up i am getting the below output :

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Setting the name of the VM:
VagrantEnv_default_1405161908450_95115
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 80 => 8000 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: produser
default: SSH auth method: private key
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...


Reply to this email directly or view it on GitHub
#1295 (comment).

@Rito1901

This comment has been minimized.

Copy link

commented Jul 12, 2014

Thanks a lot Canausa. This helps.
I changed it to vagrant . It runs fine. produser id i used just to highlight username. We are not using it for Prod instance.

@WangWenchao

This comment has been minimized.

Copy link

commented Dec 23, 2015

https://docs.vagrantup.com/v2/vagrantfile/ssh_settings.html
config the ssh username and password and insert_key to be true could resolve your problem

@dobando-dev

This comment has been minimized.

Copy link

commented Jan 25, 2016

vagrant ssh -p

@anthony-malkoun-intrepid

This comment has been minimized.

Copy link

commented Jul 6, 2017

This helped me, posting here as Google search is taking me to this link which references this issue comment

Basically as the Vagrantfile is Ruby you can do:

VAGRANT_COMMAND = ARGV[0]

Vagrant.configure("2") do |config|
  if VAGRANT_COMMAND == "ssh"
      config.ssh.username = 'other_username'
  end

  ...
end

Thanks to danielfrg for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.