developer tip

기본적으로 루트로 방랑자 로그인

optionbox 2020. 9. 15. 07:40
반응형

기본적으로 루트로 방랑자 로그인


문제점 : 자주 상자에 입력하는 첫 번째 명령은 su -.

질문 : vagrant ssh기본적으로 루트 사용자를 사용하려면 어떻게해야 합니까?

버전 : vagrant 1.6.5


이것은 유용합니다.

sudo passwd root

먼저 vagrant에서 루트 암호를 설정해야하는 필요성에 사로 잡힌 사람을 위해


솔루션 :
다음을 추가하십시오 Vagrantfile.

config.ssh.username = 'root'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'

때 당신이 vagrant ssh이제부터, 당신은로 로그인합니다 root다음을 기대한다 :

==> 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!

2015 년 6 월 23 일 업데이트 : 1.7.2 버전에서도 작동합니다. 키 지정 보안은 1.7.0 이후 개선되었습니다. 이 기술은 알려진 개인 키를 사용하는 이전 방법으로 다시 대체됩니다. 이 솔루션은 게시 전에 적절한 보안 조치를 취하지 않고 공개적으로 액세스 할 수있는 상자에 사용하기위한 것이 아닙니다.

참고:


이것은 ubuntu / trusty64 상자에있는 경우 작동합니다.

vagrant ssh

우분투 상자에 들어가면 :

sudo su

이제 루트 사용자입니다. 아래와 같이 루트 비밀번호를 업데이트 할 수 있습니다.

sudo -i
passwd

이제 파일에서 아래 줄을 편집하십시오. /etc/ssh/sshd_config

PermitRootLogin yes

또한 자신 만의 대체 사용자 이름을 만드는 것이 편리합니다.

adduser johndoe

암호를 요청할 때까지 기다리십시오.


Vagrantfile아래와 같은 경우 :

config.ssh.username = 'root'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'

그러나 vagrant는 여전히 루트 암호를 묻습니다. 사용했던 기본 상자가 루트 로그인을 허용하도록 구성되지 않았을 가능성이 높습니다.


예를 들어, offical 한 ubuntu14.04 상자가 설정하지 마십시오 PermitRootLogin yes/etc/ssh/sshd_config.

따라서 상자가 루트 기본값으로 로그인 할 수 있도록하려면 (Vagrantfile 만, 더 이상 작업하지 않음) 다음을 수행해야합니다.

  1. 사용자 이름으로 VM 설정 vagrant(루트 제외)

  2. sshd 구성 파일에 로그인하고 편집합니다.

    우분투 : 편집 /etc/ssh/sshd_config, 설정PermitRootLogin yes

    기타 : ....

    (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.


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

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

PermitRootLogin yes

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

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.


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

vagrant destroy
vagrant up

Please add this to vagrant file:

config.ssh.username = 'vagrant'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'

참고URL : https://stackoverflow.com/questions/25758737/vagrant-login-as-root-by-default

반응형