在 virtualbox 中使用 CentOs 6 & CentOS 7

1. 安裝 增強功能 virtualbox Additions工具包

1.1. 安裝步驟

  • “增強功能”要編譯安裝,so先安裝 build essential : yum groupinstall "Development Tools"
  • 菜單: 設備 》安裝增強功能,會掛載安裝光盤
  • 執行光盤下的 /media/VBOXADDITIONS_5.1.2_108956/VBoxLinuxAdditions.run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
yum groupinstall "Development Tools"
yum -y install kernel-devel kernel-headers binutils
yum update kernel

# 注意 `uname -r` 不一定是正確路徑名, 直接去 /usr/src/kernels 看看
KERN_DIR=/usr/src/kernels/`uname -r`

# 檢查下 kernel-devel 目錄是否存在
ll $KERN_DIR
# 如果不存在,重新檢查下
ll /usr/src/kernels/

export KERN_DIR

cd /media/VirtualBoxGuestAdditions
./VBoxLinuxAdditions.run


# 安裝過程報錯:vboxadd + "modprobe vboxguest failed"
# 不用管,重啓之後再執行 ./VBoxLinuxAdditions.run
  • 正確安裝的提示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost VBOXADDITIONS_5.1.18_114002]# ./VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.18 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.1.18 of VirtualBox Guest Additions...
vboxadd.sh: Stopping VirtualBox Additions.
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Building Guest Additions kernel modules.
Failed to set up service vboxadd, please check the log file
/var/log/VBoxGuestAdditions.log for details.

[root@localhost VBOXADDITIONS_5.1.18_114002]# rpm -ql kernel-devel-2.6.32-696.el6.i686 | less

[root@localhost VBOXADDITIONS_5.1.18_114002]# export KERN_DIR

[root@localhost VBOXADDITIONS_5.1.18_114002]# ./VBoxLinuxAdditions.run 
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.18 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Removing installed version 5.1.18 of VirtualBox Guest Additions...
vboxadd.sh: Stopping VirtualBox Additions.
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Building Guest Additions kernel modules.
vboxadd.sh: Starting the VirtualBox Guest Additions.

You may need to restart the Window System (or just restart the guest system)
to enable the Guest Additions.

1.2. Trouble shooting

1.2.1. 日誌位置

/var/log/VBoxGuestAdditions/
/var/log/vboxadd-install.log

1.2.2. yum 無法使用:Anthor app is currently holding the yum lock

  • 錯誤關鍵字
    1
    2
    Existing lock /var/run/yum.pid
    Anthor app is currently holding the yum lock
    
  • 解決辦法

    這個問題在 CentOS7 會出現,由 PackageKit 引起。

    1
    2
    3
    4
    systemctl stop packagekit
    systemctl disable packagekit
    
    yum remove PackageKit
    

1.2.3. 問題:找不到 kernel source

不知道爲啥找不到,按照錯誤日誌的提示,設置 KERN_DIR 變量,告訴它哪兒有

編輯 /etc/profile (見如下代碼塊)後,重啓。

1
2
3
## ADD KERN_DIR to help vboxadd find kernel source
KERN_DIR=/usr/src/kernels/2.6.32-642.6.1.el6.i686
export KERN_DIR

重新執行 VBoxLinuxAdditions.run

2. 設置 host 和 vm 之間剪貼板共享

設置 》常規 》 高級 》 共享粘貼板:雙向

3. 設置網卡

3.1. CentOS 7 啓動後網絡默認關閉

  • 手動打開
    通過圖形界面 Applications - System Tools - Network 打開 Network Manager

  • 設置開機自動打開

    1. 修改配置文件
      1
      2
       vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
       # enp0s3 是通過  ifconfig -a 獲得虛擬網卡接口的名稱
      
    2. 改爲 ONBOOT=yes
    3. 重啓

3.2. 雙網卡模式

即能訪問網絡、又能固定IP連接VM 的sshd。需要設置2個網卡。

3.2.1. VirutalBox 啓用2個網卡 (設置 - 網絡)

  • 【網卡1】網絡地址轉換NAT:VM通過該網卡訪問外網。
  • 【網卡2】僅主機(host-only)網絡 : 設置固定IP,來進行ssh連接。
    1. 界面名稱:VirtualBox Host-Only Ehternet Adapter
    2. 控制芯片:Intel PRO/1000 MT 桌面(82540EM)
    3. 混雜模式:全部允許
    4. MAC 地址,刷新一下

3.2.2. CentOS中設置

圖形界面方式進入CentOS。ifconfig -a 查看對應的網卡名稱和信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# ifconfig -a
eth1      Link encap:Ethernet  HWaddr 08:00:27:04:40:74  
          inet addr:192.168.56.102  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe04:4074/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:80 errors:0 dropped:0 overruns:0 frame:0
          TX packets:86 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:26530 (25.9 KiB)  TX bytes:16830 (16.4 KiB)

eth2      Link encap:Ethernet  HWaddr 08:00:27:48:07:46  
          inet addr:10.0.3.15  Bcast:10.0.3.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe48:746/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:322 errors:0 dropped:0 overruns:0 frame:0
          TX packets:356 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:147746 (144.2 KiB)  TX bytes:36617 (35.7 KiB)
在 host-only 網卡上 設置固定IP
  1. 進入圖形界面
  2. 系統菜單: System > Preferences > Network Connections
  3. 對 host-only 網卡例如 Auto eth1 進行修改,選擇 Edit…
  4. 選擇tab頁:IPv4 Settings
  5. Method: Manual
  6. 添加 Address
    • Address : 192.168.56.101
    • Netmask : 255.255.255.0
    • Geteway : 0.0.0.0
    • 注意:網關必須寫 0.0.0.0 。寫 host地址(例如,196.168.56.1 )時,ssh客戶端連上VM會很慢,VM也可能使用這個網卡訪問外網,導致不能上網。
  7. 保存後,重啓。

4. ssh 連接很慢

參考: http://ask.xmodulo.com/fix-slow-ssh-login-issue-linux.html

1
2
3
4
5
6
7
vi /etc/ssh/sshd_config

# To disable GSSAPI authentication on an SSH server
GSSAPIAuthentication no

# add this line
UseDNS no

5. CentOS - 7

5.1. 設置啓動模式(runlevel)

CentOS7 不在使用 inittab ,改爲 systemd

1
2
3
4
5
6
7
8
9
10
11
12
# To view current default target, run:
systemctl get-default

# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5

# 圖形界面
systemctl set-default graphical.target

# 多用戶文字界面
systemctl set-default multi-user.target

5.2. systemctl 啓動/關閉服務

1
2
3
4
systemctl status sshd.service
systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章