On LineageOS 23.2 with Magisk is there something similar to `/etc/sudoers`, such that I can run only a given command as `root` on Termux?
2/2
Apr 12, 12:26pm

On LineageOS 23.2 with Magisk, you don’t have a traditional /etc/sudoers file (since Android is not a full Unix system), but you can achieve similar functionality for running specific commands as root from Termux using the following approaches:


1. Using sudo in Termux

Termux can install a sudo package, which allows you to run commands as root if you have Magisk installed (which provides root access).

Steps:

  1. Install sudo in Termux:

    bash
    pkg install tsu

    tsu is a Termux-specific sudo alternative.

  2. Add a Rule for Specific Commands:

    • Edit the sudoers file for Termux (located at /data/data/com.termux/files/usr/etc/sudoers).
    • Add a line like this to allow a specific user (e.g., u0_a123) to run a specific command as root:
      text
      u0_a123 ALL=(root) NOPASSWD: /path/to/your/command
    • Replace u0_a123 with your Termux app’s user ID (find it with id in Termux).
  3. Set Permissions:

    • Ensure the file is owned by root and has the correct permissions:
      bash
      chmod 440 /data/data/com.termux/files/usr/etc/sudoers
      chown root:root /data/data/com.termux/files/usr/etc/sudoers
  4. Usage:

    • Run your command with:
      bash
      sudo /path/to/your/command

2. Using Magisk’s su Directly

If you want to allow Termux to run su without a password for specific commands, you can use Magisk’s built-in su rules.

Steps:

  1. Edit Magisk’s su Rules:

    • Open the Magisk app.
    • Go to Superuser > Superuser settings > Policy > Add rule.
    • Add a rule like:
      text
      su
  2. Allow Termux to Use su:

    • In Termux, run:
      bash
      su -c "your_command"
    • This will prompt for root access, but you can automate it if you trust the command.

3. Using Termux’s termux-setup-storage and termux-chroot

If you need to run commands in a more controlled environment, you can use Termux’s chroot or bind mounts.


Summary Table

Termux sudo
Allows running specific commands as root with a custom sudoers file.
sudo /system/bin/reboot
Magisk su Rules
Grants root access to Termux for specific commands.
su -c "mount -o remount,rw /"
Termux chroot
Runs commands in a chroot environment (advanced).
termux-chroot ls /

Which method do you want to use? If you need help setting up a specific rule or troubleshooting, let me know!

Apr 12, 12:26pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.