Raspberry pi comes with a default user account that is added to multiple groups:

username@hostname:~ $ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio

I'd like to script adding a new primary user with the same group membership as the default. I think this would also be practical for adding new admins to any Unix/Linux system. Windows has the functionality from ADUC by Copying an existing account.

I believe the script will need to:

  1. Use a loop
  2. Prompt for existing username (eu) input
  3. Store the existing user's current groups as a variable (eg) - groups
  4. Prompt for new username (nu) input
  5. Add the new user with the existing user's groups - useradd

I presume this will work to also gain sudo permission, based on ændrük's answer to a similar question. Another similar question is Rahul's about adding a list of users to multiple groups.

  • So, how would you go about to doing that?
    – ilkkachu
    Jan 25, 2018 at 19:48
  • @ilkkachu, I don't know the answer to the question. I'm trying to figure it out now, but am still green enough to struggle with multi-line comments. Jan 25, 2018 at 19:52
  • how disappointing with multi-line comments: unix.stackexchange.com/a/37414/145311. Perhaps Python would be a better language choice than bash. Jan 25, 2018 at 20:07

1 Answer 1


Given a template user of some sort, like your default pi user on Raspbian, etc.

First, create a new user -

adduser newuser

Then put them in all the same groups as the pi user

for i in `grep -E "(:|,)pi(:,|$)" /etc/group|cut -f1 -d:`
  adduser newuser $i

Be very careful about giving too much access. Also, remember that a user has to log completely out and then log back in before group membership really changes.

You must log in to answer this question.

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