0% found this document useful (0 votes)
166 views7 pages

The Complete Guide To - Useradd - Command in Linux - 15 Practical Examples

The document provides a guide to using the "useradd" command in Linux to add and manage user accounts. It discusses 10 basic uses of useradd including adding a new user, creating a user with a different home directory or specific user/group IDs. It also covers adding users to multiple groups, without a home directory, or with account/password expiry dates. The guide then discusses 5 advanced uses including adding a user with a specific default home directory and changing a user's login shell.

Uploaded by

hammet217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views7 pages

The Complete Guide To - Useradd - Command in Linux - 15 Practical Examples

The document provides a guide to using the "useradd" command in Linux to add and manage user accounts. It discusses 10 basic uses of useradd including adding a new user, creating a user with a different home directory or specific user/group IDs. It also covers adding users to multiple groups, without a home directory, or with account/password expiry dates. The guide then discusses 5 advanced uses including adding a user with a specific default home directory and changing a user's login shell.

Uploaded by

hammet217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

7/4/2016

The Complete Guide to "useradd" Command in Linux

TheCompleteGuideto"useradd"CommandinLinux
WeallareawareaboutthemostpopularcommandcalleduseraddoradduserinLinux.Therearetimes
whenaLinuxSystemAdministratoraskedtocreateuseraccountsonLinuxwithsomespecificproperties,
limitationsorcomments.
InLinux,auseraddcommandisalowlevelutilitythatisusedforadding/creatinguseraccountsinLinuxand
otherUnixlikeoperatingsystems.Theadduserismuchsimilartouseraddcommand,becauseitisjusta
symboliclinktoit.

useraddcommandexamples

InsomeotherLinuxdistributions,useraddcommandmaycomeswithlightlydifferenceversion.Isuggestyou
toreadyourdocumentation,beforeusingourinstructionstocreatenewuseraccountsinLinux.
WhenwerunuseraddcommandinLinuxterminal,itperformsfollowingmajorthings:
1.Itedits/etc/passwd,/etc/shadow,/etc/groupand/etc/gshadowfilesforthenewlycreatedUseraccount.
2.Createsandpopulateahomedirectoryforthenewuser.
3.Setspermissionsandownershipstohomedirectory.
Basicsyntaxofcommandis:
useradd [options] username
Inthisarticlewewillshowyouthemostused15useraddcommandswiththeirpracticalexamplesinLinux.
WehavedividedthesectionintotwopartsfromBasictoAdvanceusageofcommand.
1.PartI:Basicusagewith10examples
2.PartII:Advanceusagewith5examples
PartI10BasicUsageofuseraddCommands
http://www.tecmint.com/add-users-in-linux/

1/7

7/4/2016

The Complete Guide to "useradd" Command in Linux

1.HowtoAddaNewUserinLinux
Toadd/createanewuser,allyouvetofollowthecommanduseraddoradduserwithusername.The
usernameisauserloginname,thatisusedbyusertologinintothesystem.
Onlyoneusercanbeaddedandthatusernamemustbeunique(differentfromotherusernamealreadyexists
onthesystem).
Forexample,toaddanewusercalledtecmint,usethefollowingcommand.
[root@tecmint ~]# useradd tecmint
WhenweaddanewuserinLinuxwithuseraddcommanditgetscreatedinlockedstateandtounlockthat
useraccount,weneedtosetapasswordforthataccountwithpasswdcommand.
[root@tecmint ~]# passwd tecmint
Changing password for user tecmint.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
Onceanewusercreated,itsentryautomaticallyaddedtothe/etc/passwdfile.Thefileisusedtostoreusers
informationandtheentryshouldbe.
tecmint:x:504:504:tecmint:/home/tecmint:/bin/bash
Theaboveentrycontainsasetofsevencolonseparatedfields,eachfieldhasitsownmeaning.Letssee
whatarethesefields:
1.Username:Userloginnameusedtologinintosystem.Itshouldbebetween1to32charcterslong.
2.Password:Userpassword(orxcharacter)storedin/etc/shadowfileinencryptedformat.
3.UserID(UID):EveryusermusthaveaUserID(UID)UserIdentificationNumber.BydefaultUID0is
reservedforrootuserandUIDsrangingfrom199arereservedforotherpredefinedaccounts.Further
UIDsrangingfrom100999arereservedforsystemaccountsandgroups.
4.GroupID(GID):TheprimaryGroupID(GID)GroupIdentificationNumberstoredin/etc/groupfile.
5.UserInfo:Thisfieldisoptionalandallowyoutodefineextrainformationabouttheuser.Forexample,user
fullname.Thisfieldisfilledbyfingercommand.
6.HomeDirectory:Theabsolutelocationofusershomedirectory.
7.Shell:Theabsolutelocationofausersshelli.e./bin/bash.

2.CreateaUserwithDifferentHomeDirectory
Bydefaultuseraddcommandcreatesausershomedirectoryunder/homedirectorywithusername.Thus,
forexample,weveseenabovethedefaulthomedirectoryfortheusertecmintis/home/tecmint.
However,thisactioncanbechangedbyusingdoptionalongwiththelocationofnewhomedirectory(i.e.
/data/projects).Forexample,thefollowingcommandwillcreateauseranushawithahomedirectory
http://www.tecmint.com/add-users-in-linux/

2/7

7/4/2016

The Complete Guide to "useradd" Command in Linux

/data/projects.
[root@tecmint ~]# useradd -d /data/projects anusha
Youcanseetheuserhomedirectoryandotheruserrelatedinformationlikeuserid,groupid,shelland
comments.
[root@tecmint ~]# cat /etc/passwd | grep anusha
anusha:x:505:505::/data/projects:/bin/bash

3.CreateaUserwithSpecificUserID
InLinux,everyuserhasitsownUID(UniqueIdentificationNumber).Bydefault,wheneverwecreateanew
useraccountsinLinux,itassignsuserid500,501,502andsoon
But,wecancreateuserswithcustomuseridwithuoption.Forexample,thefollowingcommandwillcreatea
usernavinwithcustomuserid999.
[root@tecmint ~]# useradd -u 999 navin
Now,letsverifythattheusercreatedwithadefineduserid(999)usingfollowingcommand.
[root@tecmint ~]# cat /etc/passwd | grep tecmint
navin:x:999:999::/home/navin:/bin/bash
NOTE:MakesurethevalueofauserIDmustbeuniquefromanyotheralreadycreatedusersonthesystem.

4.CreateaUserwithSpecificGroupID
Similarly,everyuserhasitsownGID(GroupIdentificationNumber).Wecancreateuserswithspecificgroup
IDsaswellwithgoption.
Hereinthisexample,wewilladdausertarunikawithaspecificUIDandGIDsimultaneouslywiththehelpof
uandgoptions.
[root@tecmint ~]# useradd -u 1000 -g 500 tarunika
Now,seetheassigneduseridandgroupidin/etc/passwdfile.
[root@tecmint ~]# cat /etc/passwd | grep tarunika
tarunika:x:1000:500::/home/tarunika:/bin/bash

5.AddaUsertoMultipleGroups
http://www.tecmint.com/add-users-in-linux/

3/7

7/4/2016

The Complete Guide to "useradd" Command in Linux

TheGoptionisusedtoaddausertoadditionalgroups.Eachgroupnameisseparatedbyacomma,withno
interveningspaces.
Hereinthisexample,weareaddingausertecmintintomultiplegroupslikeadmins,webadminand
developer.
[root@tecmint ~]# useradd -G admins,webadmin,developers tecmint
Next,verifythatthemultiplegroupsassignedtotheuserwithidcommand.
[root@tecmint ~]# id tecmint
uid=1001(tecmint) gid=1001(tecmint)
groups=1001(tecmint),500(admins),501(webadmin),502(developers)
context=root:system_r:unconfined_t:SystemLow-SystemHigh

6.AddaUserwithoutHomeDirectory
Insomesituations,wherewedontwanttoassignahomedirectoriesforausers,duetosomesecurity
reasons.Insuchsituation,whenauserlogsintoasystemthathasjustrestarted,itshomedirectorywillbe
root.Whensuchuserusessucommand,itslogindirectorywillbetheprevioususerhomedirectory.
Tocreateuserswithouttheirhomedirectories,Misused.Forexample,thefollowingcommandwillcreatea
usershilpiwithoutahomedirectory.
[root@tecmint ~]# useradd -M shilpi
Now,letsverifythattheuseriscreatedwithouthomedirectory,usinglscommand.
[root@tecmint ~]# ls -l /home/shilpi
ls: cannot access /home/shilpi: No such file or directory

7.CreateaUserwithAccountExpiryDate
Bydefault,whenweadduserswithuseraddcommanduseraccountnevergetexpiresi.etheirexpirydateis
setto0(meansneverexpired).
However,wecansettheexpirydateusingeoption,thatsetsdateinYYYYMMDDformat.Thisishelpfulfor
creatingtemporaryaccountsforaspecificperiodoftime.
Hereinthisexample,wecreateauseraparnawithaccountexpirydatei.e.27thApril2014inYYYYMMDD
format.
[root@tecmint ~]# useradd -e 2014-03-27 aparna
Next,verifytheageofaccountandpasswordwithchagecommandforuseraparnaaftersettingaccount
http://www.tecmint.com/add-users-in-linux/

4/7

7/4/2016

The Complete Guide to "useradd" Command in Linux

expirydate.
[root@tecmint ~]# chage -l aparna
Last password change

: Mar 28, 2014

Password expires

: never

Password inactive

: never

Account expires

: Mar 27, 2014

Minimum number of days between password change

:0

Maximum number of days between password change

: 99999

Number of days of warning before password expires

:7

8.CreateaUserwithPasswordExpiryDate
Thefargumentisusedtodefinethenumberofdaysafterapasswordexpires.Avalueof0inactivetheuser
accountassoonasthepasswordhasexpired.Bydefault,thepasswordexpiryvaluesetto1meansnever
expire.
Hereinthisexample,wewillsetaaccountpasswordexpirydatei.e.45daysonausertecmintusingeand
foptions.
[root@tecmint ~]# useradd -e 2014-04-27 -f 45 tecmint

9.AddaUserwithCustomComments
Thecoptionallowsyoutoaddcustomcomments,suchasusersfullname,phonenumber,etcto
/etc/passwdfile.Thecommentcanbeaddedasasinglelinewithoutanyspaces.
Forexample,thefollowingcommandwilladdausermansiandwouldinsertthatusersfullname,Manis
Khurana,intothecommentfield.
[root@tecmint ~]# useradd -c "Manis Khurana" mansi
Youcanseeyourcommentsin/etc/passwdfileincommentssection.
[root@tecmint ~]# tail -1 /etc/passwd
mansi:x:1006:1008:Manis Khurana:/home/mansi:/bin/sh

10.ChangeUserLoginShell:
Sometimes,weadduserswhichhasnothingtodowithloginshellorsometimeswerequiretoassigndifferent
shellstoourusers.Wecanassigndifferentloginshellstoaeachuserwithsoption.
Hereinthisexample,willaddausertecmintwithoutloginshelli.e./sbin/nologinshell.
[root@tecmint ~]# useradd -s /sbin/nologin tecmint
http://www.tecmint.com/add-users-in-linux/

5/7

7/4/2016

The Complete Guide to "useradd" Command in Linux

Youcancheckassignedshelltotheuserin/etc/passwdfile.
[root@tecmint ~]# tail -1 /etc/passwd
tecmint:x:1002:1002::/home/tecmint:/sbin/nologin
PartII5AdvanceUsageofuseraddCommands

11.AddaUserwithSpecificHomeDirectory,DefaultShellandCustomComment
Thefollowingcommandwillcreateauserraviwithhomedirectory/var/www/tecmint,defaultshell
/bin/bashandaddsextrainformationaboutuser.
[root@tecmint ~]# useradd -m -d /var/www/ravi -s /bin/bash -c "TecMint Owner" U ravi
Intheabovecommandmdoptioncreatesauserwithspecifiedhomedirectoryandthesoptionsetthe
usersdefaultshelli.e./bin/bash.ThecoptionaddstheextrainformationaboutuserandUargument
create/addsagroupwiththesamenameastheuser.

12.AddaUserwithHomeDirectory,CustomShell,CustomCommentandUID/GID
Thecommandisverysimilartoabove,butherewedefiningshellas/bin/zshandcustomUIDandGIDtoa
usertarunika.WhereudefinesnewusersUID(i.e.1000)andwhereasgdefinesGID(i.e.1000).
[root@tecmint ~]# useradd -m -d /var/www/tarunika -s /bin/zsh -c "TecMint
Technical Writer" -u 1000 -g 1000 tarunika

13.AddaUserwithHomeDirectory,NoShell,CustomCommentandUserID
Thefollowingcommandisverymuchsimilartoabovetwocommands,theonlydifferenceishere,thatwe
disablingloginshelltoausercalledavishekwithcustomUserID(i.e.1019).
Heresoptionaddsthedefaultshell/bin/bash,butinthiscasewesetloginto/usr/sbin/nologin.That
meansuseravishekwillnotabletologinintothesystem.
[root@tecmint ~]# useradd -m -d /var/www/avishek -s /usr/sbin/nologin -c
"TecMint Sr. Technical Writer" -u 1019 avishek

14.AddaUserwithHomeDirectory,Shell,CustomSkell/CommentandUserID
Theonlychangeinthiscommandis,weusedkoptiontosetcustomskeletondirectoryi.e.
/etc/custom.skell,notthedefaultone/etc/skel.Wealsousedsoptiontodefinedifferentshelli.e./bin/tcsh
tousernavin.
[root@tecmint ~]# useradd -m -d /var/www/navin -k /etc/custom.skell -s
/bin/tcsh -c "No Active Member of TecMint" -u 1027 navin
http://www.tecmint.com/add-users-in-linux/

6/7

7/4/2016

The Complete Guide to "useradd" Command in Linux

15.AddaUserwithoutHomeDirectory,NoShell,NoGroupandCustomComment
Thisfollowingcommandisverydifferentthantheothercommandsexplainedabove.HereweusedMoption
tocreateuserwithoutusershomedirectoryandNargumentisusedthattellsthesystemtoonlycreate
username(withoutgroup).Therargumentsisforcreatingasystemuser.
[root@tecmint ~]# useradd -M -N -r -s /bin/false -c "Disabled TecMint Member"
clayton
Formoreinformationandoptionsaboutuseradd,runuseraddcommandontheterminaltoseeavailable
options.

http://www.tecmint.com/add-users-in-linux/

7/7

You might also like