How To: UNIX Add A User To A Group

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.

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
23 comments… add one
  • Mihir G joshi Jun 18, 2008 @ 12:37

    Hello Vivek,

    I don’t want my private files to be displayed in the output of find command????

  • Mihir G joshi Jun 18, 2008 @ 12:39

    How do i hide my confidential files to be isolated from find output??

    I am using RHEL 5

    Mihir G joshi

  • Jonathan Dade Jan 10, 2009 @ 10:03

    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.

  • esha Jun 25, 2009 @ 18:32

    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

    • 🐧 nixCraft Jun 25, 2009 @ 18:41

      I don’t get you, provide more info.

  • esha Jun 25, 2009 @ 19:00

    Vivek,
    Example: i have account named esha and i want to add #esha without manually vi to password file.
    Any possible way?

    • 🐧 nixCraft Jun 25, 2009 @ 20:50

      You wanna lock user account? If so use passwd itself:
      passwd -l username
      To unlock
      passwd -u username

    • krishna Dec 22, 2011 @ 9:17

      no its cannot possiable ok
      u have to create via useradd command
      note: while creating the user , system will automatically created related configuration file ok

  • esha Jul 7, 2009 @ 14:51

    Thanks Vivek. Another one:
    What is the SUN command to get directory full path for example for shortcut: $QERP_TOP.

  • william lee Aug 3, 2009 @ 0:46

    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

  • Samuel Oct 15, 2009 @ 13:58

    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

  • Lyenna Amchurek Oct 29, 2009 @ 3:05

    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

    • krishna Dec 22, 2011 @ 8:47

      we cannot change the same id
      but chenahe the id for chmod command
      it root loogin u have to change the permissions for perticular user
      using of chown command

  • Samuel Nov 17, 2009 @ 12:55

    Lyenna

    By editing /etc/passwd and changing the ID (second field) to zero.

    Bye

    • krishna Dec 22, 2011 @ 8:48

      using of chmod command only
      do n;t chage the in vi mode ok

  • Asit Pal Jul 19, 2010 @ 9:34

    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

    • anon Jan 3, 2011 @ 18:42

      sed ‘s/asit/amit/g’ /etc/passwd > /etc/passwd.tmp
      sed ‘s/ae/pp/g’ /etc/passwd.tmp > /etc/passwd
      pwck ( to check for password file errors )

    • srikanth kumbam Feb 25, 2011 @ 5:03

      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

  • wasim Oct 21, 2011 @ 6:55

    Hi I am new to HP Unix please help in L1 and L2 commands and please let me any test server is there online where is can practice

    • krishna Dec 22, 2011 @ 8:49

      no
      and u have to search the google only

  • Alec Mar 15, 2012 @ 16:33

    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.

  • basis May 30, 2013 @ 2:50

    thank you for sharing

    I like your post.

  • Jordan May 19, 2015 @ 13:27

    Thanks assholes, for tricking me into removing myself from the sudoers group!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.