Linux set root password on etc / shadow file
I am creating a ram filesystem and I have created a "/" directory. I would like to set a default root password for the generated "etc / shadow" file. Actual file:
root:*:15980:0:99999:7:::
bin:*:15980:0:99999:7:::
daemon:*:15980:0:99999:7:::
adm:*:15980:0:99999:7:::
...
.....
I would like to replace "root: *:" with "root: Hashedpassword:", I know it is very easy to replace "*" with "HASHEDPASSWORD", but this should be done using the pattern "root: nothing:" and this also shouldn't be hard, even if I would like to ask about it here, because it might be helpful for others.
For example:
the password I want to set is "centos"
Salt is "xyz"
I get a hashed pass:
pass=$(openssl passwd -1 -salt xyz centos)
Then I would like me to replace "nothing" in "root: anything:" with "root: Hashedpassword:" with the command "sed".
source to share
You can do it with GNU sed like
sed -i -e "s/^root:[^:]\+:/root:$pass:/" etc/shadow
If it $pass
can have characters in it /
, you can use a different delimiter, for example ,
if it doesn't. You can do it like:
sed -i -e "s,^root:[^:]\+:,root:$pass:," etc/shadow
if you don't have GNU sed, you may not have -i
or may need an argument.
You can also use awk
for this:
awk -F: "BEGIN {OFS=FS;} \$1 == \"root\" {\$2=\"$pass\"} 1" etc/shadow
if you have the latest version of GNU awk, you can do it with the -i
following:
awk -i inplace -F: "BEGIN {OFS=FS;} \$1 == \"root\" {\$2=\"$pass\"} 1" etc/shadow
source to share