Change root password from script

I am looking for a way to change the root user password on a Linux system through a bash script without booting the system. The only thing I have found so far is to either remove the password or use chroot, which I prefer not to use.

I know how to clear the root password, but I need to change it to another password defined earlier in the script.

I have root access to the entire file system.

The system uses shadow passwords, is there a way to generate an encrypted shadow password without logging in / chrooting?

Any other ways to change the root password from a script?

+5
source share
2 answers

/etc/shadow. () . crypt (3). DES, glibc2 :

ID  | Method
---------------------------------------------------------
1   | MD5
2a  | Blowfish (not in mainline glibc; added in some
    | Linux distributions)
5   | SHA-256 (since glibc 2.7)
6   | SHA-512 (since glibc 2.7)

, : $5$saltysalt$KhboodWTnuXJ5siXvWx5mxYXbnuNJOxROfD1inCILfD

$5 $ SHA-256, - , - .

, (3), , C:

#include <stdio.h>
#include <crypt.h>

int main(int argc, char *argv[]) {
        printf("%s\n", crypt(argv[1], argv[2]));
}

cc mkpass.c -o mkpass -lcrypt, , /etc/shadow:

./mkpass yourpassword yoursalt                   # DES (default)
./mkpass yourpassword '$6$yoursalt$encrypted'    # SHA-512 (quote your $)

Linux-. ( $id $).

+8

e Ubuntu .

rw init=/bin/bash

linux. root, passwd root.

-1

All Articles