【精】RockyLiunx纯命令行安装kvm虚拟机

zhuanbike 1月前 76

首先,最小安装rockylinux,安装完成后,进行如下步骤。

1、设置hostname(如需安装宝塔则需要这一步)

hostnamectl set-hostname x86

2、更换阿里云源

sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
    -i.bak \
    /etc/yum.repos.d/[Rr]ocky-*.repo
    
 #重建缓存
 yum clean all
 dnf makecache

设置网络自动连接

nmcli con mod enp5s0 connection.autoconnect yes #替换对应的网卡名称 enp5s0
nmcli con up enp5s0


3、安装远程桌面(按需 一般不需要,直接看第四步)

yum install -y epel-release
yum install xrdp
yum install tigervnc
yum install tigervnc-server
# 设置开机启动
sudo systemctl start xrdp
sudo systemctl enable xrdp

# 查看状态 注意放行防火墙3389端口
systemctl status xrdp

4、安装虚拟机

yum -y install qemu-kvm  libvirt   virt-manager virt-install  virt-viewer

5、启动虚拟机

systemctl start --now libvirtd

# 设置自动启动

systemctl enable libvirtd


6、安装web管理页面(这个好,比桌面好用)

yum -y install cockpit 
yum -y install cockpit-machines

7、启动cockpit.socket服务

systemctl enable --now cockpit.socket

#打开浏览器,访问以下地址:

https://服务器IP:9090

我的主机ip 10.0.0.152

https://10.0.0.152:9090/

登录服务器用户(root用户一般不允许登录,登录自建用户后切换到root)

8、创建虚拟机存储

qemu-img create -f qcow2 /home/data.qcow2 1190G


9、创建虚拟机网络

打开的界面中,选择虚拟机,创建自己的虚拟机,网络,选择 direct 模式 直连。

当然也可以选择网桥 bridge

10、启动虚拟机后,设置自动启动

virsh autostart x86

11、自动化运维,监测虚拟机运行状态

webcode=$(curl -s -o /dev/null -w "%{http_code}" http://www.baidu.com)
selfcode=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.9:8080/)
if [ $selfcode != 200 ]&&[ $webcode = 200 ]; then
    virsh reset x86
fi

实际应用脚本:

get_http_status() {
    curl -s -o /dev/null -w "%{http_code}" "$1"
}
webcode=$(get_http_status http://www.baidu.com)
selfcode=$(get_http_status http://192.168.0.107:8080)
if [[ -z "$webcode" || -z "$selfcode" ]]; then
    echo "!!!!! System Exception, Reboot  !!!!!"
    reboot
elif [[ $webcode -eq 200 && $selfcode -ne 200 ]]; then
    virsh reset x86
else
    echo "  ¥¥¥¥      System OK      ¥¥¥¥  "
fi


最新回复 (3)
  • zhuanbike 1月前
    0 引用 2

    本文参考了  https://blog.csdn.net/Wsjm666/article/details/127867831

  • zhuanbike 12天前
    0 引用 3
    如果需要把网络类型更新为全锥型,将 DMZ绑定对应设备的IPv4地址。
  • zhuanbike 9天前
    0 引用 4
    重启虚拟机网络命令:virsh net-restart default
发新帖