Method 1: Use the passwd command directly
/bin/echo newpass|/usr/bin/passwd --stdin username
*Note: This method is only applicable to Red Hat operating systems, such as centos, redhat and other systems. In Debian operating systems, such as Debian, Ubuntu and other systems, the passwd command does not have the --stdin parameter, and the command cannot be completed.
Method 2: Use the chpasswd command
The above method using the passwd command can only be used for Red Hat systems. Here is a method that can be used for Debian systems, using the chpasswd command
/bin/echo username:newpass |chpasswd
Method 3: Use the python+usermod command
The usermod command has a -p parameter, which can directly set an encrypted ciphertext as the system password
There is a crypt library in python, which can encrypt a string with DES single item, relatively low security
So the two are used together. Now encrypt the password we want with the crypt library to get the ciphertext, and then use the -p parameter of the usermod command to change it to the system password
def R_chpass(user,pass_): username=user passwd=crypt.crypt(pass_,'ab') os.system("usermod -p %s %s" % (passwd,username))