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 ?

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
  • 197,895
  • 55
  • 485
  • 740