RANGER GAMEZ

CompTIA Linux+ (XK0-006) · chapter 5 of 14

Month 2: Backups and virtual machines

Objective 1.6

Archives and compression

tar: the archiver everyone uses

tar bundles files into one archive and keeps permissions and owners. The letters: c create, x extract, t list, f the archive file, v verbose, -C extract into a directory. Add a compressor: z gzip, j bzip2, J xz.

tar -czf etc.tar.gz /etc     # create
tar -tzf etc.tar.gz          # list
tar -xzf etc.tar.gz -C /tmp  # extract
tar -cJf logs.tar.xz /var/log/app

Exam tip f must be followed by the archive name. tar -cfz backup ... names the archive "z".

cpio: archives from a file list

cpio reads the list of files to pack from standard input, which pairs nicely with find. -o creates (copy-out), -i extracts (copy-in), -d creates directories while extracting. You meet it in the wild because initramfs images are cpio archives.

find /srv/shop -mtime -1 |
  cpio -o > today.cpio
cpio -id < today.cpio

Exam tip tar takes file names as arguments. cpio takes them on stdin.

Compression tools compared

Each tool trades speed for size.

  • gzip (.gz): fast, everywhere; gunzip or gzip -d to undo
  • bzip2 (.bz2): smaller files, slower
  • xz (.xz): smallest of the three, slowest to compress, fast to decompress; -T0 uses all CPU cores
  • 7-Zip (7z, .7z): high ratio, also reads many formats; handy for Windows partners
  • unzip (.zip): opens zip files from Windows and Macs (zip makes them)

Exam tip Size, smallest to biggest, usually: xz < bzip2 < gzip. Speed: the reverse.

Reading compressed files in place

Rotated logs are often .gz. You don't need to unpack them: zcat prints a .gz file, zless pages through it, zgrep searches it. zgrep 'Failed password' /var/log/auth.log.*.gz searches every old log at once.

Exam tip z-tools are for gzip files. xz has xzcat/xzgrep; bzip2 has bzcat.

Objective 1.6

Copying disks and syncing trees

dd: copy raw blocks

dd copies bytes from one file or device to another, with no idea of files or filesystems: if= is the input, of= the output, bs= the block size. It images whole disks, writes ISO files to USB sticks, and wipes devices. It also never asks for confirmation.

dd if=/dev/sdb of=/backup/sdb.img \
   bs=4M status=progress
dd if=ubuntu.iso of=/dev/sdc \
   bs=4M conv=fsync

Exam tip Swap if= and of= by mistake and you overwrite the source. Read the line twice before Enter.

ddrescue: when the disk is dying

ddrescue is dd for failing disks. It copies the healthy areas first, skips bad spots, then goes back to retry them, and it keeps a map file so you can stop and resume. Copy the dying disk to an image, then repair or mount the image, never the sick disk.

ddrescue /dev/sdb /backup/sdb.img sdb.map

Exam tip A failing disk gets worse with every read. Image it first with ddrescue, then fsck the image.

rsync: copy only what changed

rsync compares source and destination and sends only the differences, locally or over SSH. -a (archive) keeps permissions, owners, times and links; -z compresses over the network; --delete removes files at the destination that are gone from the source (a true mirror); -n is a dry run. A trailing slash matters: src/ copies the contents of src, while src copies the directory itself.

rsync -az --delete /srv/shop/ \
  backup01:/backups/shop/

Sam: rsync for nightly copies, tar for snapshots in time, and test a restore every month.

Exam tip Try --delete with -n first. A wrong path plus --delete is a very fast way to lose a backup.

Check yourself 1Priya needs every "Failed password" line from last month. The logs are rotated as auth.log.2.gz through auth.log.5.gz. What's the most direct command?

  1. grep -r 'Failed password' /var/log/auth.log.*.gz
  2. tar -xzf /var/log/auth.log.*.gz
  3. zgrep 'Failed password' /var/log/auth.log.*.gz
  4. xzgrep 'Failed password' /var/log/auth.log.*
Show the answer

Answer: C. zgrep 'Failed password' /var/log/auth.log.*.gz

zgrep decompresses gzip files on the fly and searches them, so no unpacking or temporary files are needed.

Objective 1.7

Hypervisors and VM building blocks

KVM and QEMU

KVM (Kernel-based Virtual Machine) is a kernel module that turns Linux into a hypervisor, using the CPU's virtualization features (Intel VT-x, AMD-V: look for vmx or svm in /proc/cpuinfo). QEMU emulates the rest of the machine (disks, NICs, firmware) and runs each VM as a process. Together they're the standard Linux virtualization stack. QEMU alone can also emulate other CPU types, slowly.

$ grep -cE 'vmx|svm' /proc/cpuinfo
64
$ lsmod | grep kvm
kvm_intel   487424  4
kvm        1425408  1 kvm_intel

Bare metal vs virtual machines

A bare-metal server runs its OS directly on the hardware: all of the performance, none of the flexibility. A virtual machine shares a host with others: you can snapshot, clone, resize and move it, at a small performance cost. Hypervisors that run directly on hardware (like KVM inside the Linux kernel) are type 1; ones that run as an app on a desktop OS are type 2.

Exam tip Need every bit of GPU or disk speed, or special hardware? Bare metal. Need flexibility and density? VMs.

Paravirtualized drivers: VirtIO

An emulated NIC (e1000) or disk (IDE, SATA) pretends to be real hardware, which costs CPU time. Paravirtualized drivers know they're in a VM and talk to the hypervisor directly. On KVM that's VirtIO: virtio-net for networking and virtio-blk or virtio-scsi for disks. Disks show up as /dev/vda instead of /dev/sda.

Exam tip Slow VM disk or network? Check whether it's on an emulated device instead of VirtIO.

Disk images and VM states

qemu-img handles disk images. info shows the image properties: format, virtual size, real size on disk, backing file. convert changes formats (raw, qcow2, vmdk), and resize grows an image (then grow the partition and filesystem inside the guest). VM states in virsh: running, paused (frozen in RAM), shut off, crashed, pmsuspended (guest went to sleep). Nested virtualization runs a hypervisor inside a VM: handy for labs and CI, slower in production.

qemu-img info web02.qcow2
qemu-img convert -f vmdk -O qcow2 \
   in.vmdk out.qcow2
qemu-img resize web02.qcow2 +20G

Exam tip qcow2 supports thin provisioning and snapshots; raw is simplest and fastest.

Check yourself 2A new VM's disk benchmark is far below the host's. virsh dumpxml shows <target dev='hda' bus='ide'/>. What's the best change?

  1. Switch the disk to the VirtIO bus
  2. Convert the image from qcow2 to vmdk
  3. Turn on nested virtualization
  4. Pause the VM and resume it
Show the answer

Answer: A. Switch the disk to the VirtIO bus

IDE is fully emulated. A VirtIO disk is paravirtualized and much faster; the guest then sees it as /dev/vda.

Objective 1.7

Running VMs: tools, operations, networks

libvirt, virsh and virt-manager

libvirt is the management layer (the libvirtd or modular virtqemud daemons, plus an API) that tools talk to. virsh is its command line; virt-manager is the desktop GUI for the same thing.

virsh list --all          # every VM + state
virsh start web02
virsh shutdown web02      # polite (ACPI)
virsh destroy web02       # pull the plug
virsh suspend / resume web02
virsh autostart web02     # start at boot
virsh console web02

Exam tip destroy doesn't delete anything: it's a hard power-off. undefine removes the VM's definition.

VM operations

Everyday work on VMs:

  • Resources: give a VM more vCPUs, RAM, disks or NICs (virsh setvcpus, setmem, attach-disk)
  • Baseline image templates: a golden image, patched and generalized, that every new VM starts from
  • Cloning: copy a VM (virt-clone). Then reset what must be unique: hostname, MAC, machine-id, SSH host keys (virt-sysprep does this)
  • Snapshots: a point-in-time state to roll back to (virsh snapshot-create-as). Not a backup: same storage
  • Migrations: move a VM to another host; live migration keeps it running, with shared storage

Exam tip A snapshot lives with the VM's disk. Lose the disk, lose the snapshots. Back up separately.

Virtual network types

How a VM reaches the world:

  • NAT: libvirt's default network (virbr0, 192.168.122.0/24). VMs reach out; outside hosts can't reach in
  • Bridged: the VM sits on the physical LAN like any server, with its own address there
  • Host-only / isolated: VMs talk to each other and the host, nothing else
  • Routed: the host routes the VM subnet without NAT; other routers need a route to it
  • Open: like routed, but libvirt adds no firewall rules at all; you manage them

Exam tip Users must reach the VM as a normal server? Bridged. VM only needs updates? NAT is fine.

Go deeper

More on the objectives in this chapter, in the same exam-ready detail.

Objective 1.6 · Archiving: cpio, tar

Archiving with tar and cpio

tar

tar bundles files into one archive (it doesn't compress unless you add a flag).

  • tar -cvf backup.tar /etc: create
  • tar -tvf backup.tar: list contents
  • tar -xvf backup.tar -C /restore: extract into a directory
  • Add -z (gzip, .tar.gz), -j (bzip2, .tar.bz2) or -J (xz, .tar.xz)

Exam tip c = create, x = extract, t = list, v = verbose, f = file name follows.

cpio

cpio reads a list of file names from standard input: find /etc | cpio -ov > etc.cpio creates; cpio -idv < etc.cpio extracts. It's used inside RPM packages and initramfs images.

Objective 1.6 · Compression tools: 7-Zip, bzip2, gzip, unzip, xz

Compression tools

The tools

Each tool trades speed for size differently:

  • gzip / gunzip (.gz): fast, common
  • bzip2 / bunzip2 (.bz2): smaller, slower
  • xz / unxz (.xz): smallest of the three, slowest
  • zip / unzip (.zip): compatible with Windows
  • 7z (7-Zip, .7z): high compression, many formats (package p7zip or 7zip)

Remember

gzip, bzip2 and xz compress one file and replace it; combine with tar for directories. Read compressed logs without extracting: zcat, zless, zgrep (and xzcat, bzcat).

Exam tip Best compression ratio of the three classics: xz. Fastest: gzip.

Objective 1.7 · Linux hypervisors: QEMU, KVM

QEMU and KVM

How they fit together

KVM (Kernel-based Virtual Machine) is a Linux kernel module that turns Linux into a type 1 hypervisor using CPU virtualization extensions (Intel VT-x, AMD-V). QEMU emulates the hardware (disks, network cards) and runs the VMs, using KVM for speed. libvirt manages them (virsh, virt-manager, virt-install).

  • Check CPU support: grep -E 'vmx|svm' /proc/cpuinfo
  • lsmod | grep kvm shows the module loaded
  • virsh list --all, virsh start vm1, virsh shutdown vm1

Objective 1.7 · Virtual machines (VMs): paravirtualized drivers, VirtIO

Paravirtualized drivers: VirtIO

Why they're faster

Fully emulated hardware (like an emulated Intel network card) is slow because the hypervisor imitates real hardware. Paravirtualized drivers know they're in a VM and talk to the hypervisor directly. On KVM these are the VirtIO drivers: virtio-net (network), virtio-blk and virtio-scsi (disks), virtio-balloon (memory).

Exam tip Slow disk or network in a KVM guest: make sure it uses VirtIO devices. Linux has them built in; Windows guests need the virtio-win drivers.

Objective 1.7 · Virtual machines (VMs): disk image operations (convert, resize, image properties)

VM disk images with qemu-img

Formats and commands

VM disks are files: qcow2 (QEMU's format: thin, snapshots, compression) or raw (simple, fast), plus VMDK and VHD(X) from other hypervisors.

  • qemu-img info disk.qcow2: format, virtual size, actual size, backing file
  • qemu-img convert -f vmdk -O qcow2 in.vmdk out.qcow2: convert formats
  • qemu-img resize disk.qcow2 +20G: grow (then grow the partition and filesystem inside the guest)
  • qemu-img create -f qcow2 disk.qcow2 40G: create

Objective 1.7 · Virtual machines (VMs): nested virtualization

Nested virtualization

A VM inside a VM

Nested virtualization lets a guest act as a hypervisor itself, for labs, CI testing or running a hypervisor in the cloud. On KVM it's a module option: cat /sys/module/kvm_intel/parameters/nested (Y/1 = on); enable with options kvm_intel nested=1 (or kvm_amd) in /etc/modprobe.d/, and pass the host CPU features to the guest (cpu mode host-passthrough).

Exam tip Expect lower performance; it's for testing, not production.

Objective 1.7 · VM operations: baseline image templates

Baseline image templates

Build once, deploy many

A template (golden image) is a prepared, patched VM image used to create new VMs quickly and consistently. Before making a template, generalize it: remove machine-specific data such as SSH host keys, machine-id, logs and user history (virt-sysprep does this), then use cloud-init to customize each new copy at first boot.

Exam tip Two cloned VMs with the same SSH host keys or machine-id: the template wasn't generalized.

Objective 1.7 · VM operations: cloning

Cloning VMs

Full vs linked clones

virt-clone --original vm1 --name vm2 --auto-clone copies a VM's configuration and disks. A full clone is an independent copy; a linked clone (backing file / qcow2 overlay) shares the original disk read-only and stores only changes, saving space but depending on the original.

Exam tip Clone a VM while it's shut down (or paused) so the disk is consistent, then change its hostname, IP and machine identity.

Objective 1.7 · VM operations: migrations

Migrating VMs between hosts

Live vs offline

Live migration moves a running VM to another host with almost no downtime: memory is copied while it runs, then a brief pause switches it over (virsh migrate --live vm1 qemu+ssh://host2/system). It usually needs shared storage (or storage migration) and compatible CPUs. Offline (cold) migration shuts the VM down and copies its disk and definition.

Exam tip Need to patch a host without stopping VMs: live-migrate them off first.

Objective 1.7 · Bare metal vs. virtual machines

Bare metal vs virtual machines

Compare

Where the operating system runs changes what you get:

  • Bare metal: the OS runs directly on the hardware. Full performance and direct hardware access (GPUs, specialized cards); one OS per server; slower to provision
  • Virtual machine: many isolated OSes share one physical host through a hypervisor. Better use of hardware, snapshots, templates, live migration; a little overhead
  • Type 1 hypervisor runs on bare metal (KVM, ESXi, Hyper-V); type 2 runs on top of an OS (VirtualBox)

Objective 1.7 · Network types: bridged, NAT, host-only/isolated, routed, open

VM network types

How a VM reaches the network

libvirt and other hypervisors offer several network modes:

  • Bridged: the VM sits on the physical LAN like any other machine and gets an address from the real network
  • NAT: the VM uses a private address and reaches out through the host (libvirt's default network, virbr0); outsiders can't connect in without port forwarding
  • Host-only / isolated: VMs talk to each other and the host, not the outside world
  • Routed: the host routes traffic for the VM subnet to the LAN without NAT (other routers must know the route)
  • Open: libvirt creates the network but adds no firewall rules; you manage them

Choosing

Server VMs that other machines must reach: bridged. Quick outbound internet for a test VM: NAT. Malware lab: isolated.

Practice questions

Answer in your head, then open the answer. Ranger's Terminal has 514 questions like these for Linux+, with mock exams.

Question 1Before upgrading nginx, Sam wants a gzip-compressed archive of /etc/nginx named nginx-etc.tar.gz. Which command creates it?

  1. tar -xzf nginx-etc.tar.gz /etc/nginx
  2. tar -czf nginx-etc.tar.gz /etc/nginx
  3. tar -tzf nginx-etc.tar.gz /etc/nginx
  4. tar -cjf nginx-etc.tar.gz /etc/nginx
Show the answer

Answer: B. tar -czf nginx-etc.tar.gz /etc/nginx

-c creates, -z filters through gzip and -f names the archive file. -j uses bzip2 and -J uses xz.

Question 2Sam has to restore a single site's files from site-backup.tar.xz into /srv/restore without touching the live /srv/www. Which command does this?

  1. tar -cJf site-backup.tar.xz -C /srv/restore
  2. tar -xzf site-backup.tar.xz -C /srv/restore
  3. tar -xJf site-backup.tar.xz > /srv/restore
  4. tar -xJf site-backup.tar.xz -C /srv/restore
Show the answer

Answer: D. tar -xJf site-backup.tar.xz -C /srv/restore

-x extracts, -J handles xz compression and -C changes to the target directory first. GNU tar can also detect compression automatically when extracting.

Question 3Before restoring, Sam wants to confirm that orders-2026-10.tar.gz contains the file srv/orders/refunds.csv without extracting anything. Which command shows the contents?

  1. tar -czf orders-2026-10.tar.gz
  2. tar -rzf orders-2026-10.tar.gz
  3. tar -xzf orders-2026-10.tar.gz
  4. tar -tzf orders-2026-10.tar.gz
Show the answer

Answer: D. tar -tzf orders-2026-10.tar.gz

-t lists the members of an archive. Piping to grep refunds finds the file quickly.

Question 4A legacy recovery script on files01 runs find /srv/data | cpio -ov > data.cpio. How does cpio decide which files go into the archive?

  1. It archives the current directory recursively by default
  2. It reads the names from a file named in the -o option
  3. It reads the list of file names from standard input
  4. It archives every file matching its last argument
Show the answer

Answer: C. It reads the list of file names from standard input

cpio -o (copy-out) builds an archive from the names on stdin, usually from find. cpio -idv < data.cpio extracts (copy-in), with -d creating directories.

Question 5Sam runs gzip big-export.csv to save space, and later the import script fails because big-export.csv is gone. What happened, and how could he have kept the original?

  1. gzip swapped it for big-export.csv.gz; -k keeps it
  2. gzip moved the file to /tmp; gzip -c would have left it in place
  3. gzip deleted the file by mistake; gzip -t would have prevented it
  4. gzip renamed it to .bak; gzip -r would have kept it
Show the answer

Answer: A. gzip swapped it for big-export.csv.gz; -k keeps it

By default gzip, bzip2 and xz replace FILE with FILE.gz/.bz2/.xz. -k (keep) leaves the original; gunzip or gzip -d restores it.

Practice it in Ranger's TerminalThe same lessons as a story, with drills, hands-on labs and mock exams. Free, in your browser.
Play free

CompTIA, A+, Network+, Security+ and Linux+ are trademarks of CompTIA, Inc. Ranger Gamez is not affiliated with or endorsed by CompTIA. The practice questions are written to the published exam objectives; they are not real exam questions.