You Just Installed Red Hat? Now It’s Time to Fix the Names

Red Hat

The installer for Red Hat Enterprise Linux ported faithfully to CentOS, doesn’t give you a lot of control. Or at least not obvious control. You can work around some of the annoyances if you know the right tricks. Let’s look into fixing some of them after the fact. This will be a usefully challenging test of our understanding of Linux administration!

The Problem

I installed CentOS 7, actually the 7.2.1511 update. I started with DHCP, planning on changing IP address and hostname after the installation. I got this, which I expected:

[root@dhcp56 ~]# hostname

I also got this, which I had not anticipated:

[root@dhcp56 ~]# df -h -t xfs
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/centos_dhcp56-root   50G  5.0G   46G  10% /
/dev/mapper/centos_dhcp56-home   54G   63M   54G   1% /home
/dev/sda1                       497M  159M  338M  32% /boot


This means that the file system definitions need fixing. So do the underlying storage definitions encoded into LVM (or Logical Volume Management). And, as we explore in Learning Tree’s Linux server administration course, the boot loader passes a description of the root file system to the kernel. This includes LVM details when the root file system is on a logical volume, as happens by default on Red Hat Enterprise Linux and its derivatives.

If we aren’t careful, we will really wreck things and leave the system unable to boot!

Finding Files With the Odd Name

System configuration happens under /etc/. Let’s see if any files have unwanted names:

[root@dhcp56 ~]# find /etc/ -name '*dhcp56*'

Let’s see what those are:

[root@dhcp56 ~]# ls -l /etc/lvm/archive/ /etc/lvm/backup/
total 8
-rw-------. 1 root root 2160 Dec  1 12:52
-rw-------. 1 root root 2134 Dec  1 13:01

total 4
-rw-------. 1 root root 2133 Dec  1 13:01 centos_dhcp56

[root@dhcp56 ~]# file /etc/lvm/archive/* /etc/lvm/backup/*
/etc/lvm/archive/ ASCII text
/etc/lvm/archive/ ASCII text
/etc/lvm/backup/centos_dhcp56:                      ASCII text

Finding Files Containing the Odd Name

Those are the files with the unwanted string in their name. What about files that contain references to it?

[root@dhcp56 ~]# grep dhcp56 $( find /etc -type f | sort )
Binary file /etc/aliases.db matches
/etc/default/grub:GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet"
/etc/fstab:/dev/mapper/centos_dhcp56-root /      xfs   defaults  0 0
/etc/fstab:/dev/mapper/centos_dhcp56-home /home  xfs   defaults  0 0
/etc/fstab:/dev/mapper/centos_dhcp56-swap swap   swap  defaults  0 0
Binary file /etc/httpd/alias/cert8.db matches
/etc/lvm/archive/ = ""   # Linux 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64
/etc/lvm/archive/ {
/etc/lvm/archive/                     creation_host = ""
/etc/lvm/archive/                     creation_host = ""
/etc/lvm/archive/                     creation_host = ""
/etc/lvm/archive/ {
/etc/lvm/archive/                     creation_host = ""
/etc/lvm/archive/                     creation_host = ""
/etc/lvm/archive/                     creation_host = ""
/etc/lvm/backup/centos_dhcp56:centos_dhcp56 {
/etc/lvm/backup/centos_dhcp56:   creation_host = ""
/etc/lvm/backup/centos_dhcp56:   creation_host = ""
/etc/lvm/backup/centos_dhcp56:   creation_host = "

Finding Other References to the Odd Name

Not everything is contained in the /etc hierarchy of directories and files. Logical Volume Management or LVM can have the old hostname string embedded into the storage. Also, references to it may be passed from the boot loader to the kernel when the system boots.

[root@dhcp56 ~]# pvscan
  PV /dev/sda2   VG centos_dhcp56   lvm2 [111.30 GiB / 64.00 MiB free]
  Total: 1 [111.30 GiB] / in use: 1 [111.30 GiB] / in no VG: 0 [0   ]
[root@dhcp56 ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "centos_dhcp56" using metadata type lvm2
[root@dhcp56 ~]# lvscan
  ACTIVE            '/dev/centos_dhcp56/swap' [7.75 GiB] inherit
  ACTIVE            '/dev/centos_dhcp56/home' [53.48 GiB] inherit
  ACTIVE            '/dev/centos_dhcp56/root' [50.00 GiB] inherit

[root@dhcp56 ~]# cat /proc/cmdline | fmt
root=/dev/mapper/centos_dhcp56-root ro crashkernel=auto quiet

Changing Logical Volume Management Names

OK, let’s start changing things. Make sure that it’s safe to proceed! If we just installed the system and immediately blow it up, it’s no big deal to reinstall and get it right this time. But if we have been using the system at all, make sure you can revert to a good state before continuing.

Let’s rename the volume group. This also handles the logical volumes.

[root@dhcp56 ~]# vgrename centos_dhcp56 centos7
  Volume group "centos_dhcp56" successfully renamed to "centos7"

[root@dhcp56 ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "centos7" using metadata type lvm2
[root@dhcp56 ~]# lvscan
  ACTIVE            '/dev/centos7/swap' [7.75 GiB] inherit
  ACTIVE            '/dev/centos7/home' [53.48 GiB] inherit
  ACTIVE            '/dev/centos7/root' [50.00 GiB] inherit

Now we must edit /etc/fstab. Notice that it uses slightly different names: /dev/mapper/centos_dhcp56-swap instead of the anticipated /dev/centos_dhcp56/swap!

Now we edit /boot/grub2/grub.cfg and change the multiple instances of the old name. Also, edit /etc/default/grub. Otherwise, you will have a nasty surprise a few months from now when an updated kernel package comes out!

That was the riskiest part. Let’s reboot and make sure it works.

Fixing the LVM Archive

That’s great, the system boots! Now let’s fix the LVM archive in case we need it in the future.

[root@dhcp56 ~]# for F in $( find /etc -name '*dhcp56*' )
> do
     mv $F $( echo $F | sed 's/_dhcp56/7/' )
> done

Fixing the Other Files

All the other fixes have to do with file content under /etc. We could fix this by finding the list of instances of the old name in configuration files:

[root@dhcp56 ~]# grep dhcp56 $( find /etc -type f | sort )

However, let’s work smarter instead of harder! Let’s use a loop over a nested command substitution, and then used a less-common option to sed.

[root@dhcp56 ~]# for F in $( grep -l dhcp56 $( find /etc -type f | sort ) )
> do
>   sed -i 's/dhcp56/centos7/g' $F
> done

The innermost $(find ...) runs first, returning a list of files.

Then the $(grep ...) layer lists just those files containing that string.

The for loop processes those files.

Learning Tree’s Linux power tools course teaches about sed, command substitution, and loops. It’s a great follow-up for the introductory user course.

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.