windows上安装虚拟机ubuntu

本文最后更新于 2025年4月22日 下午

1 安装虚拟机

1、安装虚拟机模拟软件virtualboxhttps://download.virtualbox.org/virtualboxhttps://download.virtualbox.org/virtualbox/7.0.10/VirtualBox-7.0.10-158379-Win.exe
2、在virtualbox中安装虚拟机ubuntu,本文选择20.04.6 LTS 桌面版

20.04.6 LTS 桌面版下载链接https://www.releases.ubuntu.com/focalhttps://www.releases.ubuntu.com/focal/ubuntu-20.04.6-desktop-amd64.iso

3、安装虚拟机时推荐参数设置:

  • 共享粘贴板为双向
  • 磁盘大小50GB
  • 内存大小为8192MB(8GB)
  • 处理器核心数为4
  • 运行峰值100%
  • 显存大小128MB
  • 存储属性选中固态驱动器
  • 网络为桥接网卡
  • 启用USB控制器

4、启动ubuntu,安装增强工具,重启,看粘贴是否生效
5、若Ctrl+T无法打开终端,将Settings -> Region & Language -> Language改为Chinese,然后重启,然后再改回English(United States)

6、若虚拟机屏幕太小,可以将Settings -> Displays -> Resolution设置修改为显示器的分辨率大小

2 配置sudo权限

1、设置root用户密码

1
sudo passwd root

根据提示,连续两次输入密码确认

2、输入su切换到root,输入root密码

1
su root

3、打开sudo权限配置文件

1
visudo

4、修改sudo权限配置文件,假设用户名为shan
在# Allow members of group sudo to execute any command下增加一行

1
shan   ALL=(ALL:ALL) ALL

上述中的shan为当前用户名

按下Ctrl+X退出编辑器,再按下Y确认保存

5、切换回普通用户,假如用户名为shan

1
su shan

3 更新软件源

备份设置软件源的文件

1
sudo mv /etc/apt/sources.list /etc/apt/sources_back.list

重新设置软件源的文件内容

1
sudo nano /etc/apt/sources.list

复制以下内容到编辑器中

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu focal main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu focal main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu focal-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-updates main restricted

## 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://cn.archive.ubuntu.com/ubuntu focal universe
# deb-src http://cn.archive.ubuntu.com/ubuntu focal universe
deb http://cn.archive.ubuntu.com/ubuntu focal-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-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://cn.archive.ubuntu.com/ubuntu focal multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal multiverse
deb http://cn.archive.ubuntu.com/ubuntu focal-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-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://cn.archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-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 focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb http://cn.archive.ubuntu.com/ubuntu focal-security main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu focal-security universe
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-security universe
deb http://cn.archive.ubuntu.com/ubuntu focal-security multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu focal-security multiverse
# deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
# deb-src [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal stable
# deb-src [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal stable

按下Ctrl+X退出编辑器,再按下Y确认保存

更新软件源

1
sudo apt-get update && sudo apt-get upgrade

若提示如下

1
2
3
4
5
6
7
8
9
10
11
12
Get:1 https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal InRelease [57.7 kB]
Err:1 https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal InRelease
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8
Hit:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InRelease
Hit:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates InRelease
Hit:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports InRelease
Hit:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security InRelease
Reading package lists... Done
W: GPG error: https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8
E: The repository 'https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

注意NO_PUBKEY 7EA0A9C3F273FCD8,说明docker存储库没有导入秘钥

运行以下命令即可,注意确认最后一串数字是否一致

1
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7EA0A9C3F273FCD8

然后再更新软件源

4 设置ssh连接

设置ssh可以让windows主机通过命令行访问ubuntu主机

1、安装ssh

1
2
3
sudo apt-get update
sudo apt-get install net-tools
sudo apt-get install openssh-server

如果安装软件失败,可以先执行以下命令

1
sudo apt-get -u dist-upgrade

2、启动ssh服务

1
sudo service ssh start

3、查询虚拟机的ip地址
在终端输入ifconfig命令,假设输出ip地址为10.98.18.10

4、ssh访问虚拟机
在SecureCRT等终端软件中通过ssh协议访问虚拟机
Hostname为ip地址,Username为虚拟机的用户名
或直接在cmd终端中输入以下命令,假设用户名为shan

1
ssh shan@10.98.18.10

第一次连接时,根据提示输入yes,保存ubuntu的公钥到本地

5 设置samba服务器

设置samba服务器可以将ubuntu上的文件映射在本地Windows电脑上,方便在windows上对相关项目文件进行添加、删除、打开等操作进行管理
1、安装samba

1
2
sudo apt-get update
sudo apt-get install samba samba-common net-tools

2、创建用于映射的文件夹,假设用于共享的文件夹为/project,设置权限为可读可写可执行

1
2
sudo mkdir -p /mnt/fileroot
sudo chmod -R 777 /mnt/fileroot

3、设置用户的samba访问密码,假设用户名为shan

1
sudo smbpasswd -a shan

输入两次密码后设置成功
4、修改samba的配置文件

1
2
sudo cp /etc/samba/smb.conf /etc/samba/smb_back.conf
sudo nano /etc/samba/smb.conf

假设设置共享文件夹路径为/project,用户为shan,访问文件夹为smb

在文件最后粘贴内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
[smb]
comment = smb folder
browseable = yes
path = /mnt/fileroot
create mask = 0700
directory mask = 0700
valid users = shan
force user = shan
force group = shan
public = yes
available = yes
writable = yes

上述设置中:

[ ]括号内的内容为samba服务映射到windows上的文件目录名称
path为ubuntu上映射出去的目录
valid users为window上访问该目录的用户名

5、重启samba服务

1
sudo service smbd restart

6、查询虚拟机的ip地址
在终端输入ifconfig命令,假设输出ip地址为10.98.18.10

7、访问虚拟机中的共享文件夹
在资源管理器的窗口中输入\10.98.18.10\smb,即可看到共享的smb文件夹

或者在电脑图标上右键映射网络驱动器,文件夹选择\10.98.18.10\smb,输入samba的用户账号、密码即可看到共享的smb文件夹

6 安装依赖包

在ubuntu上创建installed_packages.txt文件,然后在该文件中编辑添加需要的相关包

例如,可以粘贴以下内容

1
2
3
4
5
6
7
8
9
10
11
12
vim
gdb
gcc
g++
git
make
cmake
samba
curl
net-tools
build-essential
openssh-server

然后在当前ubuntu上部署相关软件包,执行以下命令

1
sudo apt-get install $(awk '{print $1}' installed_packages.txt) || true

6 快速部署开发环境

如果要将另一台ubuntu上安装的相关开发包在当前ubuntu机器上部署

首先先从另一台ubuntu上输出已经安装的依赖包,例如在另一台linux服务器上运行

1
dpkg --get-selections > installed_packages.txt

然后将该文件的内容全选复制到当前ubuntu上的installed_packages.txt文件中

执行安装命令后,如果遇到冲突的包,则在installed_packages.txt文件中将对应的包所在行删除

然后继续执行安装命令,直到安装完成


windows上安装虚拟机ubuntu
https://www.bit01.top/2025/04/02/windows-virtual-ubuntu/
作者
李珊
发布于
2025年4月2日
更新于
2025年4月22日
许可协议