3

i was reading this question How to rename machines after image install and I was wondering what you do for a linux machine. I am a windows guy and am clueless about linux. If i have a linux image or VM, is there a NewSid or SysPrep program that I have to run if I restore the image to a new machine?

I am asking more about a NewSid program rather than SysPrep, I want to make an image of an already installed machine, I don't need sysPrp to do the installing for me.

2

5 Answers 5

3

It depends on the Linux distribution but generally it means adjusting a few files in /etc for hostname and IP details. On RedHat-flavoured distros you would have to change:

  • /etc/hosts: hostname and IP addresses
  • /etc/sysconfig/network: hostname
  • /etc/sysconfig/network-scripts/ifcfg-eth*: network configuration

grep -r image_hostname /etc and grep -r image.ip.add.ress /etc would find anything you need to edit. Changing the files won't make the changes take immediately -- you'd either have to reboot or manually apply them by updating the hostname (using the hostname command) and restarting networking with service network restart.

2
  • Even better: sed --in-place 's/image_hostname/hostname1/g' Sep 7, 2009 at 20:59
  • the automated search and replace might be a little dangerous if your old or new hostname is similar to words found elsewhere in configuration files...
    – Norky
    Jun 2, 2011 at 13:10
3

Modern Linux distributions will remember the MAC address of the card they were first installed to. Booting them on any other machine may well cause your eth0 to become eth1 when you least expect it.

The solution is to remove the file that maintains the persistent mapping between your MAC address and ethernet interface, which (on Debian/Ubuntu at least; this is one area that is very distro-specific) is /etc/udev/75-persistent-net-generator.rules.

1
  • Yup, good point. Sep 7, 2009 at 19:26
2

This answer doesn't answer your question, but is more to add a different solution to the imaging problem.

The alternative to using a system image is to use something like preseeding for Debian/Ubuntu or kickstart for CentOS/Redhat/Fedora to install a base system and then use something like Puppet, CFEngine2 or Chef to configure the rest of the system. This gives you the flexibility to change your setup, and can be used to keep the configuration in sync after you've installed the initial system. This won't take significantly longer to install the system, and will reduce the time to produce your images.

2
On Debian, modify:
/etc/hostname
/etc/hosts
/etc/mailname
/etc/resolv.conf
/etc/postfix/main.cf
/etc/exim4/update-exim4.conf.conf
/etc/udev/rules.d/z25_persistent-net.rules

And run:
/etc/init.d/hostname.sh
rm -f /etc/ssh/ssh_host*
dpkg-reconfigure openssh-server
1

i currently use the following script when i clone virtual machines. I've been using the same technique since the mid 90s when i was regularly cloning debian boxes to act as gateway/firewall machines for schools.

#! /bin/bash

# change hostname of virtual machine from "$1" to "$2"

error() {
  echo "Usage: $0 OldName NewName" >&2
  exit 1
}

OLD="$1"
NEW="$2"

[ -z "$OLD" -o -z "$NEW" ] && error

find /etc -type f -print0 | \
        xargs -0r grep -l "\b$OLD\b" | \
        egrep -v 'ssh|\.db' | \
        xargs -d "\n" -r sed -i -e "s/$OLD/$NEW/g"

there are a few manual changes as well that i haven't got around to automating yet, mostly because i don't do it often enough that it's worth spending the time to figure out.

  1. if the cloned machine has hard-coded IP address(es) rather than DHCP, i edit /etc/network/interfaces.

  2. edit /etc/udev/rules.d/70-persistent-net.rules so that the interface names (eth0 & eth1) get assigned to the correct MAC addresses (otherwise they'll get eth2 & eth3 because eth0 & eth1 are already defined).

  3. delete and regenerate the host's ssh key.

3
  • and /etc/hosts has to be edited too so that the new hostname AND IP address are in there.
    – cas
    Sep 7, 2009 at 22:08
  • BTW, this little script is in /root/scripts on the source image (a minimal/base debian install) that i clone when i make new VMs, so that it's there to be run on the clone. i put it there rather than /usr/local/sbin because it's only ever used once when the VM is cloned.
    – cas
    Sep 7, 2009 at 22:11
  • You can edit those comments directly into your answer, and probably should. Sep 8, 2009 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .