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.


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.

| improve this answer | |
  • Even better: sed --in-place 's/image_hostname/hostname1/g' – Cristian Ciupitu Sep 7 '09 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 '11 at 13:10

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.

| improve this answer | |

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.

| improve this answer | |
On Debian, modify:

And run:
rm -f /etc/ssh/ssh_host*
dpkg-reconfigure openssh-server
| improve this answer | |

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


[ -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.

| improve this answer | |
  • and /etc/hosts has to be edited too so that the new hostname AND IP address are in there. – cas Sep 7 '09 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 '09 at 22:11
  • You can edit those comments directly into your answer, and probably should. – Phil Miller Sep 8 '09 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.