deepin-community-live-cd/cd-tools/pardus-boot-repair/usr/bin/reset-password

48 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Exit immediately if a command exits with a non-zero status
set -e
# Loop until a valid block device is provided
while true; do
# List all block devices and prompt the user for input
echo "Available block devices:"
ls /sys/block/* | grep "[0-9]$"
read -p "Enter the name of the rootfs partition: " disk
# Check if the input is a valid block device
if [[ -b "/dev/$disk" ]]; then
break
else
echo "Invalid block device. Please try again."
fi
done
# Unmount the partition if it is already mounted
umount -lf /dev/$disk >/dev/null 2>&1 || true
# Mount the partition to /mnt
mount /dev/$disk /mnt
# Prompt the user for a new password
while true; do
read -s -p "Enter new password: " pass1; echo
read -s -p "Confirm password: " pass2; echo
# Check if the passwords match and are not empty
if [[ "$pass1" == "$pass2" && -n "$pass1" ]]; then
break
else
echo "Passwords do not match or are empty. Please try again."
fi
done
# Get the username of the first non-root user
user=$(grep "^.*:x:1000:" /mnt/etc/passwd | cut -f 1 -d ':')
# Change the password of the user in the chroot environment
chroot /mnt usermod -p $(openssl passwd -6 "$pass1") "$user"
# Unmount the partition
umount -lf /mnt >/dev/null 2>&1