VirtualBox安装Ubuntu和初始化

VirtualBox 是一款开源虚拟机软件, 号称是最强的免费虚拟机软件. 由Oracle出品. 以下简称VMBox. 相对VMware来说具有小巧, 快速, 占用资源少, 免费等优势. 以下介绍如何在VirtualBox上安装Ubuntu.

安装篇

首先去官网下载VMBox和Ubuntu. 安装完VMBox后选择新建.

  1. 在新建的界面里选择专家模式. 填入名称, 选择内存, 虚拟硬盘选择创建. 如下图
  2. 创建虚拟硬盘的参数如下, 然后点创建
  3. 然后就看到VMBox主界面左侧就有刚才创建的虚拟机
  4. 点击上面的设置按钮, 选择网络. 连接方式选择桥接网卡, 界面名称选择你主机的上网网卡.点击ok后回到主界面
  5. 然后点启动. 启动盘选择下载的ubuntu系统的iso文件. 然后点启动
  6. 选择English
  7. 选择第一项
  8. 继续选择English
  9. 时区选择Hong Kong
  10. 键盘配置选择no就可以了
  11. 选择美式键盘
  12. 继续选择English(US)
  13. 然后等一会, 进入主机配置, 名字就是主机名字.
  14. 配置用户名, 输入你自己的.
  15. 输入密码.
  16. 我输入的是弱密码, 自己玩无所谓了, 点yes
  17. 加密home目录选no
  18. 确认时区, 选择yes
  19. 选择第二项
  20. 直接选择就行了
  21. 选择yes
  22. 选Continue
  23. 选择yes
  24. 留空就行了
  25. 然后等一会. 进去更新设置, 这里个人喜好, 我选择不自动更新
  26. 这里选择倒数一, 三两项, 记得用空格选择后再按回车. 这里安装会久一点, 包需要从网络下载
  27. 选择yes
  28. 然后选择Continue就行了, 完成安装
  29. 这里用你刚才输入的username和password登录就行了

初始化篇

设置网络

刚才在设置界面-网络里我们选择了桥接网络, 所以虚拟机可以上网的同时和你的主机也可以互通.
关于网络模式说明如下:
|模式|说明||
|--|||
|网络地址转换模式(NAT)|主机可以上网, 虚拟机也可以上. 虚拟机可以ping主机, 主机不能ping虚拟机. 虚拟机之间也不能互ping|
|桥接网络|主机可以上网, 虚拟机也可以. 主机和虚拟机可以互ping|
|内网模式|虚拟机不能上网, 虚拟机之间可以互ping. 主机和虚拟机不能互ping|
|主机模式|虚拟机不能上网, 虚拟机可以互ping. 主机和虚拟机可以互ping|

根据需要设置.

设置IP和DNS

我这边虚拟机是用来作为服务器用途的. 所以需要配置静态IP. 在虚拟机中运行:

sudo vim /etc/network/interfaces

输入刚才设置密码(凡是sudo执行的命令都要输入root密码), 然后修改配置如下:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
# iface enp0s3 inet dhcp
iface enp0s3 inet static
address 192.168.0.104
netmask 255.255.255.0
gateway 192.168.0.1

主要是把dhcp改成static. 其中enp0s3是我网卡的名称. 通过ifconfig命令可以看到. 这里有一个小技巧. 一般我们家庭用的dhcp自动分配ip地址, 但我们把虚拟机用作服务器时需要配置静态IP, 为了防止dhcp的IP和配置IP冲突, 可以把家用的路由器的dhcp ip分配池范围改一下, 例如我这边改的是192.168.0.1-192.168.0.100. 然后101-254的范围就用来做静态IP了.

修改完ip后需要重启网络, 执行以下命令:

sudo service networking restart

在ubuntu里面, 重启网络ip地址可能不会立即生效. 如果ip没变可以通过以下命令强行刷新:

sudo ip addr flush dev enp0s3
sudo ifdown enp0s3
sudo ifup enp0s3

然后再执行ifconfig命令就可以看到修改了

enp0s3    Link encap:Ethernet  HWaddr 08:00:27:f0:c5:6b
          inet addr:192.168.0.104  Bcast:192.168.0.255  Mask:255.255.255.0

设置SSH

用linux怎么能没有SSH呢, SSH是Secure Shell的缩写. 一般使用它来管理系统.

SSH登录有密码和密钥两种方式, 密码登录每次都要输入而且不安全, 所以一般设置密钥来登录.

通过以下命令可以生成密钥:

ssh-keygen -t rsa

密钥的密码为空就行. 然后进入.ssh目录就会发现有id_rsa, id_rsa.pub两个文件. 前者是私钥, 把它放到你主机上. 后者是公钥, 通过命令:

cat id_rsa.pub >> authorized_keys

把公钥添加上去.

设置authorized_keys权限

$ chmod 600 authorized_keys 

设置.ssh目录权限

$ chmod 700 -R .ssh

然后配置sshd_config. 执行命令:

sudo vim /etc/ssh/sshd_config

去掉下面的#号(注释)

#AuthorizedKeysFile     %h/.ssh/authorized_keys

并在最后添加以下内容:

UseDNS no

ClientAliveInterval 60
ClientAliveCountMax 3

保存退出, 然后执行命令:

sudo service sshd restart

重启ssh后就可以通过密钥登录了.

配置源

ubutun主要通过apt-get来管理软件. 但是自带的源下载速度可能不是那么快. 所以一般使用国内的源.

ubuntu的源文件是/etc/apt/sources.list. 通过命令:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

备份文件. 然后把sources.list替换成以下内容:


## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://mirrors.163.com/ubuntu/ /xenial universe
# deb-src http://hk.archive.ubuntu.com/ubuntu/ xenial universe
deb http://mirrors.163.com/ubuntu/ xenial-updates universe
# deb-src http://hk.archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://mirrors.163.com/ubuntu/ xenial multiverse
# deb-src http://hk.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://mirrors.163.com/ubuntu/ xenial-updates multiverse
# deb-src http://hk.archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://mirrors.163.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://hk.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://mirrors.163.com/ubuntu/ xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://mirrors.163.com/ubuntu/ xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://mirrors.163.com/ubuntu xenial-security multiverse
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

这里用的是阿里云的源, 也可以换成其他网站的.

然后执行一下:

sudo apt-get update

来更新源.

技巧篇

安装一个新的虚拟机还是挺耗费时间的, 那么我们怎么能快速的复制一个新的虚拟机呢. 可以参考以下步骤:

  1. 假设我们根据上面的安装步骤, 在C:\Users\你的用户名\VirtualBox VMs目录下会有一个ubuntu的文件夹. 如果没发现该文件夹, 也可以通过上面的安装步骤3中, 在该虚拟机中右键->在资源管理中显示就能打开该目录了.

  2. 目录里面有一个ubuntu.vdi文件, 该文件就是我们的系统文件. 我们可以把它当作我们的母文件.

  3. 如果直接copy一份这个文件是不行的, 在VMBox的vdi文件中有一个叫uuid的东西, 这个东西是唯一的. 当然我们可以通过命令修改它.

  4. VirtualBox VMs新建一个目录ubuntu_2_vdi. 然后把ubuntu目录中的ubuntu.vdi文件复制过去.

  5. 在cmd里面执行命令

    cd C:\Program Files\Oracle\VirtualBox
    VBoxManage.exe internalcommands sethduuid "C:\Users\你的用户名\VirtualBox VMs\ubuntu_2_vdi\ubuntu.vdi"
    

    这样就能修改uuid了

  6. 然后在上面安装步骤1中, 虚拟硬盘的选项选择: 使用已有的虚拟硬盘文件. 然后选择刚才我们复制出来的vdi就可以了. 这样就可以不用重新走一遍安装流程了.

问题篇

有时候在安装ubuntu后, 系统的初始依赖软件会没有安装下来. 通过以下命令查看:

apt-cache depends build-essential
build-essential
 |Depends: libc6-dev
  Depends: <libc-dev>
    libc6-dev
  Depends: gcc
  Depends: g++
  Depends: make
    make-guile
  Depends: dpkg-dev

libc6-dev那些就是基础软件. 如果没有内容输出则是有问题的. 系统初始软件很重要, 没有他们基本很难做其他事情. 所以我们需要重新安装一下, 通过以下命令安装:

sudo apt-get install build-essential

安装完后就有基础库里.

软件环境

主机系统: win7 64位
VMBox版本: 5.2.14
ubuntu: ubuntu-16.04.2-server-amd64

官网下载链接. VMBox. ubuntu

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容