如何判断服务器是云主机还是物理主机或docker容器

2023-01-11 16:22:00 浏览数 (1)

使用ssh连接到服务器后,可以通过如下几种方式来判断连接到的服务器是物理主机、虚拟机、还是云主机,还是docker环境???

容器和虚拟机的区别:容器共享内核,虚拟机独享内核

虚拟机和物理机的区别:虚拟机是软件模拟的完整硬件系统功能的、完全隔离环境的计算机系统。只要模拟的全面,是没有太大差别的。除了性能差异,毕竟软件做了硬件的事。

方法1:dmidecode命令查看

代码语言:javascript复制
 1dmidecode -t 1
 2dmidecode -s system-product-name
 3dmidecode -s system-Manufacturer
 4
 5
 6PowerEdge R730    # 这个代表物理机
 7SYS-4028GR-TR    # 这个代表物理机
 8VMware Virtual Platform      # 这个代表虚拟机
 9
10
11OpenStack Nova      # 这个代表华为云云主机
12Alibaba Cloud ECS   # 这个代表阿里云云主机

华为云:

代码语言:javascript复制
 1[root@eas-ora ~]# dmidecode -t 1
 2# dmidecode 3.2
 3Getting SMBIOS data from sysfs.
 4SMBIOS 2.8 present.
 5
 6Handle 0x0100, DMI type 1, 27 bytes
 7System Information
 8        Manufacturer: OpenStack Foundation
 9        Product Name: OpenStack Nova
10        Version: 13.2.1-20210707213230_d1da3e2
11        Serial Number: 88f22f46-0843-4ccd-afe9-2929c08e7292
12        UUID: 88f22f46-0843-4ccd-afe9-2929c08e7292
13        Wake-up Type: Power Switch
14        SKU Number: Not Specified
15        Family: Virtual Machine
16
17[root@ecs-9b83 ~]# dmidecode -t 1
18# dmidecode 3.2
19Getting SMBIOS data from sysfs.
20SMBIOS 2.8 present.
21
22Handle 0x0100, DMI type 1, 27 bytes
23System Information
24        Manufacturer: Huawei Cloud
25        Product Name: c7.2xlarge.2
26        Version: 13.2.1-20220808115737_bd245dd
27        Serial Number: 56207974-6690-4174-97fa-5f45250dad5d
28        UUID: 56207974-6690-4174-97fa-5f45250dad5d
29        Wake-up Type: Power Switch
30        SKU Number: Not Specified
31        Family: Virtual Machine
32[root@ecs-9b83 ~]# dmidecode -s system-Manufacturer
33Huawei Cloud        

阿里云:

代码语言:javascript复制
 1[root@xmmup ~]# dmidecode -t 1
 2# dmidecode 3.2
 3Getting SMBIOS data from sysfs.
 4SMBIOS 2.8 present.
 5
 6Handle 0x0100, DMI type 1, 27 bytes
 7System Information
 8        Manufacturer: Alibaba Cloud
 9        Product Name: Alibaba Cloud ECS
10        Version: pc-i440fx-2.1
11        Serial Number: da419577-4e99-4c00-90f2-d2ef8cbb8571
12        UUID: da419577-4e99-4c00-90f2-d2ef8cbb8571
13        Wake-up Type: Power Switch
14        SKU Number: Not Specified
15        Family: Not Specified

方法2:查看根分区的磁盘类型

一般物理机的根分区为/dev/sda2,而云主机的根分区为/dev/vda1,如下:

代码语言:javascript复制
 1[root@eas ~]# df -h
 2Filesystem                       Size  Used Avail Use% Mounted on
 3devtmpfs                          95G     0   95G   0% /dev
 4tmpfs                             95G  9.5M   95G   1% /dev/shm
 5tmpfs                             95G  978M   94G   2% /run
 6tmpfs                             95G     0   95G   0% /sys/fs/cgroup
 7/dev/vda1                         40G   14G   25G  36% /
 8/dev/mapper/vg_oracle-lv_oracle  1.5T  1.3T  190G  87% /oracle
 9tmpfs                             19G     0   19G   0% /run/user/0
10[root@eas-ora ~]# 

注意:也有部分云主机的根目录使用的是共享盘,那么磁盘类型也是/dev/sda1,如下:

代码语言:javascript复制
1[root@ecs-9b83 ~]# df -h
2Filesystem      Size  Used Avail Use% Mounted on
3devtmpfs        7.6G     0  7.6G   0% /dev
4tmpfs           7.6G     0  7.6G   0% /dev/shm
5tmpfs           7.6G  8.7M  7.6G   1% /run
6tmpfs           7.6G     0  7.6G   0% /sys/fs/cgroup
7/dev/sda1        40G  3.0G   35G   8% /
8tmpfs           1.6G     0  1.6G   0% /run/user/0
9[root@ecs-9b83 ~]# 

方法3:查看欢迎语

各个厂商的云主机都配置了欢迎语,即文件/etc/motd,如下:

代码语言:javascript复制
 1-- 华为云
 2[root@ora ~]# cat /etc/motd
 3
 4        Welcome to Huawei Cloud Service
 5
 6
 7
 8-- 阿里云
 9[lhr@xmmup ~]$  cat /etc/motd
10
11Welcome to Alibaba Cloud Elastic Compute Service !

是否docker环境

使用命令查看cgroup:

代码语言:javascript复制
1cat /proc/1/cgroup

原理:容器是通过 cgroup 实现资源限制,判断容器是否在一个 cgroup 组中

如果是docker,显示:

代码语言:javascript复制
 1[root@lhrxxt ~]# docker exec -it test bash
 2[root@test /]# 
 3[root@test /]# 
 4[root@test /]# 
 5[root@test /]# cat /proc/1/cgroup
 615:name=systemd:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
 714:misc:/
 813:rdma:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
 912:pids:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
1011:hugetlb:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
1110:net_prio:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
129:perf_event:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
138:net_cls:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
147:freezer:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
156:devices:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
165:blkio:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
174:cpuacct:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
183:cpu:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
192:cpuset:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
201:memory:/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698
210::/docker/91105ee80286688d365ec8fd584b292cf45d4f3751ebc56d8aa762c773044698

如果是虚拟机或者物理机器,显示:

代码语言:javascript复制
 1[root@lhrxxt ~]# cat /proc/1/cgroup
 211:cpuset:/
 310:cpuacct,cpu:/
 49:pids:/
 58:devices:/
 67:hugetlb:/
 76:perf_event:/
 85:freezer:/
 94:net_prio,net_cls:/
103:memory:/
112:blkio:/
121:name=systemd:/

总结

这几种办法可以综合来判断。

其它方法请参考: https://blog.csdn.net/u010913001/article/details/106488517 https://zhuanlan.zhihu.com/p/70656230

0 人点赞