Update linger file. with the rename.

This commit is contained in:
Storm Dragon 2025-04-26 04:14:26 -04:00
parent 3ebb6f36cf
commit 41a0592f20

View File

@ -8,73 +8,79 @@ if [[ $# -ne 2 ]]; then
exit 1
fi
old_user="$1"
new_user="$2"
oldUser="$1"
newUser="$2"
# Make sure old user exists
if ! id "$old_user" >/dev/null 2>&1; then
echo "The user $old_user does not exist."
if ! id "$oldUser" >/dev/null 2>&1; then
echo "The user $oldUser does not exist."
exit 1
fi
# Make sure old user is not logged in
if pgrep -u "$old_user" >/dev/null; then
echo "The user $old_user is currently logged in or has running processes. Cannot continue."
echo "Would you like to forcibly log out $old_user? (Y/N)"
if pgrep -u "$oldUser" >/dev/null; then
echo "The user $oldUser is currently logged in or has running processes. Cannot continue."
echo "Would you like to forcibly log out $oldUser? (Y/N)"
read -r answer
answer="${answer:0:1}"
answer="${answer^}"
if [[ "$answer" != "Y" ]]; then
exit 1
fi
loginctl terminate-user "$old_user"
loginctl terminate-user "$oldUser"
sleep 2
if pgrep -u "$old_user" >/dev/null; then
echo "The user $old_user still has running processes after termination. Cannot continue."
if pgrep -u "$oldUser" >/dev/null; then
echo "The user $oldUser still has running processes after termination. Cannot continue."
exit 1
fi
fi
# Make sure new user does not exist
if id "$new_user" >/dev/null 2>&1; then
echo "The user name $new_user is already taken."
if id "$newUser" >/dev/null 2>&1; then
echo "The user name $newUser is already taken."
exit 1
fi
# Make sure the new name is acceptable
if ! [[ "$new_user" =~ ^[a-z_][a-z0-9_-]{0,31}$ ]]; then
echo "$new_user is not an acceptable user name."
if ! [[ "$newUser" =~ ^[a-z_][a-z0-9_-]{0,31}$ ]]; then
echo "$newUser is not an acceptable user name."
exit 1
fi
# Passed all safety checks, proceed with the rename
groups=$(id -nG "$old_user")
groups=$(id -nG "$oldUser")
groups="${groups// /,}"
usermod -G "$groups" -m -d "/home/$new_user" -l "$new_user" "$old_user"
usermod -G "$groups" -m -d "/home/$newUser" -l "$newUser" "$oldUser"
# Update nodm.conf if it exists
if [[ -e /etc/nodm.conf ]]; then
sed -i -e "s#^NODM_USER=.*#NODM_USER='$new_user'#" -e "s#^NODM_XSESSION=.*#NODM_XSESSION='/home/$new_user/.xinitrc'#" /etc/nodm.conf
sed -i -e "s#^NODM_USER=.*#NODM_USER='$newUser'#" -e "s#^NODM_XSESSION=.*#NODM_XSESSION='/home/$newUser/.xinitrc'#" /etc/nodm.conf
fi
# Copy over crontab if it exists
if crontab -u "$old_user" -l >/tmp/${old_user}_crontab.bak 2>/dev/null; then
if crontab -u "$new_user" "/tmp/${old_user}_crontab.bak"; then
rm "/tmp/${old_user}_crontab.bak"
if crontab -u "$oldUser" -l >/tmp/${oldUser}_crontab.bak 2>/dev/null; then
if crontab -u "$newUser" "/tmp/${oldUser}_crontab.bak"; then
rm "/tmp/${oldUser}_crontab.bak"
else
echo "Warning: failed to restore crontab for $new_user."
rm "/tmp/${old_user}_crontab.bak"
echo "Warning: failed to restore crontab for $newUser."
rm "/tmp/${oldUser}_crontab.bak"
fi
fi
# Update linger
if [[ -e "/var/lib/systemd/linger/$oldUser" ]]; then
mv "/var/lib/systemd/linger/$oldUser" "/var/lib/systemd/linger/$newUser"
fi
# Optionally update files in new home directory
echo "Would you like to search for references to $old_user and update them to $new_user in files located in /home/$new_user?"
echo "Would you like to search for references to $oldUser and update them to $newUser in files located in /home/$newUser?"
echo "This can take a while. (Y/N)"
read -r answer
answer="${answer:0:1}"
answer="${answer^}"
if [[ "$answer" == "Y" ]]; then
find "/home/$new_user" -type f -exec sed -i "s|$old_user|$new_user|g" "{}" \;
find "/home/$newUser" -type f -exec sed -i "s|$oldUser|$newUser|g" "{}" \;
fi
exit 0