How To: UNIX Add A User To A Group

Posted on in Categories , , , , last updated June 16, 2008

Q. How do I add a user to a group under Sun Solaris UNIX operating system from a shell prompt or command line?

A.. You can useadd command to administer a new user login on the UNIX system. Solaris UNIX / HP-UX UNIX uses /etc/passwd, /etc/shadow and /etc/group files to store user information.

Following information is compatible with the Solaris, AIX and HP-UX UNIX operating systems.

Add A New User To A Group

Add a new user called vivek to a group called vsftp, enter:
# useradd -G vsftp -d /data/home/v/vivek -m vivek
# passwd vivek

Where,

  • -G vsftp : Add vivek to secondary group called vsftp. This group must exists on the system.
  • -d /data/home/v/vivek : Path to the vivek’s home directory
  • -m : Create the new user’s home directory if it does not already exist.
  • vivek : Username / login nane

UNIX Add Existing User To A Secondary Group

Use usermod command to modify a user’s login definition on the system including group membership. For, example add user vivek to sales group, enter:
# usermod -G sales vivek
Add user tom to sales but also keep his existing membership of ftpusers, enter:
# usermod -G ftpusers,sales tom

UNIX Add Existing User To A Primary Group

Add jerry to sales primary group and change its existing group membership:
# usermod -g sales jerry
For more information read following man pages:
$ man usermod
$ man useradd
$ man passwd

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

23 comment

  1. Find is a command that is used to list all files that it “finds”. If you don’t want other people to find them when using find make sure they do not have root access or access to the directory in which the files are stored.

    Usually on a multi-user system you would store your files in your home directory. The only users that have access to this directory are yourself, root and in general anyone who is a member of the group named after your username. Be aware that if you create users the hard way (by editting /etc/passwd /etc/shadow /etc/groups) and you create users with the same uid / gid then these will also be able to access because they are the “same” user.

    Of course as a RHEL 5 owner you can indeed ask this sort of question of RedHat’s support teams.

    I hope this helps.

  2. Hi,
    Can someone help. Is it possible to add # infront of user loginname without vi to the password file. I have more than 50 users loginame to hide. Any command?
    Thanks

  3. hi
    i have a problem…
    Can I change the properties of the group?
    What I mean is let say i have one admin group , it can edit and install the SW in the system, and at the same time, I have a user group , it can just use the SW but can’t install it. AS long as i add a account to the group, it automatically can have those priveledge according to the group properties.

    Thanks

    Willaim Lee

  4. Hi vivek , first sorry about my english (I’mfrom Brazil) .

    My problem is : add an existing user to a group without removing its existing group.
    For example:
    HP > /usr/sam/lbin/grpusrs -m -l ‘,user’ group
    Linux > /usr/sbin/groupmod -A user group
    AIX > chgrpmem -m + user group

    When I use this one “/usr/local/bin/usermod -G group user” , it removes any other existing group.

    Tks for your attention

    Samuel

  5. Hi,
    I’m new to HPUX.
    Let say I have existing id called Sysadmin which is a normal user. How do I upgrade this id to become root equivalent, and have all the permissions like root.

    Tq

  6. drwxr-xr-x 2 asit ae 1024 Jul 8 16:03 00000010

    How will I change the name user “asit” to “amit” and group “ae” to “pp” ?
    Any unix command?

    Thanks
    Asit

    1. This answer to asit pal, If you want to change your user name from asit to amit, you should execute the follwing command
      usermod -l amit asit
      for group its almost same groupmod -n pp ae

  7. To append a group to the user’s list of supplemental groups, include the ‘-a’ flag.

    For example, to add user ‘popeye’ who is already in the ‘sailor’ group …

    # groups popeye
    popeye : popeye sailor

    … to the ‘forearm’ group as well, you can do this:

    # usermod -a -G forearm popeye

    This way it appends him, and does not overwrite the other groups he is a part of:

    # groups popeye
    popeye : popeye sailor forearm

    This saves you the hassle of having to write out all the other groups he needs to be a part of just to add him to another one.

Leave a Comment