Linux: Hot Clone a Live *nix Machine

This article describes how to clone a live production linux server to a VM on ESX infrastructure although it is the same process to clone to another physical machine.

Prepare the Target Machine

Prepare a new server on ESX debian 32 bit machine, same hdd size etc:

In the past I’ve always used the DSL (Damn Small Linux) distro to boot from but this time I noticed that it didn’t pick up the hard drive on ESX so rather than mess around I tried Puppy Linux – it’s 160 MB compared to DSL’s 50MB but the hassle factor decided it for me and I’m very happy with it 🙂 

 

Check your fstab:

 

root@localhost:~# cat /etc/fstab
LABEL=DOROOT       /               ext4    errors=remount-ro 0       1


root@localhost:~# blkid /dev/disk/by-label/DOROOT
/dev/disk/by-label/DOROOT: LABEL="DOROOT" UUID="2c342fc9-3fcd-42fb-a837-1135ce07fe9c" TYPE="ext4"

Here we only have one filesystem to worry about – “/” – so it should be a straightforward exercise.

Make a single partition on our new VM (no swap partition on original – might need to address this)

Commands:

# fdisk /dev/sda (may be different to sda in your case, check your dmesg output)

Then “n” to add a new partition and accept the defaults for the start and finish – these will be the whole device. When done, enter “w” to write the partition to disk.

We also need to make it bootable so back into fdisk. “p” will print the partition table and here we can see /dev/sda1. Enter “a” to make a partition bootable and then the partition number; in this case we only have one partition butbasically you make the partition that contains “/boot” bootable. Again, enter “w” to write the partition to disk:

Format our partition using the same filesystem as our source machine – ext4 in this case:

Make a mount directory (remember we’re still in puppy linux in memory) and mount the hard drive partition(s) to it. Then create our other parts of the filesystem – dev, sys, proc and tmp:

Copy the Live System to the Target Machine

Make sure VM has connectivity to live source machine and perform the rysnc:

    rsync -aHxvz root@1.2.3.4:/* /mount --exclude=/dev --exclude=/proc --exclude=/sys --exclude=/tmp

The switches used are as follows:

  • -a – archive mode – a shortcut to avoid using multiple switches and ideal for backups – serverfault has a good description.
  • -H – preserve hard links – not necessary for a backup but certainly for cloning
  • -x – preserve extended attributes
  • -v – verbose output – keep an eye on what’s going on
  • -z – although it’s not on the screenshot above, use this to enable end-to-end compression.

Modify the Filesystem Table (fstab) and Install Grub

Update the filesystem table (fstab) if necessary. This is currently under /mount/etc/fstab – info on the format can be found here.

Install the grub bootmanager using the following command:

# grub-install --root-directory=/mnt/sda1 /dev/sda

Once it’s done, say a quick prayer and reboot, hopefully job done!

** If, however, you run into errors (like the “/dev/sda does not have any corresponding BIOS drive” error) and you don’t have enough time or experience with grub configuration, my advice would be to download the most illustrious Boot Repair Disk and allow it to install / reconfigure your grub.

And then it really is job done 🙂

Cleaning Up!

Keyboard Map

For some reason, the keyboard map had changed, meaning that I my root password appeared to be wrong. Once I got logged in, a quick keyboard mapping sorted it out – easily done using the following command:

dpkg-reconfigure console-data

NIC Configuration

Your new machine will also have the same network configuration as your live one and will most likely need reconfigured – see the debian wiki for step-by-step instructions on how to achieve what you need. The main config file is /etc/network/interfaces.

/tmp Permissions

Make sure your /tmp directory has correct permissions set; you may not notice this until a daemon fails e.g. mysqld because it can’t write to the directory

# chown root:root /tmp
# chmod 1777 /tmp

 

Linux: Cut Down on the Information Leaked by Apache2 Webserver

It’s a given that information leakage in the form of server / mod versions can seriously aid an attacker in compromising your server and / or web application. By cutting down the amount of information that your server freely surrenders you can make the attacker’s job that much harder – these very quick tips will do just that!

ServerTokens

In your /etc/apache2/conf.d/security file, look for “ServerTokens” and set the parameter to “Prod” – this will identify the server software only, no versions, or extensions.

# ServerTokens
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.
# Set to one of:  Full | OS | Minimal | Minor | Major | Prod
# where Full conveys the most information, and Prod the least.
#
ServerTokens Prod

Description:

ServerTokens Prod[uctOnly]
Server sends (e.g.): Server: Apache
ServerTokens Major
Server sends (e.g.): Server: Apache/2
ServerTokens Minor
Server sends (e.g.): Server: Apache/2.0
ServerTokens Min[imal]
Server sends (e.g.): Server: Apache/2.0.41
ServerTokens OS
Server sends (e.g.): Server: Apache/2.0.41 (Unix)
ServerTokens Full (or not specified)
Server sends (e.g.): Server: Apache/2.0.41 (Unix) PHP/4.2.2 MyMod/1.2

expose_php

In the same vein, we want to remove any information shown by the php install which is done by setting the “expose_php” directive to “Off”.

Locate this in the /etc/php5/apache2/php.ini file and set accordingly:

; Decides whether PHP may expose the fact that it is installed on the server
; (e.g. by adding its signature to the Web server header).  It is no security
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
; http://php.net/expose-php
expose_php = Off

Linux: Recursively FTP Directories Using CLI Using ‘wget’

So you want to recursively copy full FTP directory structures but don’t want to use a GUI client (or can’t)?

Everyone seems to resort to ‘mget’ or multiple-get on the command line but this does not do recursive copies.

The best way is to use wget which will do *exactly* what you want – copy the directories and store them in their original structure. This is incredibly easy to do as follows:

wget -r --user myusername --password mypassword ftp://ftp.mydomain.co.uk/mysite

You’ll see a bunch of entries – one for each file – as follows, showing that it’s working 🙂
--2013-11-16 12:36:48--  ftp://ftp.mydomain.co.uk/websites/testfile.txt

=> `ftp://ftp.mydomain.co.uk/websites/testfile.txt'
==> CWD not required.
==> PASV ... done.    ==> RETR testfile.txt ... done.
Length: 11897 (12K)

100%[==============================================================================>]
11,897      24.1K/s   in 0.5s

Exit mobile version
%%footer%%