dogi / ole--vagrant-community

0 stars 19 forks source link

set other then standard password #2

Closed dogi closed 8 years ago

dogi commented 8 years ago

http://stackoverflow.com/questions/22575261/vagrant-stuck-connection-timeout-retrying

dogi commented 8 years ago

Change Vagrantfile so that the reverse of standard password vagrant is the new password.

xinglunxu commented 8 years ago

So, you want to insert this inside Vagrantfile? config.ssh.password = "tnargav" Not sure if I get your point correctly.

dogi commented 8 years ago

I am on some communities exposing ports to the big internet ;)

@xinglunxu : where exactly in the "Vagrantfile" to I have to put that in ... context is missing ;(

xinglunxu commented 8 years ago

Here is the reference to how to configure the ssh connection from vagrant to the vm To change the ssh password, we can just put the setting config.ssh.password = "tnargav" anywhere under Vagrant.configure(2) do |config|

dogi commented 8 years ago

@xinglunxu thx

you have the go for it ;)

xinglunxu commented 8 years ago

@dogi So After I inserted this line and tried to reload the machine, an error like this happened.

screen shot 2016-07-06 at 7 03 15 pm

I still haven't figure out how to solve it. But before that, may I ask why are we changing the password of the vagrant ssh? What is the problem we are trying to solve in this issue post? I looked at the link from https://github.com/dogi/ole--vagrant-community/issues/2#issue-157220242 But I still didn't get the problem. :sweat_smile:

xinglunxu commented 8 years ago

Yo, I finally found a way to change the password for ssh by script. But I am almost positive that there is a better way to do this.

So previously, I tried to add config.ssh.password = "tnargav" into Vagranfile to change the ssh password for vagrant. However, all it does is just change the password that Vagrant uses to ssh the vm. The ssh password of user vagrant will still be unchanged in the vm. That is why there is a connection problem in my previous post.

IMHO, the password setting for ssh is included in the "ole/jessie64" box. Since I cannot find a good way to configure it in Vagrantfile, I decided to change the ssh password after the vm is set up and run using a script file. It looks like this:

#!/bin/bash

VM_ID=`vagrant global-status | grep community | cut -d' ' -f1`
KEYPATH=`vagrant ssh-config ${VM_ID} | grep "IdentityFile" | cut -d' ' -f4`
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

ssh -i ${KEYPATH} vagrant@127.0.0.1 -p 2222 'bash -s' < ${DIR}/command.sh 

This script ssh to the vm and run a local script to change the password.

The local script is like this.

#!/bin/bash

echo -e "<currentpassword>\n<newpassword>\n<newpassword>" | passwd vagrant

Since I changed the password after the vm is set tup, this solution will be system dependent.

dogi commented 8 years ago

@xinglunxu awesome :)

will use this script for the ole template creation ... https://atlas.hashicorp.com/ole/boxes/jessie64 so that later we can just use the easy way of changing the password

xinglunxu commented 8 years ago

Ok, I will modify it a little bit more later. Now it assume that the vagrant is using port 2222 for ssh. I should get it from the "vagrant ssh-config" command instead. Also, it is assuming that the name of the vm is "community". I should make it an argument for the script.

xinglunxu commented 8 years ago

As promised, here is the improved version of the script The comments inside the script already describe the behavior of it in detail

#!/bin/bash

#Run this script with the machine name to which you want to change the password. 
#Run it with "community" for development environment and "ole" for production environment
#Example: "sh changepassword.sh community"  

#parse the name of the vm machine from command line argument
VM_NAME=$1

VM_ID=`vagrant global-status | grep ${VM_NAME} | cut -d' ' -f1`
KEYPATH=`vagrant ssh-config ${VM_ID} | grep "IdentityFile" | cut -d' ' -f4`
PORT=`vagrant ssh-config ${VM_ID} | grep "Port" | cut -d' ' -f4`

#connect to vm and change password from "vagrant" to "openlearning"  replace "openlearning" with your customized password
ssh -i ${KEYPATH} vagrant@127.0.0.1 -p ${PORT} "echo -e \"vagrant\nopenlearning\nopenlearning\" | passwd vagrant"

I combined the previous two scripts into one. Also the the script now take the name of target vm as command line argument. The port number is also no longer hardcoded. It is parsed from "vagrant ssh-config".

Also, @dogi I remember you mentioned something about the vagrant box template in the hangout today. I couldn't hear you clearly so can you write it down here? Thanks!

BTW, it will be good if @EmilyLarkin or some other mac user friends can test it.

The testing process is as easy as following

  1. Run your "community" vagrant machine.
  2. ssh to the "community" machine with the password "vagrant". Make sure it works then exit the machine.
  3. Run the script like this sh changepassword.sh community You can just copy and paste the code above then run it from any directory.
  4. ssh to the "community" machine with the new password "openlearning". See if it works again. If successfully connect to vm again, then this script works.

To manually ssh to the "community" machine using a password, type the following command ssh vagrant@127.0.0.1 -p 2222 then type the current password when prompt. Please comment here or gitter if something go wrong or if there is any question. Thanks!

sente commented 8 years ago

Is there a fork or branch of the entire repo? I have a mac and I'd like to give it a shot.

xinglunxu commented 8 years ago

@sente Hi, I haven't make a pull request yet, but I think you can just copy and paste the code into any directory and run it as in the post. It is designed to run in any directory.

sente commented 8 years ago

I don't currently use vagrant/docker on my mac so I'd be running the whole shebang from start to finish. It's good practice to use forks/branches anyways, in my opinion.

xinglunxu commented 8 years ago

@sente This is my own fork of the ole--vagrant--community https://github.com/xinglunxu/ole--vagrant-community And I just pushed the changepassword.sh script to it. It is under the macosx directory. There is only one branch there. Is this something you are looking for?

Hulmani commented 8 years ago

@xinglunxu I tried and yes it works. Cheers

dogi commented 8 years ago

done with update -https://github.com/dogi/ole--vagrant-community/commit/defdf1dc18f3f8190faf462e532ebfd330fc1d84 which updates to version 0.1.4 of ole/jessie64

the change happens in https://github.com/dogi/ole--vagrant-template/commit/dd489c3b06088f7c92b6e65188ba40d8de9cee3c