Problem description
When you run the passwd command to change the root password, the system returns a Permission denied error.
Causes
File lock: The core authentication files, such as
/etc/passwdand/etc/shadow, have an immutable attribute, which prevents any modifications.Incorrect permissions: Incorrect read and write permissions on one or more files prevent the
passwdcommand from modifying the password.SELinux policy restrictions: The SELinux security policy prevents the
passwdcommand from modifying the password.Incorrect PAM configuration file format: A Pluggable Authentication Modules (PAM) configuration file in
/etc/pam.d/uses the DOS format, which Linux cannot parse correctly.
Solution
Before you modify any system files, create a snapshot of your system disk to back up configuration data. This lets you quickly restore the system if a problem occurs.
Step 1: Check and modify file attributes
Check the file attributes.
sudo lsattr /etc/passwd /etc/shadowIf the output contains the
iattribute or theaattribute (for example,----ia------), the file is locked. Run the following commands to remove these attributes.sudo chattr -ia /etc/passwd sudo chattr -ia /etc/shadowAfter removing the attributes, retry the
passwdcommand.
Step 2: Check and fix file permissions
Check the file permissions.
sudo ls -l /etc/passwd /etc/shadowEnsure the permissions are correct. If not, use the
chmodcommand to correct them.The correct permissions for
/etc/passwdare644(-rw-r--r--).The correct permissions for
/etc/shadoware000(----------) or600(-rw-------).
sudo chmod 644 /etc/passwd sudo chmod 000 /etc/shadowAfter correcting the permissions, retry the
passwdcommand.
Step 3: Check the SELinux status
Check the current status of SELinux.
sestatusIf the
Current modeisenforcing, SELinux is in enforcing mode and will block modifications that violate its security policy. Temporarily switchSELinuxto permissive mode to allow the modification.setenforce 0After setting
SELinuxtopermissivemode, retry thepasswdcommand.
Step 4: Check the PAM configuration file format
Check the format of the PAM configuration file.
sudo file -i /etc/pam.d/passwdIf the output includes
ASCII text, with CRLF line terminators, the file is in DOS format. Convert the file format because Linux cannot parse files with CRLF line endings (\r\n).sudo yum install -y dos2unix sudo dos2unix /etc/pam.d/passwdRetry the
passwdcommand.
Recommendations
After you resolve the problem, you can re-apply the immutable attribute to the /etc/passwd and /etc/shadow files to prevent accidental changes.
sudo chattr +i /etc/passwd
sudo chattr +i /etc/shadow