-1

I've written a python script to set bootloader password in Ubuntu 22.04

However, I keep getting the following error when run with sudo

Traceback (most recent call last):
  File "/home/ubuntu/Downloads/Script/bootloader.py", line 27, in <module>
    set_bootloader_password(password, re_password, username)    
  File "/home/ubuntu/Downloads/Script/bootloader.py", line 13, in set_bootloader_password
    process_echo = subprocess.run(command_echo, capture_output=True, text=True)
  File "/usr/lib/python3.10/subprocess.py", line 501, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.10/subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1842, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'echo "set superusers=ubuntu" | tee -a etc/grub.d/40_custom && echo "password_pbkdf2 ubuntu grub.pbkdf2.sha512.10000.EC34695C6BC57134CCFFC0B25629D89BF1D020CD131ABB4E7F15CA11A228E4D976B628B95827C51B330548A0DE4B9C86B0A5946EF5760076323969CC0E9CAC5A.98F8A0A8AFC38EF2B1003165A036739ADFCB20E471B9E8F8C36329787E88A7FB87A44A408F08847DA208F3257180BD140A6E637C1254165A2A1D5B30744FD5B0" | tee -a etc/grub.d/40_custom'

When the script is run with root user no errors are given but the username and password hash haven't been written to the 40_custom file. What am I doing wrong?

The script:

import subprocess, os

def set_bootloader_password(password, re_password, username):
    # Generate Password Hash
    input_data = f"{password}\n{re_password}"
    process = subprocess.run(['grub-mkpasswd-pbkdf2'], capture_output=True, text=True, input=input_data)
    password_hash = process.stdout.split()

    

    # Set Bootloader Username and Password
    command_echo = f'echo "set superusers={username}" | tee -a /etc/grub.d/40_custom && echo "password_pbkdf2 {username} {password_hash[10]}" | tee -a /etc/grub.d/40_custom'
    process_echo = subprocess.run(command_echo, capture_output=True, text=True)
    print(process_echo.stdout)
    # Update GRUB
    process_update = subprocess.run(['update-grub'], capture_output=True, text=True)




uid = os.getuid()



if uid == 0:
    username = input("Enter Username: ")
    password = input("Enter Password: ")
    re_password = input("Re-Enter Password: ")
    set_bootloader_password(password, re_password, username)    
else:
    print("Please run the script as root")

The username and the password hash should be echoed to the /etc/grub.d/40_custom in the following format: set superusers="{username}" password_pbkdf2 {username} {password hash}

  • The code you have shown does not correlate with the error message. Note that the error indicates that it cannot find *etc/grub.d/40_custom* - i.e., it's missing the forward slash (root directory) – DarkKnight Aug 11 '23 at 06:25
  • You need `shell=True` in order to ask the shell to interpret your command string as a shell command. Otherwise, the *entire string* `command_echo` is treated as the *name* of a command that it is looking for. Please see the linked duplicate for details. – Karl Knechtel Aug 11 '23 at 07:02

0 Answers0