Linux: Refresh group membership without logging out

Have you ever added yourself to a group by editing /etc/group then realized the change doesn’t take effect until next time you log in (or spawn a new shell)?

Found this nice trick out today:

exec su -l $USER

(Note: it makes you re-enter your password, but you don’t have to restart sessions)

Posted Thursday, October 21st, 2010 under linux, tips and tricks.

Tags: , ,

7 comments

  1. Dude! Neat trick. I knew you could do the “su – username”
    But the exec part replaces the existing shell, this way Ctrl-D and exit still work the way they are supposed to. Nice!
    I am not sure you need the -l though.

  2. Great, thank you noting this little trick.

  3. Also, newgrp works too!

    useradd -G -a myGroup myUser
    newgrp myGroup

    Tada! :)

  4. @Chad, thanks for the tip. Unfortunately, this doesn’t work for me, neither did “newgrp -” which I had some hope in.

  5. plaristote says:

    As it turns out, newgrp myGroup actually pretty much does the same thing as any other solution, which is logging you again.
    The problem with this is that, if inside a script you create a group and add the current user to said group, none of these commands will help. The script will only resume once your back to the initial session, in which you’re not part of the group.

    Damn.

  6. @Jim, thanks for the response. I’m curious what dist. you’re running? I’m in Lubuntu 12.10 with success:

    $ sudo groupadd asdf
    $ sudo usermod -a -G asdf me
    $ newgrp asdf
    $ groups
    asdf group1 group2 etc….

  7. @Chad, apologies for the late response. I can’t say for sure what version I was using at the time, but it was almost definitely Fedora.

Leave a Reply