Book Image

Linux Administration Cookbook

By : Adam K. Dean
Book Image

Linux Administration Cookbook

By: Adam K. Dean

Overview of this book

Linux is one of the most widely used operating systems among system administrators,and even modern application and server development is heavily reliant on the Linux platform. The Linux Administration Cookbook is your go-to guide to get started on your Linux journey. It will help you understand what that strange little server is doing in the corner of your office, what the mysterious virtual machine languishing in Azure is crunching through, what that circuit-board-like thing is doing under your office TV, and why the LEDs on it are blinking rapidly. This book will get you started with administering Linux, giving you the knowledge and tools you need to troubleshoot day-to-day problems, ranging from a Raspberry Pi to a server in Azure, while giving you a good understanding of the fundamentals of how GNU/Linux works. Through the course of the book, you’ll install and configure a system, while the author regales you with errors and anecdotes from his vast experience as a data center hardware engineer, systems administrator, and DevOps consultant. By the end of the book, you will have gained practical knowledge of Linux, which will serve as a bedrock for learning Linux administration and aid you in your Linux journey.
Table of Contents (15 chapters)

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