LoginSignup
4
5

More than 5 years have passed since last update.

VagrantのboxをUbuntu16.04に上げた時にIPを固定出来なかったのを無理やりなんとかした話

Last updated at Posted at 2016-07-29

内容

VagrantでUbuntu16.04を上げようとしたらipアドレスを固定できなかった

この方と同様の例に悩まされた+αを回避した時の記録です

処置

  • IPを固定する

上記の方はifconfigで当ててますが、これはvagrantを再起動すると消えます。
なので、実際のvagrantboxの中の/etc/rc.localにこのifconfigを書き込みます。
こうすれば立ち上がる毎に当てられるようになり、心置きなくvagrant halt を使えます。

cmd
sed -i 's/exit\ 0/ifconfig\ enp0s8\ 192\.168\.33\.11\ \&\&\ exit\ 0/g' /etc/rc.local
  • authorized_keysのパーミッション変更

これもboxの不具合系の様です。
クライアントPC(例えばMac)から、vagrant sshする時には裏で鍵で入っていると思いますが、
vagrant側の/home/vagrant/.ssh/authorized_keys のパーミションがvagrantを再起動すると、何故か0600になっていてvagrant sshが出来なくなります。
なので、これも一行でやっつけます。

cmd
chmod 644 /home/vagrant/.ssh/authorized_keys

Vagrantfileを晒す

以上の処理をかましたVagrantfileを上げておきます。
何かしらの参考になれば幸いです

Vagrantfile
Vagrant.configure(2) do |config|
  # config.vbguest.auto_update = false

  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end

  config.vm.define :svr01 do |server|
    ### determined specific IP.
    server.vm.network "private_network", ip: "192.168.33.11", auto_config: false
    ### vbox name
    server.vm.hostname = 'svr01'
    ### box setting
    server.vm.box = "bento/ubuntu-16.04"
    ### bug fix
    server.vm.provision 'shell', inline: "ifconfig enp0s8 192.168.33.11"
    server.vm.provision 'shell', inline: "sed -i 's/exit\ 0/ifconfig\ enp0s8\ 192\.168\.33\.11/g' /etc/rc.local"
    server.vm.provision 'shell', inline: "echo 'exit 0' >> /etc/rc.local"
    server.vm.provision 'shell', inline: "chmod 644 /home/vagrant/.ssh/authorized_keys"
 end

  config.vm.provider "virtualbox" do |vb|
    ### vagrant hostname
    vb.name = "iganari-web"
    vb.customize ['modifyvm', :id,"--memory", 512]
    vb.customize ['modifyvm', :id,"--natdnshostresolver1", "on"]
    vb.customize ['modifyvm', :id,"--natdnsproxy1", "on"]
  end

  ### settitng of synced_folder
  config.vm.synced_folder "../", "/var/www/html/iganari-web", \
    :create => true, \
    :owner  => 'root', \
    :group  => 'root', \
    :mount_options => ['dmode=755,fmode=755']

  config.vm.provision :shell, :path => "ssh/init.sh"


end

結び

最近はdockerで構築する機会も多くなりましたが、vagrantも十分便利ですよね。
なので、用途に応じて使い分けていきたいものです。

4
5
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
4
5