Arun Tv has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am having issues while changing unix user groups using a perl script. I used the following commands,
1) `newgrp <newGroup>`; 2) system ("newgrp <newGroup>"); 3) system ("newgrp" "<newGroup>");
In all cases the perl program is changing the group successfully but not able to proceed any further. If anyone knows how to change groups using perl, plz share.. Thanks Arun Tv

Replies are listed 'Best First'.
Re: problem while changing user group
by Fletch (Bishop) on Aug 13, 2008 at 19:54 UTC

    Erm, newgrp creates a new shell environment with the changed group credentials; that's not going to do your parent process much good. You need to look at $) and friends in perlvar if you want to change things for the current process.

    Update: Duuur, quite right. You'd have to be privileged to muck with $) which you're not because you're resorting to newgrp. Machinations like this have to be done outside your code itself (either by the running user using newgrp then running your code, by having your code setgid as is mentioned below, or by having a small setuid wrapper use $) on your behalf then running your program).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      $) is probably not going to help either, because a non-root process is not allowed to acquire additional group privileges.

      Perhaps setgid scripts (if your operating system supports them) can help you.

        $) will provide the group ID But I am not able come back to the parent program after changing group in another sub program. Anyway .....Thanks a lot... :-)