Use robust usermod -p method for setting passwords

This commit is contained in:
2026-02-05 17:54:14 +01:00
parent 23b5f017d4
commit 9cfd6ad2e0

View File

@@ -246,19 +246,25 @@ UUID={efi_uuid} /boot vfat defaults 0 2
f.write("%wheel ALL=(ALL) ALL\n") f.write("%wheel ALL=(ALL) ALL\n")
os.chmod(os.path.join(sudoers_dir, "wheel"), 0o440) os.chmod(os.path.join(sudoers_dir, "wheel"), 0o440)
# Set user and root password using chpasswd -R (from host side) # Set user and root password using hashed passwords and usermod
try: try:
logger.info(f"Setting passwords for {user_info['username']} and root...") logger.info(f"Setting hashed passwords for {user_info['username']} and root...")
# We set both to the same password for convenience in this minimal install # Generate SHA512 hash using openssl on the host
pass_data = f"{user_info['username']}:{user_info['password']}\nroot:{user_info['password']}\n" res = subprocess.run(
subprocess.run( ["openssl", "passwd", "-6", user_info["password"]],
["chpasswd", "-R", mount_root], capture_output=True,
input=pass_data,
text=True, text=True,
check=True check=True
) )
except subprocess.CalledProcessError as e: hashed_pass = res.stdout.strip()
logger.error("Failed to set passwords using chpasswd -R")
# Apply hash to user and root using usermod -p (takes encrypted password)
run_command(["chroot", mount_root, "usermod", "-p", hashed_pass, user_info["username"]])
run_command(["chroot", mount_root, "usermod", "-p", hashed_pass, "root"])
run_command(["sync"])
except Exception as e:
logger.error(f"Failed to set passwords: {e}")
raise e raise e
# Set hostname # Set hostname