Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Linux Administration Cookbook

You're reading from   Linux Administration Cookbook Insightful recipes to work with system administration tasks on Linux

Arrow left icon
Product type Paperback
Published in Dec 2018
Publisher Packt
ISBN-13 9781789342529
Length 826 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Adam K. Dean Adam K. Dean
Author Profile Icon Adam K. Dean
Adam K. Dean
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Introduction and Environment Setup FREE CHAPTER 2. Remote Administration with SSH 3. Networking and Firewalls 4. Services and Daemons 5. Hardware and Disks 6. Security, Updating, and Package Management 7. Monitoring and Logging 8. Permissions, SELinux, and AppArmor 9. Containers and Virtualization 10. Git, Configuration Management, and Infrastructure as Code 11. Web Servers, Databases, and Mail Servers 12. Troubleshooting and Workplace Diplomacy 13. BSDs, Solaris, Windows, IaaS and PaaS, and DevOps 14. Other Books You May Enjoy

Understanding how VMs differ

Earlier, we started talking about VMs and what they are. We're now going to look at a couple of ways of working out if we're in a VM, from inside the machine itself.

I would generally do this if I'd got a new virtual private server (VPS) from a hosting provider, and wanted to know what software was being used to virtualize my new machine.

dmidecode

One of my favourite tools, dmidecode, can be used to dump a computer's desktop management interface (DMI) table. In practice, this means that it can be used to find out what kind of hardware you're running in a machine.

This command requires root access, so we'll be using sudo throughout these examples.

First, we're going to list the valid types we can pass to dmidecode:

$ dmidecode --type
dmidecode: option '--type' requires an argument
Type number or keyword expected
Valid type keywords are:
bios
system
baseboard
chassis
processor
memory
cache
connector
slot

Starting at the top, we're going to use bios and see if it gives us anything useful:

$ sudo dmidecode --type bios
# dmidecode 3.0
Getting SMBIOS data from sysfs.
SMBIOS 2.5 present.

Handle 0x0000, DMI type 0, 20 bytes
BIOS Information
Vendor: innotek GmbH
Version: VirtualBox
Release Date: 12/01/2006
Address: 0xE0000
Runtime Size: 128 kB
ROM Size: 128 kB
Characteristics:
ISA is supported
PCI is supported
Boot from CD is supported
Selectable boot is supported
8042 keyboard services are supported (int 9h)
CGA/mono video services are supported (int 10h)
ACPI is supported

Instantly, we can see VirtualBox next to Version, which is a pretty strong hint that we're dealing with a VM.

Next, we will choose something else, system:

$ sudo dmidecode --type system
# dmidecode 3.0
Getting SMBIOS data from sysfs.
SMBIOS 2.5 present.

Handle 0x0001, DMI type 1, 27 bytes
System Information
Manufacturer: innotek GmbH
Product Name: VirtualBox
Version: 1.2
Serial Number: 0
UUID: BDC643B8-8D4D-4288-BDA4-A72F606CD0EA
Wake-up Type: Power Switch
SKU Number: Not Specified
Family: Virtual Machine

Again, the Product Name seen here is VirtualBox, and the Family is Virtual Machine, both of which are pretty damning pieces of evidence.

Lastly, we're going to look at the Chassis Information:

$ sudo dmidecode --type chassis
# dmidecode 3.0
Getting SMBIOS data from sysfs.
SMBIOS 2.5 present.

Handle 0x0003, DMI type 3, 13 bytes
Chassis Information
Manufacturer: Oracle Corporation
Type: Other
Lock: Not Present
Version: Not Specified
Serial Number: Not Specified
Asset Tag: Not Specified
Boot-up State: Safe
Power Supply State: Safe
Thermal State: Safe
Security Status: None

Oracle corporation is, again, a significant piece of information that leads us to believe we're in a virtualized environment.

If we don't want a lot of other information, we can fine-tune our search using dmidecode's -s option.

Running this option without an argument outputs a list of potential arguments we can use:

$ sudo dmidecode -s
dmidecode: option requires an argument -- 's'
String keyword expected
Valid string keywords are:
bios-vendor
bios-version
bios-release-date
system-manufacturer
system-product-name
system-version
system-serial-number
system-uuid
baseboard-manufacturer
baseboard-product-name
baseboard-version
baseboard-serial-number
baseboard-asset-tag
chassis-manufacturer
chassis-type
chassis-version
chassis-serial-number
chassis-asset-tag
processor-family
processor-manufacturer
processor-version
processor-frequency

Here, we can instantly see bios-version, and as we know from earlier, it should be VirtualBox:

$ sudo dmidecode -s bios-version
VirtualBox

These types of short-output commands are very useful for scripting, where succinctness is sometimes desirable.

dmidecode is usually installed by default, at least on Ubuntu and CentOS installations.

lshw

Should dmidecode not be available, you can also make use of lshw, a command for listing hardware. Again, it makes use of the DMI table on a device.

Very quickly, we can use a format option of lshw to show the bus information of a system:

$ sudo lshw -businfo
Bus info Device Class Description
=====================================================
system VirtualBox
bus VirtualBox
memory 128KiB BIOS
memory 1GiB System memory
cpu@0 processor Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
pci@0000:00:00.0 bridge 440FX - 82441FX PMC [Natoma]
pci@0000:00:01.0 bridge 82371SB PIIX3 ISA [Natoma/Triton II]
pci@0000:00:01.1 scsi1 storage 82371AB/EB/MB PIIX4 IDE
scsi@1:0.0.0 /dev/cdrom disk CD-ROM
pci@0000:00:02.0 display VirtualBox Graphics Adapter
pci@0000:00:03.0 enp0s3 network 82540EM Gigabit Ethernet Controller
pci@0000:00:04.0 generic VirtualBox Guest Service
pci@0000:00:05.0 multimedia 82801AA AC'97 Audio Controller
pci@0000:00:06.0 bus KeyLargo/Intrepid USB
usb@1 usb1 bus OHCI PCI host controller
pci@0000:00:07.0 bridge 82371AB/EB/MB PIIX4 ACPI
pci@0000:00:0d.0 scsi2 storage 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode]
scsi@2:0.0.0 /dev/sda disk 8589MB VBOX HARDDISK
scsi@2:0.0.0,1 /dev/sda1 volume 1GiB Linux filesystem partition
scsi@2:0.0.0,2 /dev/sda2 volume 7167MiB Linux LVM Physical Volume partition
input PnP device PNP0303
input PnP device PNP0f03

This gives us information that instantly suggests a VM, such as the system, bus, and display entries.

We also have an easy-to-read breakdown of the classes available, meaning that we can query those directly, starting with disk in this example:

$ sudo lshw -c disk
*-cdrom
description: DVD reader
product: CD-ROM
vendor: VBOX
physical id: 0.0.0
bus info: scsi@1:0.0.0
logical name: /dev/cdrom
logical name: /dev/sr0
version: 1.0
capabilities: removable audio dvd
configuration: ansiversion=5 status=nodisc
*-disk
description: ATA Disk
product: VBOX HARDDISK
vendor: VirtualBox
physical id: 0.0.0
bus info: scsi@2:0.0.0
logical name: /dev/sda
version: 1.0
serial: VB5cbf266c-3015878d
size: 8GiB (8589MB)
capabilities: partitioned partitioned:dos
configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512 signature=000b6a88

Alternatively, if we think that's too much information, we could query the system class:

$ sudo lshw -c system
localhost.localdomain
description: Computer
product: VirtualBox
vendor: innotek GmbH
version: 1.2
serial: 0
width: 64 bits
capabilities: smbios-2.5 dmi-2.5 vsyscall32
configuration: family=Virtual Machine uuid=BDC643B8-8D4D-4288-BDA4-A72F606CD0EA
You have been reading a chapter from
Linux Administration Cookbook
Published in: Dec 2018
Publisher: Packt
ISBN-13: 9781789342529
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image