38

I have to start an application with my own user rights, but the group must be different. So, instead of:

$ ps -eo "user,group,args" | grep qbittorrent
morfik   morfik      /usr/bin/qbittorrent

it should be for example:

$ ps -eo "user,group,args" | grep qbittorrent
morfik   p2p      /usr/bin/qbittorrent

It also has to be done without asking about password. Is there a way to achieve this?

1
  • Sorry can't comment so put this follow up in answer area.<br> sg doesn't seem to work in our case. A user usually needs to use "newgrp" command to change to "jazz" group to be able to descend into a particular directory "dirA" from commandline. But he can't find a way to get "nautilus --browser" to descend into that "dirA". He tried "ng jazz -c 'nautilus --browser'" but it still can't descend into "dirA". How come?
    – CLTECH
    Commented Apr 16, 2018 at 15:00

4 Answers 4

52

Use sg.

For example, the following command will invoke sleep for group group-name

sg group-name -c 'sleep 100'

From the man page:

NAME
   sg - execute command as different group ID

SYNOPSIS
   sg [-] [group [-c ] command]

DESCRIPTION
   The sg command works similar to newgrp but accepts a command. The
   command will be executed with the /bin/sh shell...
2
  • Pretty simple, and it works. :) Commented Feb 3, 2014 at 1:50
  • 1
    Note that to avoid getting asked for a password, you have to be member of the group before running sg. Commented Jan 28, 2021 at 16:24
3

sg ask for group password

sudo have option -g that do same by user password.

it require mention group in sudoers

I changed /etc/sudoers by

%wheel All=(ALL) ALL

to

%wheel All=(ALL:ALL) ALL

Sometimes it more convenient

Safe way to edit sudoers is:

sudo sudoedit /etc/sudoers
1

To avoid the password prompt with sg add yourself as a member of the group

    sudo gpasswd -M morfik p2p

You may also need to remove the password and restrict access to only members

    sudo gpasswd -r p2p
    sudo gpasswd -R p2p

man gpasswd will give you more detail

1

su should be an option, too. For example:

su -pg group1 -c $'ps -eo \'user,group,args\' | grep qbittorrent' "${USER?}";

Some might say it's usually more available if compared to alternatives, including sudo, doas, or gosu.


su - run a command with substitute user and group ID ...

su [options] [-] [user [argument...]]

... su allows commands to be run with a substitute user and group ID. When called with no user specified, su defaults to running an interactive shell as root. When user is specified, additional arguments can be supplied, in which case they are passed to the shell.

... This version of su uses PAM for authentication, account and session management. Some configuration options found in other su implementations, such as support for a wheel group, have to be configured via PAM.

su is mostly designed for unprivileged users, the recommended solution for privileged users (e.g., scripts executed by root) is to use non-set-user-ID command runuser that does not require authentication and provides separate PAM configuration. If the PAM session is not required at all then the recommended solution is to use command setpriv. ...

-c, --command=command

Pass command to the shell with the -c option. ...

-g, --group=group

Specify the primary group. This option is available to the root user only.

-G, --supp-group=group

Specify a supplementary group. This option is available to the root user only. The first specified supplementary group is also used as a primary group if the option --group is not specified. ...

-m, -p, --preserve-environment

Preserve the entire environment, i.e., do not set HOME, SHELL, USER or LOGNAME. This option is ignored if the option --login is specified.

Source: https://man7.org/linux/man-pages/man1/su.1.html or man su.


Related: https://redd.it/l6y7nv (Is doas a good alternative to sudo?...)

You must log in to answer this question.

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