# Linux User Management

**👤 User Management in Linux: Why It's Vital 🐧**

Managing users in a Linux system is fundamental to maintaining a secure and organized environment. User management involves creating, modifying, and deleting user accounts and their permissions. Let's dive into why user management is crucial and how it can be achieved efficiently. 🚀

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">All the user information is stored in the /etc/passwd file and each line of the /etc/passwd file describes a user as given example below.</div>
</div>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690536400669/8dab4354-c1a3-456e-b7bd-19ebacda20d7.png align="center")

**👨‍💻 Common User Management Tasks:**

### Different ways to add a Linux user

* *sudo useradd -g &lt;group\_name&gt; -s &lt;shell&gt; -c "&lt;description or comments&gt;" -m -d &lt;home\_directory\_of\_user&gt; &lt;username&gt;*
    
    **Example**:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690538881073/2e38e864-596c-4224-85b0-0e1260b48da9.png align="center")
    
    As we have given all the options to useradd command in the above example but it is not mandatory to provide all the options to the useradd command (group name, comments, shell name and home directories are optional to use with useradd command).
    
* If we don't provide a group name while adding a user it will create a self-group as per the username given in the command, if we don't provide a shell name it will configure a default shell (/bin/sh or /bin/bash..etc based on our default shell in our system) to the user, likewise, if we do not provide a home directory along with useradd command it will create a home directory with username itself (/home/&lt;username&gt;)
    
    **Example**:
    
    *sudo useradd &lt;username&gt;*
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690540065771/2e2412b7-10d0-4ab7-a8a3-e6fd069c22a8.png align="center")
    
    We can observe as given in the screenshot - even if we have not provided the group name, home directory and shell name, a new **group ID (1006)** got created and assigned to **raygurudeo** user and likewise **/bin/sh** shell configured and **/home/ragurudeo** (home directory) created by default.
    

### How to set a Linux user password

* Linux stores encrypted user's password in /etc/shadow.
    
* We can set the user's password using the below command:
    
    *sudo passwd &lt;username&gt;*
    
    **Example**:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690541901775/843a252f-8acd-4e3c-885e-fd8794c8ed94.png align="center")
    

### How to delete a user in Linux

* *sudo userdel -r &lt;username&gt;*
    
    It will delete a Linux user along with the user's home directory however **\-r** flag is not mandatory if we do not use **\-r** it will delete the user but the user's home directory will not be deleted.
    
    Example:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690541135227/e54de221-1886-4742-a344-f02b9aa4e0fa.png align="center")
    
    We can observe that we have the user's home directory listed as **raygurudeo** and once we have deleted the user, home directory also got deleted.
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">I have developed a bash script to perform all the user related operations. please have a look at the GitHub repo and please share your feedback on it.</div>
</div>

%[https://github.com/raygurudeo/shell-script/tree/master] 

Thank you !
