一、相關(guān)軟件:
折騰過(guò)程中需要用到以下內(nèi)容:
操作系統(tǒng)centos7的鏡像,本次使用CentOS-7-x86_64-Minimal-1708.iso
虛擬機(jī)軟件virtualbox, 本次使用版本VirtualBox 5.2.8
vagrant是一個(gè)基于Ruby的工具,用于創(chuàng)建和部署虛擬化開(kāi)發(fā)環(huán)境, 本次使用版本vagrant_2.0.2_x86_64.msi
二、 虛擬機(jī)配置
- 內(nèi)存:2G
- 硬盤(pán):40G 動(dòng)態(tài)分配
- 聲音、USB: 禁用
- 網(wǎng)絡(luò): 網(wǎng)絡(luò)地址轉(zhuǎn)換(NAT), 端口轉(zhuǎn)發(fā)-> ssh 宿主機(jī):127.0.0.1 222 => 虛擬機(jī): (空) 22

三、安裝centos7
- 安裝centos 7
- 默認(rèn)分區(qū)
- 設(shè)置root密碼為vagrant
四、配置centos
安裝后重啟,進(jìn)入centos配置
1. 更改網(wǎng)絡(luò)配置,設(shè)置開(kāi)機(jī)啟動(dòng)
將ifcfg-enp0s3的ONBOOT=no,改為ONBOOT=yes
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
以前是傳統(tǒng)的命名eth0、eth1等, centos7里網(wǎng)卡代號(hào)是enp0s3,或者其他方式如enp4s0,如果覺(jué)得ifcfg-enp0s3不習(xí)慣,也可以改成ifcfg-eth0:
更改grub
# vi /etc/default/grub
在GRUB_CMDLINE_LINUX的最后,加上 net.ifnames=0 biosdevname=0 的參數(shù)
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet net.ifnames=0 biosdevname=0"
# grub2-mkconfig -o /boot/grub2/grub.cfg
# mv /etc/sysconfig/network-scripts/ifcfg-enp0s3 /etc/sysconfig/network-scripts/ifcfg-eth0
# reboot
2. 安裝ssh-server
a. 安裝openssh-server
# yum -y install openssh-server
# systemctl enable sshd.service
# systemctl start sshd.service
# systemctl status sshd.service
b. 配置ssh, 沒(méi)配的話連不上
備份/etc/ssh/sshd_config
# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
c. 修改sshd_config的GSSAPIAuthentication和UseDNS
# sed -i -e 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_ config
# sed -i -e 's/^#UseDNS yes/UseDNS no/' /etc/ssh/sshd_ config
# systemctl restart sshd.service
# systemctl status sshd.service
3. 更改成163的yum源
非必須步驟,yum安裝慢了可以更改yum源
首先,安裝wget,然后,備份CentOS-Base.repo, 下載并保存163的repo成CentOS-Base.repo,然后生成緩存。
# yum -y install wget
# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
# mv CentOS7-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo
# yum clean all
# yum makecache
- 配置selinux
將SELinux配置成permissive模式
# sed -i -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
5. 關(guān)閉防火墻
centos7中默認(rèn)用的是firewalld做防火墻,iptables如果需要的話要自己裝,開(kāi)發(fā)的box可以直接禁用掉firewalld。
# systemctl stop firewalld.service
# systemctl disable firewalld.service
6. 安裝ntpd服務(wù)及其他服務(wù)
ntpd主要用來(lái)同步時(shí)間, ntpq -p可以驗(yàn)證是否能夠同步。
centos7中,默認(rèn)沒(méi)有ifconfig, netstat等工具,可以安裝net-tools相關(guān)工具。
# yum install -y openssh-clients nano ntp net-tools*
# systemctl enable ntpd.service
# systemctl stop ntpd.service
# ntpdate cn.ntp.org.cn
# systemctl start ntpd.service
# ntpq -p
7. 創(chuàng)建用戶vagrant
# useradd vagrant
# passwd vagrant
# groupadd admin
# usermod -G admin vagrant
8. 配置sudoers
將vagrant用戶添加到/etc/sudoers中, 免密登錄
# echo 'vagrant ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
注: 從centos6.8開(kāi)始,沒(méi)有Defaults requiretty的配置項(xiàng)目不需要
sed -i 's/^(Defaults.*requiretty)/#\1/' /etc/sudoers
9. 安裝virtualbox additions
首先在虛擬機(jī)上找到設(shè)備的菜單, 設(shè)備=>安裝增強(qiáng)功能,然后掛載cdrom,執(zhí)行安裝腳本
# yum install gcc bzip2 make kernel-devel-`uname -r` perl
# mkdir /home/vbox
# mount -t auto /dev/cdrom /home/vbox
# cd /home/vbox
# sh ./VBoxLinuxAdditions.run
10. 添加vagrant's public key
# su vagrant
$ mkdir -m 0700 -p /home/vagrant/.ssh
$ curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub >> /home/vagrant/.ssh/authorized_keys
$ chmod 0600 /home/vagrant/.ssh/authorized_keys
11. 清理centos7
切換回root,然后清理緩存,清理臨時(shí)文件,清理命令歷史,關(guān)閉虛擬機(jī)
# yum clean all
# rm -rf /var/cache/yum
# rm -rf /tmp/*
# rm -f /var/log/wtmp /var/log/btmp
# history -c
# shutdown -h now
五、 vagrant創(chuàng)建box
創(chuàng)建一個(gè)目錄,然后打開(kāi)命令行,打包虛擬機(jī)成box,--base的參數(shù)必須是安裝的centos7的虛擬機(jī)名稱,本次是centos7-base。
$ vagrant package --output centos7-base.box --base centos7-base
六、測(cè)試box
首先將生成的box添加到box list中, 然后用vagrant init當(dāng)前目錄, vagrant up拉起虛擬機(jī),完成后,vagrant ssh鏈接進(jìn)入虛擬機(jī)就可以操作了。
$ vagrant box add centos7-base centos7-base.box
$ vagrant box list
$ vagrant init centos7-base
$ vagrant up
$ vagrant ssh