Search code examples
scriptingrhelgrub

Use `grub-md5-crypt` in a script where user is prompted for password to encrypt


As you already know from title, I want to configure an encrypted password for grub in /etc/grub.conf. I want to use a single script, where I will use grub-md5-crypt, enter my password that I want to encrypt, and sed that encrypted output in /etc/grub.conf. Second part is fine for me. but how to handle password prompt in the script:

[root@localhost ssh]# grub-md5-crypt 
Password: 
Retype password: 
$1$3L3j7$lkZs92MnlmQkVYiCH9dtJ.

How can I automatically save the encrypted password in a variable or manage it somehow, so that in the next line of my script I can sed it like this:

sed -i '/^[# ]*timeout.*/a $hashedpwd/' /etc/grub.conf

Please help


Solution

  • Hashvalue="$(echo -e "P@$$w0rd\nP@$$w0rd" | grub-md5-crypt 2>/dev/null | tail --lines=1)"
    

    Example

    $ echo $Hashvalue
    $1$F9fFw$czx0UwjdaorHHXf1QHRzi1