<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Script- Automatic User Creation]]></title><description><![CDATA[Script- Automatic User Creation]]></description><link>https://createuser.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 05:01:34 GMT</lastBuildDate><atom:link href="https://createuser.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Automating User Creation in Linux with a Bash Script]]></title><description><![CDATA[Managing users on a Linux system is a crucial administrative task. If you're frequently adding users, automating the process with a Bash script can save time and reduce errors. In this blog, we'll walk through a powerful script that creates a user, a...]]></description><link>https://createuser.hashnode.dev/automating-user-creation-in-linux-with-a-bash-script</link><guid isPermaLink="true">https://createuser.hashnode.dev/automating-user-creation-in-linux-with-a-bash-script</guid><category><![CDATA[#LinuxUserCreation]]></category><category><![CDATA[#AutomatingLinuxTasks]]></category><category><![CDATA[Linux]]></category><category><![CDATA[bash script]]></category><category><![CDATA[automation]]></category><category><![CDATA[shell scripting]]></category><category><![CDATA[bash scripting]]></category><dc:creator><![CDATA[Omkar Mahandule]]></dc:creator><pubDate>Wed, 12 Feb 2025 07:38:41 GMT</pubDate><content:encoded><![CDATA[<p>Managing users on a Linux system is a crucial administrative task. If you're frequently adding users, automating the process with a Bash script can save time and reduce errors. In this blog, we'll walk through a powerful script that creates a user, assigns a password, and forces them to change it upon first login. Let's dive in!</p>
<h2 id="heading-why-automate-user-creation">Why Automate User Creation?</h2>
<p>Creating users manually using <code>useradd</code> and <code>passwd</code> works fine, but it can be tedious when handling multiple accounts. Automation ensures consistency and efficiency, reducing the risk of misconfigurations.</p>
<h2 id="heading-the-bash-script">The Bash Script</h2>
<p>Below is the script that automates user creation:</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/bin/bash</span>

<span class="hljs-comment"># Ensure the script is run as root</span>
<span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">${UID}</span>"</span> -ne 0 ]];  <span class="hljs-comment"># UID=0 -&gt; root user</span>
 <span class="hljs-keyword">then</span>        
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Please run with sudo or root."</span>
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Require at least a username argument</span>
<span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">${#}</span>"</span> -lt 1 ]];
 <span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Usage: <span class="hljs-variable">${0}</span> USER_NAME (Mandatory) [COMMENT]..."</span>
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Store username and optional comment</span>
USER_NAME=<span class="hljs-string">"<span class="hljs-variable">${1}</span>"</span>
<span class="hljs-built_in">shift</span>
COMMENT=<span class="hljs-string">"<span class="hljs-variable">${@}</span>"</span>  

<span class="hljs-comment"># Generate a random password</span>
PASSWORD=$(date +%s%N)

<span class="hljs-comment"># Create the user</span>
useradd -c <span class="hljs-string">"<span class="hljs-variable">${COMMENT}</span>"</span> -m <span class="hljs-string">"<span class="hljs-variable">$USER_NAME</span>"</span>
<span class="hljs-keyword">if</span> [[ $? -ne 0 ]];             <span class="hljs-comment"># If the previous command fails then only execute this</span>
<span class="hljs-keyword">then</span>       
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"The account could not be created"</span>
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Set the password for the user</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">${USER_NAME}</span>:<span class="hljs-variable">${PASSWORD}</span>"</span> | chpasswd
<span class="hljs-keyword">if</span> [[ $? -ne 0 ]];              <span class="hljs-comment"># If the previous command fails then only execute this</span>
<span class="hljs-keyword">then</span>  
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Password could not be set"</span>
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>

<span class="hljs-comment"># Force password change on first login</span>
passwd -e <span class="hljs-string">"<span class="hljs-variable">$USER_NAME</span>"</span>    <span class="hljs-comment"># The user will be required to change their password upon first login.</span>

<span class="hljs-comment"># Display credentials</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Username: <span class="hljs-variable">$USER_NAME</span>"</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Password: <span class="hljs-variable">$PASSWORD</span>"</span>
<span class="hljs-built_in">echo</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-subst">$(hostname)</span>"</span>
</code></pre>
<h2 id="heading-breaking-down-the-key-sections">Breaking Down the Key Sections</h2>
<h3 id="heading-1-ensuring-the-script-runs-as-root">1. Ensuring the Script Runs as Root</h3>
<pre><code class="lang-bash"><span class="hljs-keyword">if</span> [[ <span class="hljs-string">"<span class="hljs-variable">${UID}</span>"</span> -ne 0 ]];  <span class="hljs-comment"># user must have right permission or he must be root user to execute the script.</span>
<span class="hljs-keyword">then</span>
    <span class="hljs-built_in">echo</span> <span class="hljs-string">"Please run with sudo or root."</span>  
    <span class="hljs-built_in">exit</span> 1
<span class="hljs-keyword">fi</span>
</code></pre>
<p>Since only root can create users, this check ensures the script isn't run by an unprivileged user.</p>
<h3 id="heading-2-generating-a-secure-password">2. Generating a Secure Password</h3>
<pre><code class="lang-bash">PASSWORD=$(date +%s%N) <span class="hljs-comment"># Generates the complex password. date -&gt; in sec + in nano ssec</span>
</code></pre>
<p>This command creates a password using the current system timestamp (seconds + nanoseconds). Since timestamps are always unique, this ensures randomness.</p>
<h3 id="heading-3-setting-the-users-password">3. Setting the User’s Password</h3>
<pre><code class="lang-bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">${USER_NAME}</span>:<span class="hljs-variable">${PASSWORD}</span>"</span> | chpasswd    <span class="hljs-comment"># chpasswd -&gt; to set the password non-interactively.</span>
</code></pre>
<p>Instead of using <code>passwd</code>, which requires manual input, we use <code>chpasswd</code> to set the password non-interactively. The <code>echo</code> command formats it as <code>username:password</code> before passing it to <code>chpasswd</code>.</p>
<h2 id="heading-running-the-script">Running the Script</h2>
<p>To use this script:</p>
<ol>
<li><p>Save it as <code>create_user.sh</code>.</p>
</li>
<li><p>Give it execute permissions: <code>chmod +x create_user.sh</code></p>
</li>
<li><p>Run it with root privileges:</p>
<pre><code class="lang-bash"> sudo ./create_user.sh newuser <span class="hljs-string">"New User Account"</span>
</code></pre>
</li>
</ol>
<p>The output will show the username and generated password. The user will be required to change their password upon first login.</p>
<pre><code class="lang-bash">omkar@Om-ThinkPad-T490:~/projects$ sudo ./create_user.sh newuser1 <span class="hljs-string">"New User Account"</span> 
passwd: password expiry information changed.
Username: newuser1

Password: 1739345135373630874

Hostname: Om-ThinkPad-T490
</code></pre>
<pre><code class="lang-plaintext">omkar@Om-ThinkPad-T490:~/projects$ su - newuser1
Password: 1739345135373630874
You are required to change your password immediately (administrator enforced).
Changing password for newuser1.
Current password:xxxxxxxxxxxxxxxxxxx
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This Bash script provides an efficient way to automate user creation in Linux. By using <code>chpasswd</code> for password assignment and enforcing password updates, it ensures security while streamlining administration. Automating repetitive tasks like this can significantly improve system management and reduce human errors.</p>
<p>Have any questions or suggestions? Let me know in the comments!</p>
]]></content:encoded></item></channel></rss>