Learn how to change UID or GID safely in Linux. Also, know how to switch UID between two users and GID between two groups without impacting files ownership they own.
In this article, we will walk you through to change UID or GID of existing users or groups without affecting file ownership owned by them. Later, we also explained how to switch GID between two groups and how to switch UID between two users on the system without affecting file ownership owned by them.
Let’s start with changing UID or GID on the system.
Current scenario :
User shrikant with UID 1001 Group sysadmin with GID 2001
Expected scenario :
User shrikant with UID 3001 Group sysadmin with GID 4001
Changing GID and UID is simple using usermod or groupmod command, but you have to keep in mind that after changing UID or GID you need to change ownership of all files owned by them manually since file ownership is known to the kernel by GID and UID, not by username.
Now, search and change all file’s ownership owned by this user or group with for loop
root@kerneltalks # for i in `find / -user 1001`; do chown 3001 $i; done
root@kerneltalks # for i in `find / -group 2001`; do chgrp 4001 $i; done
OR
root@kerneltalks # find / -user 1001 -exec chown -h shrikant {} \;
root@kerneltalks # find / -group 2001 -exec chgrp -h sysadmin {} \;
That’s it. You have safely changed UID and GID on your system without affecting any file ownership owned by them!
How to switch GID of two groups
Current scenario :
Group sysadmin with GID 1111 Group oracle with GID 2222
Expected scenario :
Group sysadmin with GID 2222 Group oracle with GID 1111
In the above situation, we need to use one intermediate GID which is currently not in use on your system. Check /etc/group file and select one GID XXXX which is not present in a file. In our example, we take 9999 as intermediate GID.
Now, the process is simple –
Change sysadmin GID to 9999
Find and change the group of all files owned by GID 1111 to sysadmin
Change oracle GID to 1111
Find and change the group of all files owned by GID 2222 to oracle
Change sysadmin GID to 2222
Find and change the group of all files owned by GID 9999 to sysadmin
Learn how to safely remove the disk from LVM. It’s useful when you need to free up disks from the volume group and re-use somewhere else or replace a faulty disk.
This article will serve solution for below questions :
How to safely remove the disk from LVM
How to remove the disk from VG online
How to copy data from one disk to other at the physical level
How to replace a faulty disk in LVM online
How to move physical extents from one disk to another
How to free up disk from VG to shrink VG size
How to safely reduce VG
We have volume group named vg01 which has 20M logical volume created in it and mounted it on /mydata mount point. Check lsblk output below –
root@kerneltalks # lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 10G 0 disk
├─xvda1 202:1 0 1M 0 part
└─xvda2 202:2 0 10G 0 part /
xvdf 202:80 0 1G 0 disk
└─vg01-lvol1 253:0 0 20M 0 lvm /mydata
Now, attach new disk of the same or bigger size of the disk /dev/xvdf. Identify the new disk on the system by using lsblk command again and comparing the output to the previous one.
root@kerneltalks # lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 10G 0 disk
├─xvda1 202:1 0 1M 0 part
└─xvda2 202:2 0 10G 0 part /
xvdf 202:80 0 1G 0 disk
└─vg01-lvol1 253:0 0 20M 0 lvm /mydata
xvdg 202:96 0 1G 0 disk
You can see the new disk has been identified as /dev/xvdg. Now, we will add this disk to current VG vg01. This can be done using vgextend command. Obviously, before using it in LVM you need to run pvcreate on it.
Observe the above output. Since we created a 20M mount point from disk /dev/xvdf it has 20M less free size. The new disk /dev/xvdg is completely free.
Now, we need to move physical extents from disk xvdf to xvdg. pvmove is the command used to achieve this. You just need to supply a disk name from where you need to move out PE. Command will move PE out of that disk and write them to all available disks in the same volume group. In our case, only one other disk is available to move PE.
Move progress is shown periodically. If due to any reason operation interrupted in between then moved PE will remain at destination disks and un-moved PEs will remain on the source disk. The operation can be resumed by issuing the same command again. It will then move the remaining PE out of the source disk.
In the above command, it will run pvmove in the background. It will redirect normal console outputs in normal.log file under the current working directory whereas errors will be redirected and saved in error.log file in the current working directory.
Now if you check pvs output again, you will find all space on disk xvdf is free which means its not been used to store any data in that VG. This ensures you can remove the disk without any issues.
Before removing/detaching disk from the server, you need to remove it from LVM. You can do this by reducing VG and opting for that disk out.
root@kerneltalks # vgreduce vg01 /dev/xvdf
Removed "/dev/xvdf" from volume group "vg01"
Now disk xvdf can be removed/detached from server safely.
Few useful switches of pvmove :
Verbose mode prints more detailed information on the operation. It can be invoked by using -v switch.
root@kerneltalks # pvmove -v /dev/xvdf
Cluster mirror log daemon is not running.
Wiping internal VG cache
Wiping cache of LVM-capable devices
Archiving volume group "vg01" metadata (seqno 17).
Creating logical volume pvmove0
activation/volume_list configuration setting not defined: Checking only host tags for vg01/lvol1.
Moving 5 extents of logical volume vg01/lvol1.
activation/volume_list configuration setting not defined: Checking only host tags for vg01/lvol1.
Creating vg01-pvmove0
Loading table for vg01-pvmove0 (253:1).
Loading table for vg01-lvol1 (253:0).
Suspending vg01-lvol1 (253:0) with device flush
Resuming vg01-pvmove0 (253:1).
Resuming vg01-lvol1 (253:0).
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 18).
activation/volume_list configuration setting not defined: Checking only host tags for vg01/pvmove0.
Checking progress before waiting every 15 seconds.
/dev/xvdf: Moved: 0.00%
/dev/xvdf: Moved: 100.00%
Polling finished successfully.
The interval at which command updates the progress can be changed. -i switch followed by a number of seconds can be used to get updates from command on user-defined intervals on progress.
How to guide to boot Suse Linux from old kernel after kernel upgrade.
This article is basically a how-to guide for booting SUSE Linux system from the previous kernel after the kernel upgrade process. Normally, Linux like Red Hat has the option to just change boot priority of kernel in /etc/grub.conf and reboot into the kernel of your choice. But in SUSE Linux, we do not have that option. Now the question is how to boot into old kernel once I upgrade the kernel.
You can boot into the older kernel by using the below method. I explained kernel upgrade first and then how to uninstall update to boot from the older kernel. This is a kind of rollback kernel upgrade in SUSE Linux.
1. Upgrade kernel in Suse Linux
The first thing you want to check and confirm that if your SUSE supports multiversion or not. Go to /etc/zypp/zypp.conf and make sure the below-mentioned line is not commented on. If there is # at the beginning of it, remove it.
multiversion = provides:multiversion(kernel)
There are many ways to maintain how many old kernel versions can be maintained by the system. We won’t be going through it. You can find more details about it here.
Once you are confirmed, multiversion is active then go ahead with kernel upgrade. If it’s not activated, zypper will auto-delete old kernel and you won’t be able to use it.
root@kerneltalks # uname -a
Linux kerneltalks 4.4.114-94.11-default #1 SMP Thu Feb 1 19:28:26 UTC 2018 (4309ff9) x86_64 x86_64 x86_64 GNU/Linux
Install the new kernel version using zypper. Make sure you are installing the new kernel and not updating your current one.
root@kerneltalks # zypper in kernel-default
Refreshing service 'SMT-http_smt-ec2_susecloud_net'.
Refreshing service 'cloud_update'.
Loading repository data...
Reading installed packages...
Resolving package dependencies...
The following 5 NEW packages are going to be installed:
crash-kmp-default crda kernel-default-4.4.120-94.17.1 kernel-firmware
wireless-regdb
5 new packages to install.
Overall download size: 81.3 MiB. Already cached: 0 B. After the operation,
additional 358.3 MiB will be used.
Continue? [y/n/...? shows all options] (y): y
Retrieving package kernel-default-4.4.120-94.17.1.x86_64
(1/5), 38.6 MiB (167.2 MiB unpacked)
Retrieving: kernel-default-4.4.120-94.17.1.x86_64.rpm ........[done (7.1 MiB/s)]
Retrieving package kernel-firmware-20170530-21.19.1.noarch
(2/5), 42.5 MiB (191.1 MiB unpacked)
Retrieving: kernel-firmware-20170530-21.19.1.noarch.rpm .....[done (18.2 MiB/s)]
Retrieving package wireless-regdb-2017.12.23-4.3.1.noarch
(3/5), 14.1 KiB ( 13.0 KiB unpacked)
Retrieving: wireless-regdb-2017.12.23-4.3.1.noarch.rpm ...................[done]
Retrieving package crash-kmp-default-7.1.8_k4.4.92_6.30-4.6.2.x86_64
(4/5), 116.8 KiB ( 7.8 KiB unpacked)
Retrieving: crash-kmp-default-7.1.8_k4.4.92_6.30-4.6.2.x86_64.rpm ........[done]
Retrieving package crda-1.1.3-4.2.1.x86_64 (5/5), 14.4 KiB ( 34.5 KiB unpacked)
Retrieving: crda-1.1.3-4.2.1.x86_64.rpm ..................................[done]
Checking for file conflicts: .............................................[done]
(1/5) Installing: kernel-default-4.4.120-94.17.1.x86_64 ..................[done]
Additional rpm output:
Creating initrd: /boot/initrd-4.4.120-94.17-default
dracut: Executing: /usr/bin/dracut --logfile /var/log/YaST2/mkinitrd.log --force /boot/initrd-4.4.120-94.17-default 4.4.120-94.17-default
dracut: dracut module 'btrfs' will not be installed, because command 'btrfs' cou ld not be found!
dracut: dracut module 'dmraid' will not be installed, because command 'dmraid' c ould not be found!
dracut: dracut module 'mdraid' will not be installed, because command 'mdadm' co uld not be found!
dracut: dracut module 'btrfs' will not be installed, because command 'btrfs' cou ld not be found!
dracut: dracut module 'dmraid' will not be installed, because command 'dmraid' c ould not be found!
dracut: dracut module 'mdraid' will not be installed, because command 'mdadm' co uld not be found!
dracut: *** Including module: bash ***
dracut: *** Including module: systemd ***
dracut: *** Including module: systemd-initrd ***
dracut: *** Including module: i18n ***
dracut: No KEYMAP configured.
dracut: *** Including module: xen-tools-domU ***
dracut: *** Including module: kernel-modules ***
dracut: *** Including module: rootfs-block ***
dracut: *** Including module: suse-xfs ***
dracut: *** Including module: terminfo ***
dracut: *** Including module: udev-rules ***
dracut: Skipping udev rule: 40-redhat.rules
dracut: Skipping udev rule: 50-firmware.rules
dracut: Skipping udev rule: 50-udev.rules
dracut: Skipping udev rule: 91-permissions.rules
dracut: Skipping udev rule: 80-drivers-modprobe.rules
dracut: *** Including module: dracut-systemd ***
dracut: *** Including module: haveged ***
dracut: *** Including module: usrmount ***
dracut: *** Including module: base ***
dracut: *** Including module: fs-lib ***
dracut: *** Including module: shutdown ***
dracut: *** Including module: suse ***
dracut: *** Including modules done ***
dracut: *** Installing kernel module dependencies and firmware ***
dracut: *** Installing kernel module dependencies and firmware done ***
dracut: *** Resolving executable dependencies ***
dracut: *** Resolving executable dependencies done***
dracut: *** Hardlinking files ***
dracut: *** Hardlinking files done ***
dracut: *** Stripping files ***
dracut: *** Stripping files done ***
dracut: *** Generating early-microcode cpio image ***
dracut: *** Store current command line parameters ***
dracut: Stored kernel commandline:
dracut: root=UUID=26fa33a2-ad40-4a85-a495-402aca6a2127 rootfstype=ext4 rootflag s=rw,relatime,data=ordered
dracut: *** Creating image file '/boot/initrd-4.4.120-94.17-default' ***
dracut: *** Creating initramfs image file '/boot/initrd-4.4.120-94.17-default' d one ***
(2/5) Installing: kernel-firmware-20170530-21.19.1.noarch ................[done]
(3/5) Installing: wireless-regdb-2017.12.23-4.3.1.noarch .................[done]
(4/5) Installing: crash-kmp-default-7.1.8_k4.4.92_6.30-4.6.2.x86_64 ......[done]
(5/5) Installing: crda-1.1.3-4.2.1.x86_64 ................................[done]
Output of kernel-firmware-20170530-21.19.1.noarch.rpm %posttrans script:
Creating initrd: /boot/initrd-4.4.114-94.11-default
dracut: Executing: /usr/bin/dracut --logfile /var/log/YaST2/mkinitrd.log --f orce /boot/initrd-4.4.114-94.11-default 4.4.114-94.11-default
dracut: dracut module 'btrfs' will not be installed, because command 'btrfs' could not be found!
dracut: dracut module 'dmraid' will not be installed, because command 'dmrai d' could not be found!
dracut: dracut module 'mdraid' will not be installed, because command 'mdadm ' could not be found!
dracut: dracut module 'btrfs' will not be installed, because command 'btrfs' could not be found!
dracut: dracut module 'dmraid' will not be installed, because command 'dmrai d' could not be found!
dracut: dracut module 'mdraid' will not be installed, because command 'mdadm ' could not be found!
dracut: *** Including module: bash ***
dracut: *** Including module: systemd ***
dracut: *** Including module: systemd-initrd ***
dracut: *** Including module: i18n ***
dracut: No KEYMAP configured.
dracut: *** Including module: xen-tools-domU ***
dracut: *** Including module: kernel-modules ***
dracut: *** Including module: rootfs-block ***
dracut: *** Including module: suse-xfs ***
dracut: *** Including module: terminfo ***
dracut: *** Including module: udev-rules ***
dracut: Skipping udev rule: 40-redhat.rules
dracut: Skipping udev rule: 50-firmware.rules
dracut: Skipping udev rule: 50-udev.rules
dracut: Skipping udev rule: 91-permissions.rules
dracut: Skipping udev rule: 80-drivers-modprobe.rules
dracut: *** Including module: dracut-systemd ***
dracut: *** Including module: haveged ***
dracut: *** Including module: usrmount ***
dracut: *** Including module: base ***
dracut: *** Including module: fs-lib ***
dracut: *** Including module: shutdown ***
dracut: *** Including module: suse ***
dracut: *** Including modules done ***
dracut: *** Installing kernel module dependencies and firmware ***
dracut: *** Installing kernel module dependencies and firmware done ***
dracut: *** Resolving executable dependencies ***
dracut: *** Resolving executable dependencies done***
dracut: *** Hardlinking files ***
dracut: *** Hardlinking files done ***
dracut: *** Stripping files ***
dracut: *** Stripping files done ***
dracut: *** Generating early-microcode cpio image ***
dracut: *** Store current command line parameters ***
dracut: Stored kernel commandline:
dracut: root=UUID=26fa33a2-ad40-4a85-a495-402aca6a2127 rootfstype=ext4 root flags=rw,relatime,data=ordered
dracut: *** Creating image file '/boot/initrd-4.4.114-94.11-default' ***
dracut: *** Creating initramfs image file '/boot/initrd-4.4.114-94.11-defaul t' done ***
Creating initrd: /boot/initrd-4.4.120-94.17-default
dracut: Executing: /usr/bin/dracut --logfile /var/log/YaST2/mkinitrd.log --f orce /boot/initrd-4.4.120-94.17-default 4.4.120-94.17-default
dracut: dracut module 'btrfs' will not be installed, because command 'btrfs' could not be found!
dracut: dracut module 'dmraid' will not be installed, because command 'dmrai d' could not be found!
dracut: dracut module 'mdraid' will not be installed, because command 'mdadm ' could not be found!
dracut: dracut module 'btrfs' will not be installed, because command 'btrfs' could not be found!
dracut: dracut module 'dmraid' will not be installed, because command 'dmrai d' could not be found!
dracut: dracut module 'mdraid' will not be installed, because command 'mdadm ' could not be found!
dracut: *** Including module: bash ***
dracut: *** Including module: systemd ***
dracut: *** Including module: systemd-initrd ***
dracut: *** Including module: i18n ***
dracut: No KEYMAP configured.
dracut: *** Including module: xen-tools-domU ***
dracut: *** Including module: kernel-modules ***
dracut: *** Including module: rootfs-block ***
dracut: *** Including module: suse-xfs ***
dracut: *** Including module: terminfo ***
dracut: *** Including module: udev-rules ***
dracut: Skipping udev rule: 40-redhat.rules
dracut: Skipping udev rule: 50-firmware.rules
dracut: Skipping udev rule: 50-udev.rules
dracut: Skipping udev rule: 91-permissions.rules
dracut: Skipping udev rule: 80-drivers-modprobe.rules
dracut: *** Including module: dracut-systemd ***
dracut: *** Including module: haveged ***
dracut: *** Including module: usrmount ***
dracut: *** Including module: base ***
dracut: *** Including module: fs-lib ***
dracut: *** Including module: shutdown ***
dracut: *** Including module: suse ***
dracut: *** Including modules done ***
dracut: *** Installing kernel module dependencies and firmware ***
dracut: *** Installing kernel module dependencies and firmware done ***
dracut: *** Resolving executable dependencies ***
dracut: *** Resolving executable dependencies done***
dracut: *** Hardlinking files ***
dracut: *** Hardlinking files done ***
dracut: *** Stripping files ***
dracut: *** Stripping files done ***
dracut: *** Generating early-microcode cpio image ***
dracut: *** Store current command line parameters ***
dracut: Stored kernel commandline:
dracut: root=UUID=26fa33a2-ad40-4a85-a495-402aca6a2127 rootfstype=ext4 root flags=rw,relatime,data=ordered
dracut: *** Creating image file '/boot/initrd-4.4.120-94.17-default' ***
dracut: *** Creating initramfs image file '/boot/initrd-4.4.120-94.17-defaul t' done ***
Reboot system and you see your system is booted with the latest new kernel.
root@kerneltalks # uname -a
Linux kerneltalks 4.4.120-94.17-default #1 SMP Wed Mar 14 17:23:00 UTC 2018 (cf3a7bb) x86_64 x86_64 x86_64 GNU/Linux
Now check all the installed kernel packages on your system using –
root@kerneltalks # zypper se -si 'kernel*'
Refreshing service 'SMT-http_smt-ec2_susecloud_net'.
Refreshing service 'cloud_update'.
Loading repository data...
Reading installed packages...
S | Name | Type | Version | Arch | Repository
---+-----------------+---------+------------------+--------+-------------------
i+ | kernel-default | package | 4.4.120-94.17.1 | x86_64 | SLES12-SP3-Updates
i+ | kernel-default | package | 4.4.114-94.11.3 | x86_64 | SLES12-SP3-Updates
i | kernel-firmware | package | 20170530-21.19.1 | noarch | SLES12-SP3-Updates
Here, you can see there are two kernels installed on the system. Old one is 4.4.114-94.11.3 and the new one is 4.4.120-94.17.1 from which the current system is booted.
2. Boot from the old kernel in SUSE Linux
For Suse with GRUB2
Now, if you want to boot the system from the old kernel 4.4.114-94.11.3 without un-installing new kernel then follow the below steps.
Make copy of /etc/default/grub file as a backup. and then edit it –
root@kerneltalks # cp /etc/default/grub /etc/default/grub.backup
root@kerneltalks # vi /etc/default/grub
Look for GRUB_DEFAULT=0and edit the number per old kernel menu number. Old kernel menu number can be found in /boot/grub2/grub.cfg
Open /boot/grub2/grub.cfg and look for entry menuentry You will be able to see different kernel entries in it. First being 0 and then counter goes on. Check and choose the menu number of your old kernel.
After editing /etc/default/grub file you need to re-create /boot/grub2/grub.cfg You can do it with below command –
root@kerneltalks # grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.120-94.17-default
Found initrd image: /boot/initrd-4.4.120-94.17-default
Found linux image: /boot/vmlinuz-4.4.114-94.11-default
Found initrd image: /boot/initrd-4.4.114-94.11-default
done
Once done, reboot the system. That’s it. You can see your system is booted with an old kernel while your new kernel is still installed on the server.
For Suse with GRUB
Edit /boot/grub/grub.conf which is also link to /boot/grub/menu.lst . Look for parameter default 0 and change the number 0 to your desired kernel menu number.
You can see the kernel list to be displayed later in the same file. Remember, the numbering starts at 0. So countdown to your old kernel version number and use it for the default parameter.
Save the file and reboot the system. You will be booted with old kernel.
3. Rollback to old kernel in SUSE Linux
Now if you want to rollback system to the old kernel 4.4.114-94.11.3, remove this latest installed kernel 4.4.120-94.17.1. You need to give kernel name as <package_name>-<package_version>. You can get the version from the above command output. In this way, we are downgrading kernel in SUSE Linux.
root@kerneltalks # zypper rm kernel-default-4.4.120-94.17.1
Loading repository data...
Reading installed packages...
Resolving package dependencies...
The following package is going to be REMOVED:
kernel-default-4.4.120-94.17.1
1 package to remove.
After the operation, 167.2 MiB will be freed.
Continue? [y/n/...? shows all options] (y): y
(1/1) Removing kernel-default-4.4.120-94.17.1.x86_64 .............................................................................................................[done]
There are some running programs that might use files deleted by recent upgrade. You may wish to check and restart some of them. Run 'zypper ps -s' to list these programs.
Now as per warning lets check which all processes are using it.
root@kerneltalks # zypper ps -s
The following running processes use deleted files:
PID | PPID | UID | User | Command | Service
----+------+-----+------+---------------+--------------
486 | 1 | 0 | root | systemd-udevd | systemd-udevd
You may wish to restart these processes.
See 'man zypper' for information about the meaning of values in the above table.
Lets reboot system and check kernel version.
root@kerneltalks # uname -a
Linux kerneltalks 4.4.114-94.11-default #1 SMP Thu Feb 1 19:28:26 UTC 2018 (4309ff9) x86_64 x86_64 x86_64 GNU/Linux
You can see the system is booted with your old kernel 4.4.114-94.11.3!
Now, Check installed kernel packages and you can see a newer kernel package is no more installed/active on the system.
root@kerneltalks # zypper se -si 'kernel*'
Refreshing service 'SMT-http_smt-ec2_susecloud_net'.
Refreshing service 'cloud_update'.
Loading repository data...
Reading installed packages...
S | Name | Type | Version | Arch | Repository
---+-----------------+---------+------------------+--------+-------------------
i+ | kernel-default | package | 4.4.114-94.11.3 | x86_64 | SLES12-SP3-Updates
i | kernel-firmware | package | 20170530-21.19.1 | noarch | SLES12-SP3-Updates
If you have another method (command line) to boot into the older kernel then please share in the comments below.