vmhost-ubuntu-vbox虚拟主机安装

vmhost-ubuntu-vbox虚拟主机安装

记录时间:2011-02-13

硬件

  • Samsung R540 i3-380M JA05
    • Intel Core i3-380M 2x 2,53GHz
    • 4096MB DDR3 Ram PC3 8500 (1066 MHz)
    • 320GB S-ATA HDD, 5400rpm
    • Ethernet: 10/100/1000-MBit/s
    • Wireless LAN 802.11 B/G/N
    • 15,6″, 39cm, 1366 x 768 Pixel (WXGA TFT)
    • Intel® HD Graphics, ~1023MB
    • 3xUSB2.0, VGA, HDMI, MMC, SDHC, SDXC, SD
    • Webcam u. int. Mikro, DVD Brenner
    • Windows 7 Home Premium 64 Bit

软件

分区

电脑启动时在Samsung恢复系统里选择高级选项:

  • 把硬盘参照以下列表分区
    • sda1: RECOVERY (ntfs): 21.5GB
    • sda2: SYSTEM (ntfs): 104.9MB (B)
    • sda3: WINDOWS (ntfs): 62.7GB
    • 预留给ubuntu系统:220GB
      • sda5: UBUNTU (ext4): 10GB
      • sda7: DATA (ext4): 200GB
      • sda6: swap (swap): 9,6GB

安装ubuntu

放入ubuntu安装光盘,在BIOS里选择从光盘启动:

  • Installer boot menu
    • Language: English
    • <F6>: noacpi=on, nomodeset=on
    • Install Ubuntu Server 10.04 (Rescue mode)
  • Ubuntu installer main menu
    • Choose language: English
    • Location: other, Europe, Germany
    • Detect keyboard layout: yes, + y <ENTER> => de:nodeadkeys
  • Configure the network
    • DHCP: Auto
    • hostname: vmhost-ubuntu-vbox
  • Partition disks
    • Manual, Guided partitioning
      • / 226.2GB (分区应该分为10GB+200GB,见下文)
      • swap 9.6GB
  • Install the base system
  • Set up users and passwords
    • Fullname: admin 2cn.de
    • Username: r
    • Password: <password>
    • Encrypt your home directory: No
  • Configure the package manager
    • HTTP proxy: 无
  • Select and install software: No automatic updates
  • Software selection
    • OpenSSH Server
  • Configuring grub-pc
    • Install the GRUB boot loader to the MBR: Yes
  • Restart
  • 用SystemRescueCD纠正分区
    • gparted resize sda5 to 10GB
    • gparted add sda7 ext4 partition 200GB
      sudo su
      ls -l /dev/disk/by-uuid
vi /etc/fstab

添加以下几行

UUID=<partition-by-uuid> /opt ext4 defaults 0 0
mount -a

设置系统

安装软件,基本设置

设置代理,更新系统

sudo su
export http_proxy=http://www-cache.uni-mannheim.de:3128
apt-get update
apt-get dist-upgrade
apt-get autoremove
apt-get install python-software-properties
apt-get install rcconf
apt-get install p7zip-full
apt-get remove ubuntu-docs
apt-get remove mysql-common
apt-get remove samba-common
apt-get clean
apt-get autoclean
apt-get autoremove
cat /etc/issue
vi /etc/hosts
127.0.0.1 vmhost-ubuntu-vbox

为r和root用户设置top

为r和root添加screen显示设置

vi  ~/.screenrc

添加以下几行

caption always "%{Wb} %H %{Bk}| %{Ck}%-w%50>%{Cb} %n %t %-%+w%<%{- Wk}% |{Bk} | %=%{Wb} %C "

避免意外删除文件等错误

vi /etc/bash.bashrc

添加以下行

alias rm="rm -i"
alias cp="cp -i"
alias mv="mv -i"
alias halt="confirm halt"
alias reboot="confirm reboot"
alias shutdown="confirm shutdown"

SSH设置

用PAM限制SSH用户

vi /etc/pam.d/sshd

在vi里写入

auth required pam_listfile.so item=user sense=allow file=/etc/ssh/ssh.allow onerr=fail

添加允许SSH的用户

vi /etc/ssh/ssh.allow

在vi里写入

r

10分钟后自动关闭空闲的SSH链接

vi /etc/ssh/sshd_config

添加以下几行:

ClientAliveInterval 300
ClientAliveCountMax 2
UseDNS no

Postfix设置

dpkg-reconfigure postfix

Satellite System: 只通过smarthost发送,不接受邮件

vi /etc/postfix/main.cf

添加以下行

smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_password
sender_canonical_maps = hash:/etc/postfix/sender_canonical
message_size_limit = 25600000
vi /etc/postfix/sasl_password

添加以下几行

smtp.gmx.net china-portal@gmx.net:<password>
chmod 600 /etc/postfix/sasl_password
postmap /etc/postfix/sasl_password
vi /etc/postfix/sender_canonical

添加以下几行

r china-portal@gmx.net
root china-portal@gmx.net
vbox china-portal@gmx.net
nagios china-portal@gmx.net
postmap /etc/postfix/sender_canonical
/etc/init.d/postfix restart

安装监测程序

监测网卡流量

apt-get install iftop
apt-get install jnettop

监测电脑硬件配置

apt-get install hwinfo

监测其他电脑数据

apt-get install iotop
apt-get install apachetop

安装Nagios

安装Nagios 3.2.0

apt-get install nagios3
apt-get install nagios-nrpe-plugin
  • Configuring nagios3-cgi
    • nagiosadmin password: <password>
      htpasswd -c /etc/nagios3/htpasswd.users nagiosadmin
      htpasswd -c /etc/nagios3/htpasswd.users nagios

设置权限

vi /etc/nagios3/cgi.cfg

在vi里修改行

authorized_for_read_only=nagios

设置apache2

vi /etc/apache2/apache2.conf

在vi里修改以下几行

ServerName localhost
Timeout 60
MaxKeepAliveRequests 10
StartServers 1
MinSpareServers 1
MaxSpareServers 1
MaxClients 10
vi /etc/apache2/conf.d/security

添加/修改行:

<Directory />
Options -Indexes
AllowOverride None
Order Deny,Allow
Deny from all
</Directory>
ServerTokens Prod
ServerSignature Off
TraceEnable Off
vi /var/www/index.html
<html><body></body></html>
groupadd log
usermod -a -G log vbox
usermod -a -G log www-data
usermod -a -G log r
mkdir /opt/logs
mkdir /opt/logs/apache2
rm -R /var/log/apache2
ln -s /opt/logs/apache2 /var/log/apache2
chown -R www-data.log /opt/logs
chmod -R g+w /opt/logs
chmod -R o-r /opt/logs
chmod -R o-x /opt/logs

配置nagios

vm-ubuntu-build

vi /etc/nagios3/conf.d/vm-ubuntu-build.cfg
define host{
use                     generic-host
host_name               vm-ubuntu-build
alias                   vm-ubuntu-build
address 192.168.178.111
}
define service{
use                             generic-service
host_name                       vm-ubuntu-build
service_description             DNS
check_command                   check_dns!192.168.178.111
}
define service {
use                             generic-service
host_name                       vm-ubuntu-build
service_description             NRPE Disk Usage
check_command                   check_nrpe_1arg!check_all_disks!vm-ubuntu-build
}
define service{
use                             generic-service
host_name                       vm-ubuntu-build
service_description             NRPE Current Users
check_command                   check_nrpe_1arg!check_users!vm-ubuntu-build
}
define service{
use                             generic-service
host_name                       vm-ubuntu-build
service_description             NRPE Zombie Processes
check_command                   check_nrpe_1arg!check_zombie_procs!vm-ubuntu-build
}
define service{
use                             generic-service
host_name                       vm-ubuntu-build
service_description             NRPE Total Processes
check_command                   check_nrpe_1arg!check_total_procs!vm-ubuntu-build
}
/etc/init.d/nagios3 restart

安装VirtualBox

安装VirtualBox4.0.2

add-apt-repository "deb [http://download.virtualbox.org/virtualbox/debian] lucid contrib"
add-apt-repository "deb [http://download.virtualbox.org/virtualbox/debian] lucid non-free"
wget -q- -[http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc]- --O- | sudo apt-key add -
apt-get update
apt-get install libqtcore4
apt-get install libqtgui4
apt-get install libssl0.9.8
apt-get install virtualbox-4.0
apt-get install dkms
wget [http://download.virtualbox.org/virtualbox/4.0.2/Oracle_VM_VirtualBox_Extension_Pack-4.0.2-69518.vbox-extpack]
VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.0.2-69518.vbox-extpack
useradd -m -s /bin/bash vbox
passwd vbox
adduser vbox vboxusers
mkdir /opt/vbox
mkdir /opt/vbox-shared
chown -R vbox.vboxusers /opt/vbox
chown -R vbox.vboxusers /opt/vbox-shared
su - vbox
vi ~.VirtualBox/VirtualBox.xml

把defaultMachineFolder改为/opt/vbox

安装PhpVirtualBox

vi /etc/default/virtualbox
VBOXWEB_USER=vbox
VBOXWEB_HOST=localhost
VBOXWEB_PORT=18083
VBOXWEB_TIMEOUT=60
VBOXWEB_CHECK_INTERVAL=5
VBOXWEB_THREADS=2
VBOXWEB_KEEPALIVE=2
VBOXWEB_LOGFILE=/var/log/virtualbox.log
INSTALL_DIR=/usr/lib/virtualbox
mkdir /var/log/virtualbox
update-rc.d vboxweb-service defaults
apt-get install libapache2-mod-php5
mkdir /opt/phpvirtualbox
cd /opt/phpvirtualbox
wget `wget -q -O - [http://phpvirtualbox.googlecode.com/files/LATEST.txt]` -O phpvirtualbox-latest.zip
7z x phpvirtualbox-latest.zip
ln -s /opt/phpvirtualbox/phpvirtualbox-4.0-4b/ /var/www/vbox
mkdir /opt/phpvirtualbox/conf
vi /opt/phpvirtualbox/conf/config.php
<?php
class phpVBoxConfig {
var $username = 'vbox';
var $password = '<密码>';
var $location = 'http://127.0.0.1:18083/';
var $language = 'en_us';
var $previewUpdateInterval = 600;
var $enableAdvancedConfig = true;
}
ln -s /opt/phpvirtualbox/conf/config.php /opt/phpvirtualbox/phpvirtualbox-4.0-4b/config.php
vi /etc/apache2/sites-available/vbox
<VirtualHost *:80>
Alias /vbox /opt/phpvirtualbox/phpvirtualbox
<Directory /opt/phpvirtualbox/phpvirtualbox>
Options None
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/vbox-error.log
* Possible values include: debug, info, notice, warn, error, crit,
* alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/vbox-access.log combined
</VirtualHost>
a2ensite vbox
chown -R vbox.www-data /opt/phpvirtualbox/
chmod -R o-r /opt/phpvirtualbox
chmod -R o-x /opt/phpvirtualbox
/etc/init.d/apache2 restart
  • 在浏览器里打开http://<vmhost-ubuntu-vbox>/vbox网页
    • 用户admin,密码admin
    • File – Preferences – 修改admin密码

安装vboxMgr

mkdir /opt/vboxmgr
cd /opt/vboxmgr
wget http://vboxmgr.svn.sourceforge.net/viewvc/vboxmgr/trunk/vboxmgr-init
wget http://vboxmgr.svn.sourceforge.net/viewvc/vboxmgr/trunk/vboxmgr
wget http://vboxmgr.svn.sourceforge.net/viewvc/vboxmgr/trunk/autostart.conf.README
chmod 754 /opt/vboxmgr/vboxmgr
ln -s /opt/vboxmgr/vboxmgr /usr/local/bin/vboxmgr
chmod 754 /opt/vboxmgr/vboxmgr-init
ln -s /opt/vboxmgr/vboxmgr-init /etc/init.d/vboxmgr-init
mkdir /opt/vboxmgr/conf
mv /opt/vboxmgr/autostart.conf.README /opt/vboxmgr/conf
ln -s /opt/vboxmgr/conf/ /etc/vboxmgr
chown -R vbox.vboxusers /opt/vboxmgr
update-rc.d vboxmgr-init defaults 99 20
vi /opt/vboxmgr/conf/autostart.conf

添入以下几行

"vm-ubuntu-build" 0
"vm-winxp-work" 0

virtualbox备份

创建备份文件夹

mkdir /opt/backup
chown -R vbox.vbox /opt/backup
mkdir /opt/scripts
chown -R vbox.vbox /opt/scripts
vi /opt/scripts/vbox_backup.sh

添入以下备份脚本

!/bin/bash
BACKUP_TARGET="/opt/backup"
KEEP_DAYS=1
BACKUP_VMS=("vm-ubuntu-fe" "vm-ubuntu-build")
HOST=`hostname`
LAST_BACKUP=`/bin/date +%G%m%d`
AVAILABLE_VMS=`VBoxManage list vms | grep '"' | cut -d'"' -f2 2>/dev/null`
function suspendVM {
VM_STATE=`VBoxManage showvminfo "${CURRENT_VM}" --machinereadable | grep "^\(VM_STATE=\)" | cut -d'"' -f2 2>/dev/null`
if [ "${VM_STATE}" = "running" ]; then
echo "Suspending ${CURRENT_VM} ..."
VBoxManage controlvm ${CURRENT_VM} savestate
fi
}
function backupVM {
vbox_cfg_file=`VBoxManage showvminfo "${CURRENT_VM}" --machinereadable | grep "^\(CfgFile=\)" | cut -d'"' -f2 2>/dev/null`
vbox_vdi_files=`VBoxManage showvminfo "${CURRENT_VM}" --machinereadable | grep ".vdi" | cut -d'"' -f4 2>/dev/null`
mkdir "${BACKUP_TARGET}/${CURRENT_VM}"
echo "Backing up ${CURRENT_VM} ..."
rsync -aP --stats "${vbox_cfg_file}" "${BACKUP_TARGET}/${CURRENT_VM}/"
for current_vdi in ${vbox_vdi_files}; do
rsync -aP --stats "${current_vdi}" "${BACKUP_TARGET}/${CURRENT_VM}/"
done
}
function resumeVM {
if [ "${VM_STATE}" = "running" ]; then
echo "Resuming ${CURRENT_VM} . . ."
VBoxHeadless -s ${CURRENT_VM}
fi
}
function compressBackup {
backup_file="${CURRENT_VM}_${LAST_BACKUP}.tar.gz"
echo "Compressing ${CURRENT_VM} backup ..."
tar -cf "${BACKUP_TARGET}/${backup_file}" -C "${BACKUP_TARGET}/${CURRENT_VM}"
}
function deleteOldBackups {
echo "Deleting older than ${KEEP_DAYS} day(s)"
find "${BACKUP_TARGET}" -name "${CURRENT_VM}_*.tar.gz" -mtime ${KEEP_DAYS} -exec rm -vf {} \;
}
for backup_vm in "${BACKUP_VMS[@]}"; do
for CURRENT_VM in ${AVAILABLE_VMS}; do
if [ "${backup_vm}" = "${CURRENT_VM}" ]; then
echo "Start backup: ${CURRENT_VM} (`date`)"
suspendVM
sleep 3
backupVM
resumeVM
sleep 3
compressBackup
deleteOldBackups
echo "Finished backup: ${CURRENT_VM} (`date`)"
fi
done
done
chmod 750 /opt/scripts/vbox_backup.sh

设置固定IP

vi /etc/network/interfaces

删除以下dhcp的设置

auto eth0
iface eth0 inet dhcp

添加IP设置

auto  eth0
iface eth0 inet static
address 192.168.178.11
netmask 255.255.255.0
network 192.168.178.0
broadcast 192.168.178.255
gateway 192.168.178.1
vi /etc/resolv.conf
search 2cn.de
domain 2cn.de
nameserver 192.168.178.1
nameserver 134.155.96.53
vi /etc/hosts
127.0.0.1 vmhost-ubuntu-vbox
127.0.0.1 vmhost-ubuntu-vbox.2cn.de
192.168.178.111 vm-ubuntu-build
192.168.178.111 vm-ubuntu-build.vm.2cn.de
192.168.178.112 vm-winxp-work
192.168.178.112 vm-winxp-work.vm.2cn.de
/etc/init.d/networking restart

删除不需要的软件包

apt-get remove telnet

卸载dhcp

apt-get remove dhcp-client3

设置grub-reboot

把/etc/default/grub里的GRUB_DEFAULT函数改为saved

GRUB_DEFAULT=saved
grub-set-default 0
update-grub
vi /etc/bash.bashrc

添加使用Windows7重启指令

alias reboot2win="grub-reboot \"Windows 7 (loader) (on /dev/sda2)\" && reboot"

TODOs

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s