LoginSignup
14
15

More than 5 years have passed since last update.

Vagrantで起動したVMにrootでログインする

Last updated at Posted at 2015-03-28

やりたいこと

  • rootユーザで公開鍵認証でログインしたい
  • ~/.vagrant.d/insecure_keyを使いたい
  • vagrant sshでログインしたい

環境

OSX 10.10
Vagrant 1.7.2

うまくいかなかった設定

Vagrantfile
Vagrant.configure(2) do |config|

  config.vm.box = "chef/centos-7.0"
  config.vm.network 'private_network', ip: '192.168.90.10'
  config.cache.scope = :box if Vagrant.has_plugin? 'vagrant-cachier'
  config.ssh.username = 'root'
  config.ssh.password = 'vagrant'
  config.ssh.insert_key = 'false'
end

こんな設定を試しました。ssh.usernameを指定するとrootでログインできるのですが、ssh.insert_keyがデフォルトでtrueなためログインに利用する鍵が指定できませんでした。falseにすると共通の鍵を使うようになりますが、/root/.ssh/以下に公開鍵が入っておらず、/home/vagrant/.sshに入っている状態だったためログインができませんでした。

最終的に

Vagrantfile
Vagrant.configure(2) do |config|

  config.vm.box = "chef/centos-7.0"
  config.vm.network 'private_network', ip: '192.168.90.10'
  config.cache.scope = :box if Vagrant.has_plugin? 'vagrant-cachier'
  config.ssh.username = 'root'
  config.ssh.insert_key = 'false'
  config.vbguest.auto_update = false
  config.vm.provision "shell", inline: <<-SHELL
     sudo cat /home/vagrant/.ssh/authorized_keys >> /root/.ssh/authorized_keys
 SHELL

end

provisionの指定をすることに。初回起動時にパスワード効かれますが、我慢します。。。一度起動したVMにvagrant sshでrootとして入りたいなと思ったことがあったので今回いろいろ試してみました。ssh.insert_keyをtrueにしてもよいなら起動時にパスワード聞かれることもないと思うので、使えるかもしれないですね。(参考に記載)

初回vagrant up時にmountできないエラーが出る時があったので、config.vbguest.auto_update = falseを追記しました。

参考

Vagrant

Vagrant.configure(2) do |config|

  config.vm.box = "chef/centos-7.0"
  config.vm.network 'private_network', ip: '192.168.90.10'
  config.cache.scope = :box if Vagrant.has_plugin? 'vagrant-cachier'
  config.ssh.username = 'root'

14
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
15