在linux环境非UI界面搭建虚拟机

在linux环境非UI界面搭建虚拟机

手动创建虚拟机

virtualBox的安装教程可以自行百度

1、创建虚拟机,centos是红帽出品,所以选择RedHat_64,可以使用 --basefolder /home/virtualbox/来指定目录

VBoxManage createvm --name k8s1 --ostype RedHat_64 --register

可以用命令 VBoxManage list ostypes(查看支持的操作系统类型)

2、创建磁盘

VBoxManage createvdi --filename /root/VirtualBox\ VMs/k8s1/k8s1.vdi --size 40000

3、创建硬盘控制器

VBoxManage storagectl k8s1 --name storage_controller_1 --add ide

4、挂载虚拟硬盘

VBoxManage storageattach k8s1 --storagectl storage_controller_1 --type hdd --port 0 --device 0 --medium /root/VirtualBox\ VMs/k8s1/k8s1.vdi

5、挂载虚拟光驱,需要保证 iso在硬盘中有

VBoxManage storageattach k8s1 --storagectl storage_controller_1 --type dvddrive --port 1 --device 0 --medium /home/CentOS-7-x86_64-Minimal-2009.iso

6、设置启动顺序,光驱第一,硬盘第二

VBoxManage modifyvm k8s1 --boot1 dvd
VBoxManage modifyvm k8s1 --boot2 disk

7、查看网卡,命令 ip addr,选择要桥接的网卡,我的网卡为team

VBoxManage modifyvm k8s1 --nic1 bridged --cableconnected1 on --nictype1 82540EM --bridgeadapter1 team --intnet1 brigh1 --macaddress1 auto

8、设置内存和CPU

VBoxManage modifyvm k8s1 --memory 8192
VBoxManage modifyvm k8s1 --cpus 2

9、远程桌面操作

# 设置远程桌面连接(可以使用VNC通过5540端口访问虚拟机桌面)
VBoxManage modifyvm k8s1 --vrdeport 5540 --vrdeaddress ""
VBoxManage modifyvm k8s1 --vrde on
VBoxManage modifyvm k8s1 --vrde off

9、启动虚拟机

VBoxManage startvm k8s1 -type headless

脚本批量创建

完整的shell命令如下,可以进行批量操作

#!/bin/bash
# createvm.sh
# 使用命令: sh createvm.sh k8s2 '/home/CentOS-7-x86_64-Minimal-2009.iso' team 5569
VMNAME="$1"
ISOPATH="$2"
NETCARD="$3"
VRDEPORT="$4"
echo "创建vm"
VBoxManage createvm --name "$VMNAME" --ostype RedHat_64 --register
echo "创建vdi"
VBoxManage createvdi --filename /root/VirtualBox\ VMs/"$VMNAME"/"$VMNAME".vdi --size 80000
echo "创建storagectl"
VBoxManage storagectl "$VMNAME" --name storage_controller_1 --add ide
echo "进行关联"
VBoxManage storageattach "$VMNAME" --storagectl storage_controller_1 --type hdd --port 0 --device 0 --medium /root/VirtualBox\ VMs/"$VMNAME"/"$VMNAME".vdi
echo "关联DVD"
VBoxManage storageattach "$VMNAME" --storagectl storage_controller_1 --type dvddrive --port 1 --device 0 --medium "$ISOPATH"
echo "设置启动顺序"
VBoxManage modifyvm "$VMNAME" --boot1 dvd
VBoxManage modifyvm "$VMNAME" --boot2 disk
echo "设置网卡"
VBoxManage modifyvm "$VMNAME" --nic1 bridged --cableconnected1 on --nictype1 82540EM --bridgeadapter1 "$NETCARD" --intnet1 brigh1 --macaddress1 auto
echo "设置内存和CPU"
VBoxManage modifyvm "$VMNAME" --memory 8192
VBoxManage modifyvm "$VMNAME" --cpus 2
echo "设置远程桌面连接"
VBoxManage modifyvm "$VMNAME" --vrdeport "$VRDEPORT"  --vrdeaddress ""
VBoxManage modifyvm "$VMNAME" --vrde on
echo "开机啦"
VBoxManage startvm "$VMNAME" -type headless

执行如下命令批量操作

sh createvm.sh k8s2 '/home/CentOS-7-x86_64-Minimal-2009.iso' team 5569

善后工作

点击windows电脑的远程桌面连接,输入宿主机的地址+vrde的端口,进入到linux安装界面。

修改网络配置,使IP和宿主机在同一网段

部分配置如下,之前有重复的配置进行修改,没有的添加

BOOTPROTO=static
ONBOOT=yes
PREFIX=24
GATEWAY=192.168.80.254
DNS1=223.5.5.5
IPADDR=192.168.80.67

修改完成之后重启服务器即可

其他命令

虚拟机的信息

VBoxManage -v
VBoxManage list vms
VBoxManage list runningvms
VBoxManage showvminfo UbuntuRDHome
VBoxManage list hdds
VBoxManage list dvds

虚拟机的其他命令

# 关机
VBoxManage controlvm k8s1 poweroff
# 启动
VBoxManage startvm k8s1 --type headless
# 删除虚拟机,谨慎操作
VBoxManage unregistervm --delete k8s1

参考文章

[centos6.7 安装 virtualBox 再安装 centos 7]

VirtualBox命令行VBoxManage创建与管理虚拟机教程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是刘奇奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值