1

I am trying to create my own PID 1 init script, to be called from the boot cmdline with init=/myscript. How can I make it work on a real filesystem, with any kernel?

When it runs in an initrd, it works fine and can mount things, etc. - but when I use it on my filesystem without an initrd, it fails to mount things, because:

mount: only root can do that (effective UID is 1000)

When I strace any command that fails, it inevitably issues geteuid32() and that returns 1000. Why? How can I run as euid 0?

1
  • Did you try debug it with a more simple script like print current euid then loop forever? Apr 15, 2020 at 10:38

2 Answers 2

0

There's no special treatment for init on initrd, so there must be some other issue.

If run as root, the euid will match the owner of the binary if the setuid bit is set.

Check the ownership on /bin/mount.

2
  • 1
    What do you mean by “If run as root, the euid will match the owner of the binary”? When run as root, the euid is root, unless the binary is setuid (which /bin/mount is, but you don’t mention that). Apr 15, 2020 at 8:26
  • Of course, fixed, thanks.
    – Dagelf
    Apr 15, 2020 at 10:11
0

Use Sudo from Unprivileged Accounts

The error is telling you what the problem is: you're using a "mortal" account with UID 1000, not the root account with UID 0, to call mount. Many system functions need to run as root. Many processes like initrd run as root, so the commands have sufficient privilege to run. However, when you're running things from your everyday user account, you need su or sudo to perform actions as a different user.

For mount, you'd want to call it like this:

sudo mount /mnt/foo

Unless specified by the -u flag, the user for sudo commands defaults to root. Running your command through sudo should resolve the problem you've described, provided the account with UID 1000 is set up properly in the appropriate /etc/sudoers or /etc/sudoers.d file.

See Also

  • man 5 sudo
  • man 5 sudoers
  • man 8 visudo
1
  • I get exactly the same error - as sudo is run with an euid of 1000, and sh is my init. I want to know how initrd can run /init with euid of 0 but it doesn't work when I manually do it. It seems the Linux kernel discriminates because I'm not running in an initramfs, why? Or am I missing something obvious?
    – loser
    Apr 14, 2020 at 6:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .