1

I used usermod -G without the -a option, now the main user has only 2 groups. I want to restore all the previous groups but the problem is that i need to list all the groups before the changes

I am trying to use

locate /etc/groups

and grep username in the previous version of the file to get all the previous group

How can I access to the file from the db and read them ?

Future Gadget
  • 147
  • 2
  • 2
  • 12
  • Possible duplicate of [Default groups for user in Ubuntu?](https://askubuntu.com/questions/219083/default-groups-for-user-in-ubuntu) – muru May 15 '18 at 15:46
  • And you'll probably need https://askubuntu.com/q/70442/158442 as well to get back your lost sudo rights. – muru May 15 '18 at 15:46
  • 1
    Possible duplicate of [How do I add myself back as a sudo user?](https://askubuntu.com/questions/70442/how-do-i-add-myself-back-as-a-sudo-user) – karel May 15 '18 at 21:03

1 Answers1

1

There is no /etc/groups. There's /etc/group and /etc/group- (the backup). To get results for your user from the backup, you can do:

grep username /etc/group-

Or, to get just the groups, using awk:

awk -F: -v u=username '$NF ~ u {print $1}' /etc/group-

To then add those groups back, as root:

awk -F: -v u=username '$NF ~ u {print $1}' /etc/group- |
  xargs -n1 gpasswd -a username
muru
  • 193,181
  • 53
  • 473
  • 722