Linux Administration Cookbook
上QQ阅读APP看书,第一时间看更新

Technical requirements

As introduced in the first chapter, we're going to use Vagrant and VirtualBox for all of our work in this chapter and those going forward. This allows us to quickly provision infrastructure for testing, and saves you the manual job of creating multiple VMs each time.

If you really, really, don't want to use VirtualBox or Vagrant, then you don't have to, and I've tried to keep the examples as generic as possible, but you will probably find it much easier if you do.

I've put together the following Vagrantfile for use in this chapter:

# -*- mode: ruby -*-
# vi: set ft=ruby :

$provisionScript = <<-SCRIPT
sed -i 's#PasswordAuthentication no#PasswordAuthentication yes#g' /etc/ssh/sshd_config
systemctl restart sshd
SCRIPT

Vagrant.configure("2") do |config|
config.vm.provision "shell",
inline: $provisionScript

config.vm.define "centos1" do |centos1|
centos1.vm.box = "centos/7"
centos1.vm.network "private_network", ip: "192.168.33.10"
centos1.vm.hostname = "centos1"
centos1.vm.box_version = "1804.02"
end

config.vm.define "centos2" do |centos2|
centos2.vm.box = "centos/7"
centos2.vm.network "private_network", ip: "192.168.33.11"
centos2.vm.hostname = "centos2"
centos2.vm.box_version = "1804.02"
end

config.vm.define "centos3" do |centos3|
centos3.vm.box = "centos/7"
centos3.vm.network "private_network", ip: "192.168.33.12"
centos3.vm.hostname = "centos3"
centos3.vm.box_version = "1804.02"
end
end
Note something new about this Vagrantfile. We've included a provision step, which runs the code assigned to the variable at the top of the file. In this case, we're making some changes to the SSH configuration of the default CentOS image, so our examples work as we expect. We've put all three VMs on their own private network.

It would be advisable to create a folder called Chapter Two and copy this code into a file called Vagrantfile or if you're using the code from GitHub, navigating into the right folder.

Running vagrant up from inside the folder containing your Vagrantfile should configure two VMs for testing.

Once provisioned, make sure that you can connect to the first by running the following:

$ vagrant ssh centos1
Vagrant is great for testing purposes, but you shouldn't use it in a production environment for deploying machines. Some of the decisions that are made are for ease of use (such as those around the default vagrant user in our image) and as a result, are not best practices for a secure deployment.