A Complete Guide to “useradd” Command in Linux with Examples _ Hostbillo

KiyanaSharma1 53 views 12 slides Jul 17, 2023
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

This article will cover how to use the useradd command in Linux to set up new user accounts.


Slide Content

ACompleteGuideto“useradd”Commandin
LinuxwithExamples|Hostbillo
Introduction
MultipleuserscaninteractwithLinuxatoncebecauseitisamulti-usersystem.
Youareaccountableforoverseeingthesystem'susersandgroupsbyallowing
anddenyingusersandaddingandremovingthemfromdifferentgroups.
ThisarticlewillcoverhowtousetheuseraddcommandinLinuxtosetup
newuseraccounts.
UseaddCommand
FortheuseraddcommandinLinux,thefollowinggeneralsyntaxapplies:
useradd[OPTIONS]USERNAME

Userswithsudobenefitsorrootaretheonlyoneswhocancreatenewuser
accountsinLinuxusingtheuseraddcommand.
Accordingtothechoicesyoutypeatthecommandprompt,theuseradd
commandcreatesanewuseraccountonceyourunit.
Sincethevariablesinthisfilearedifferentbetweendistributions,theLinuxadd
usercommandbehavesdifferentlyonvarioussystems.
Additionally,useraddreadsthedatainthe/etc/login.defsfile.Thisfileincludes
configurationinformationfortheshadowpasswordsuite,includingpassword
expirationpolicy,userIDrangesusedwhencreatingthesystemandregular
users,andmore.
HowtoaddUserinLinux?
Usetheuseraddcommandafterenteringtheuser'snametocreateanewuser
inLinux.
Ifyouwantedtomakeanewuserwiththenameusername,youmightrun:
sudousername

Useraddusesthedefaultsettingslistedinthe/etc/default/useraddfilewhenitis
runwithoutanyoptions,creatinganewuseraccount.
Youneedtomarktheaccountpasswordpriortologgingintotherecently
createduserinlinux.Performthepasswdcommand,thenentertheusername:
sudopasswdusername.
Keepinmindtochooseanuniquepassword.
AddaNewUserandCreateaHomeDirectory
MostLinuxdistributionsdonotbuildtheuser'shomedirectorywhenevera
newlyaddedaccountissetupwithuseraddcommand.
Thehomedirectorywouldbecreatedbyimplementingthe-m(—create-home)
element.

sudouseradd-musername
Withtheaforementionedcommand,theuser'shomedirectoryiscreatedand
filesfromthe/etc/skeldirectoryarecopiedover.Youcanfindtheinitialization
filesbylistingthefilesinthe/home/usernamedirectory:
ls-la/home/username/
Auser'shomedirectorycancontainfoldersandfilesthattheycanadd,change,
anddelete.
CreateaUserwithSpecificHomeDirectory
UseraddCommandinLinuxinstantlycreatesthehomedirectory.Ifyouwantto
placetheuser'shomedirectorysomewhereelse,thenused(—home)feature.
Usingtheexamplebelow,youcanmakeanewuserwiththenameusername
andahomedirectoryof/opt/username:

sudouseradd-m-d/opt/usernameusername
CreateNewUserinLinuxwithSpecificUserId
UsernamesanduniqueUIDsareusedinLinuxandotherUnix-likeoperating
systemstoidentifyusers.
TheLinuxprogramgiveseachuseraspecialpositiveintegerthattheycanrefer
toastheir"useridentifier"(UID).TheUIDaswellasotheraccesscontrol
policiesdefinethetypesofactivitiesauserisallowedtoperformonsystem
resources.
ThesystemwillautomaticallydesignatethefollowingUIDfromthecatalogof
userIDsmentionedinthelogin.defsfiletoanewlycreateduserLinux.
Whenoperatinguseradd,utilise-u(—uid)optiontosetupauserwithanunique
UID.Forinstance,tocreateanewuserwiththeUIDof1700andthename
username,youwouldtype:
sudouseradd-u1700username
Withtheidcommand,youcanconfirmtheuser'sUID:
id-uusername

CreateaUserwithaSpecificGroupId
UseraccountsinLinuxareorganizedandmanagedbygroups,whichare
organizationalunits.Themainfunctionofgroupsistospecifyasetofprivileges
thatcanbesharedbyallmembersofthegroup,suchaspermissionsforreading,
writing,orimplementingaspecificresource.
Whenaddinganewuser,theuseraddpromptautomaticallycreatesagroupthat
hastheexactnameastheusernameandtheidenticalGIDastheUID.
Byusing-g(—gid)feature,youcanallocateausertoaspecificinitiallogin
group.YoucanchoosebetweenstatingthegroupnameandtheGIDnumber.
ThegroupIDornamemustalreadybeinuse.
Inthisexample,we'llcreateabrand-newuserwiththenameusername,and
we'llsetthelogingrouptousertype.
sudouseradd-gusersusername
Youcouldperhapsvalidatetheuser'sGIDbyusingidprompt:
id-gnusername

AddaUserandallotMultipleGroups
Theterms"primarygroup"aswellas"secondarygroup"refertotwodistinct
categoriesinLinuxoperatingsystems.Eachuserhasamaximumofone
primarygroupandzerotomoresecondarygroupstowhichtheycanbelong.
Withthe-G(—groups)option,youcanspecifyalistofextragroupsthatthe
userwillbelongto.
Withtheprimarygroupusers,secondarygroupswheel,anddocker,the
followingcommandcreatesanewuserwiththenameusername.
sudouseradd-gusers-Gwheel
Additionally,youcouldincorporatesearching
idusername
AddUserinLinuxwithoutanyHomeDirectory
Somesituationscallforustostepawayfromgivingusershomedirectoriesfor
safetypurposes.Inthiscase,theuser'shomedirectorywillberootwhenthey
logintoasystemthathasjustrestarted.Whensuchauserexecutesthesu
command,theprevioususer'shomedirectorywillbeusedasthelogindirectory.
Usingtheoption"-M,"userscanbecreatedwithouthavinghomedirectories.
Youcanmakeauserwithouttheneedforahomedirectorybyusingthe
commandprompt,forexample.

sudouseradd-Musername
Thelscommandwillnowbeusedtoconfirmthattheuserwasestablishedwith
noneedforahomedirectory.
sudols-l/home/username
ls:cannotaccess/home/username:Nosuchfileordirectory
AddNewUserwithExpiryDate
Youcandesignateadayandtimeforthenewlyaddedaccounts'expirationby
usingthe-e(—expiredate)feature.Thiscanbeusedtocreatetemporary
accounts.
Inordertospecifyadate,usetheformatYYYY-MM-DD.
Forinstance,tomakeanewuseraccountwiththenameusername(ABC)and
anexpirationdateofApril07th,2023,youwouldexecutethefollowing
command:
sudouseradd-e2023-04-07username(ABC)
Tofindoutwhenanaccountwillexpire,choosethechageprompt:
sudochage-lusername(ABC)

CreateNewUserwithCustomComments
Youcanprovideaprecisedescriptionforthenewuserbyusingthe-c
(—comment)option.Usually,acommentwillincludetheuser'sfullnameor
contactinformation.
Intheexamplebelow,weareaddinganewuserwiththenameusernameand
thecommenttextstringTestUserAccount:
sudouseradd-c"TestUserAccount"username
grepusername/etc/passwd
AnothernameforthecommentsectionisGECOS.
AddUserwithachangedloginshell

Theloginshellforanewuserisautomaticallysettotheonelistedinthe
/etc/default/useraddfile.Thedefaultshellsettingvariesbetweendistributions;
insome,itis/bin/sh,andinothers,/bin/bash.
Withthe-s(—shell)feature,youcanidentifythenewlyaddeduser'sloginshell.
Forexample,
sudouseradd-s/usr/bin/zshusername
Determinetheuser'sloginshellbyexaminingtheentryinthefile:
grepusername/etc/passwd
ThecommandforChangingtheDefaultuseraddValues
The-D,—defaultsfunction,allowsyoutoseeandmodifythestandarduseradd
configurations.
Toseethecurrentdefaultoptions,usethefollowingsyntax:
useradd-D
Considerthescenariowhereyouwanttoswitchfrom/bin/shto/bin/bashasthe
defaultloginshell.Enterthenewshellinthefollowingformattoachievethat:
sudouseradd-D-s/bin/bash

Userscanactuallyconfirmthatthestandardshellcomponenthasbeenaltered
byrunningthecommandshownbelow:
sudouseradd-D|grep-ishell
CommandtosetaPasswordforNewUser
Thefollowingcommandcanbeusedtosetanunencryptedpasswordforthe
user.
sudouseradd-ptest_passwordusername
Thiswillsetupanewuserwiththename"username"andtheunencrypted
password"testpassword".
CommandtoDisplayHelp
Youcandemonstratehelpbyenteringthefollowingsyntax.
sudouseradd--help
Theuseraddcommand'shelpsectionwillbedisplayedwhenthiscommandis
used.

Conclusion
We'vedemonstratedinthisarticlehowtousetheuseraddcommandinLinux
toaddnewuseraccounts.AllLinuxdistributions,suchasCentOS,RHEL,
Debian,Ubuntu,andothers,alsoshouldfollowthesameinstructions.Asof
rightnow,youcantestthesefeaturesandattempttogiveLunixmore
functionality.
Tags