如何判断当前服务器是云主机、物理机、虚拟机、docker环境?
Tags: docker环境LinuxOS云云主机云服务器容器物理主机物理服务器物理机虚拟机
使用ssh连接到服务器后,可以通过如下几种方式来判断连接到的服务器是物理主机、虚拟机、还是云主机,还是docker环境???
容器和虚拟机的区别:容器共享内核,虚拟机独享内核
虚拟机和物理机的区别:虚拟机是软件模拟的完整硬件系统功能的、完全隔离环境的计算机系统。只要模拟的全面,是没有太大差别的。除了性能差异,毕竟软件做了硬件的事。
方法1:dmidecode命令查看
1 2 3 4 5 6 7 8 9 10 | dmidecode -t 1 dmidecode -s system-product-name dmidecode -s system-Manufacturer PowerEdge R730 # 这个代表物理机 SYS-4028GR-TR # 这个代表物理机 VMware Virtual Platform # 这个代表虚拟机 OpenStack Nova # 这个代表华为云云主机 Alibaba Cloud ECS # 这个代表阿里云云主机 |
华为云:
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 | [root@eas-ora ~]# dmidecode -t 1 # dmidecode 3.2 Getting SMBIOS data from sysfs. SMBIOS 2.8 present. Handle 0x0100, DMI type 1, 27 bytes System Information Manufacturer: OpenStack Foundation Product Name: OpenStack Nova Version: 13.2.1-20210707213230_d1da3e2 Serial Number: 88f22f46-0843-4ccd-afe9-2929c08e7292 UUID: 88f22f46-0843-4ccd-afe9-2929c08e7292 Wake-up Type: Power Switch SKU Number: Not Specified Family: Virtual Machine [root@ecs-9b83 ~]# dmidecode -t 1 # dmidecode 3.2 Getting SMBIOS data from sysfs. SMBIOS 2.8 present. Handle 0x0100, DMI type 1, 27 bytes System Information Manufacturer: Huawei Cloud Product Name: c7.2xlarge.2 Version: 13.2.1-20220808115737_bd245dd Serial Number: 56207974-6690-4174-97fa-5f45250dad5d UUID: 56207974-6690-4174-97fa-5f45250dad5d Wake-up Type: Power Switch SKU Number: Not Specified Family: Virtual Machine [root@ecs-9b83 ~]# dmidecode -s system-Manufacturer Huawei Cloud |
阿里云:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@xmmup ~]# dmidecode -t 1 # dmidecode 3.2 Getting SMBIOS data from sysfs. SMBIOS 2.8 present. Handle 0x0100, DMI type 1, 27 bytes System Information Manufacturer: Alibaba Cloud Product Name: Alibaba Cloud ECS Version: pc-i440fx-2.1 Serial Number: da419577-4e99-4c00-90f2-d2ef8cbb8571 UUID: da419577-4e99-4c00-90f2-d2ef8cbb8571 Wake-up Type: Power Switch SKU Number: Not Specified Family: Not Specified |
方法2:查看根分区的磁盘类型
一般物理机的根分区为/dev/sda2,而云主机的根分区为/dev/vda1,如下:
1 2 3 4 5 6 7 8 9 10 | [root@eas ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 95G 0 95G 0% /dev tmpfs 95G 9.5M 95G 1% /dev/shm tmpfs 95G 978M 94G 2% /run tmpfs 95G 0 95G 0% /sys/fs/cgroup /dev/vda1 40G 14G 25G 36% / /dev/mapper/vg_oracle-lv_oracle 1.5T 1.3T 190G 87% /oracle tmpfs 19G 0 19G 0% /run/user/0 [root@eas-ora ~]# |
注意:也有部分云主机的根目录使用的是共享盘,那么磁盘类型也是/dev/sda1,如下:
123456789 [root@ecs-9b83 ~]# df -hFilesystem Size Used Avail Use% Mounted ondevtmpfs 7.6G 0 7.6G 0% /devtmpfs 7.6G 0 7.6G 0% /dev/shmtmpfs 7.6G 8.7M 7.6G 1% /runtmpfs 7.6G 0 7.6G 0% /sys/fs/cgroup/dev/sda1 40G 3.0G 35G 8% /tmpfs 1.6G 0 1.6G 0% /run/user/0[root@ecs-9b83 ~]#
方法3:查看欢迎语
各个厂商的云主机都配置了欢迎语,即文件/etc/motd
,如下:
1 2 3 4 5 6 7 8 9 10 | -- 华为云 [root@ora ~]# cat /etc/motd Welcome to Huawei Cloud Service -- 阿里云 [lhr@xmmup ~]$ cat /etc/motd Welcome to Alibaba Cloud Elastic Compute Service ! |
是否docker环境
使用命令查看cgroup:
1 | cat /proc/1/cgroup |
原理:容器是通过 cgroup 实现资源限制,判断容器是否在一个 cgroup 组中
如果是docker,显示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@lhrxxt ~]# docker exec -it test bash [root@test /]# [root@test /]# [root@test /]# [root@test /]# cat /proc/1/cgroup 15:name=systemd:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 14:misc:/ 13:rdma:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 12:pids:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 11:hugetlb:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 10:net_prio:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 9:perf_event:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 8:net_cls:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 7:freezer:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 6:devices:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 5:blkio:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 4:cpuacct:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 3:cpu:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 2:cpuset:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 1:memory:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 0::/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698 |
如果是虚拟机或者物理机器,显示:
1 2 3 4 5 6 7 8 9 10 11 12 | [root@lhrxxt ~]# cat /proc/1/cgroup 11:cpuset:/ 10:cpuacct,cpu:/ 9:pids:/ 8:devices:/ 7:hugetlb:/ 6:perf_event:/ 5:freezer:/ 4:net_prio,net_cls:/ 3:memory:/ 2:blkio:/ 1:name=systemd:/ |
总结
这几种办法可以综合来判断。
其它方法请参考:
https://blog.csdn.net/u010913001/article/details/106488517
https://zhuanlan.zhihu.com/p/70656230