<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adesoji Awobajo</title>
    <description>The latest articles on DEV Community by Adesoji Awobajo (@soji).</description>
    <link>https://dev.to/soji</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F326909%2F767270fe-7bdf-48aa-a420-f981f36050c5.png</url>
      <title>DEV Community: Adesoji Awobajo</title>
      <link>https://dev.to/soji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/soji"/>
    <language>en</language>
    <item>
      <title>How to Setup Users and User Groups on Linux</title>
      <dc:creator>Adesoji Awobajo</dc:creator>
      <pubDate>Wed, 03 Jul 2024 10:38:07 +0000</pubDate>
      <link>https://dev.to/soji/how-to-setup-users-and-user-groups-on-linux-25ia</link>
      <guid>https://dev.to/soji/how-to-setup-users-and-user-groups-on-linux-25ia</guid>
      <description>&lt;p&gt;Setting up users and user groups is the first step to managing employees in your organisation. As a SysOps engineer one of the basic tools you must familiarise yourself with is linux and its environment. Its important that when creating users for your organisation you must properly configure what access and how each user interface within the organisation, their access, groups and permissions; All these can be done on your machine in a linux environment using a script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linux User and User Group Creation
&lt;/h2&gt;

&lt;p&gt;In this article, I will be showing you a step by step process on how you can setup users and configure specific user groups they should belong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we begin, make sure you have the following installed and ready to use:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A Virtual machine&lt;/strong&gt; - VM running on &lt;a href="https://www.linux.org/"&gt;&lt;strong&gt;Linux&lt;/strong&gt;&lt;/a&gt; environment, I recommend &lt;a href="https://ubuntu.com/"&gt;&lt;strong&gt;Ubuntu&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Basic understanding of linux commands.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A code editor&lt;/strong&gt;, I use &lt;a href="https://code.visualstudio.com/"&gt;&lt;strong&gt;Visual Studio Code&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: Create user file
&lt;/h2&gt;

&lt;p&gt;Specify a file where your users will be listed and the groups they should belong to. I recommend a simple output for this, so that it can be easy to identify. For this tutorial, we will be using a sample file users.txt, and its formatted as &lt;strong&gt;users;groups&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;soji;sudo,dev,www-data
ade;sudo,dev
ayo;dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the word before the semicolon represents the &lt;code&gt;user&lt;/code&gt; and after represents the &lt;code&gt;group(s)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In line one above, &lt;code&gt;soji&lt;/code&gt; is the &lt;strong&gt;user&lt;/strong&gt; and &lt;code&gt;sudo,dev,www-data&lt;/code&gt; are the &lt;strong&gt;user groups&lt;/strong&gt; to be created and assigned to the user; Similarly for line two &lt;strong&gt;user&lt;/strong&gt; is &lt;code&gt;ade&lt;/code&gt;, with groups &lt;code&gt;sudo,dev&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Create script file
&lt;/h2&gt;

&lt;p&gt;Open your code editor and create file, e.g &lt;code&gt;create_users.sh&lt;/code&gt;, you can also create the file using your terminal by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch create_users.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;Your script file will handle the actual logic of what will be done to &lt;code&gt;users.txt&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The thought process around this is to run a command for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bash create_users.sh users.txt 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which will be used to create users and groups.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Script Implementation
&lt;/h2&gt;

&lt;p&gt;First we need to check that a first argument is passed and if not should return an error and then exit the program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check if first argument is passed:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [[ ! $1 ]]; then
echo "Error: requires at least one arg to be passed, e.g bash create_users.sh &amp;lt;name-of-text-file&amp;gt;"
exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we need to be able to allow our script know when we pass &lt;code&gt;user.txt&lt;/code&gt; and then process it, and to go about this we check if &lt;code&gt;users.txt&lt;/code&gt; is passed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;The name of the file doesn't matter, but that its a file and type is of &lt;code&gt;text/plain&lt;/code&gt; which is the &lt;code&gt;mime-type&lt;/code&gt; for text files.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Check if file exists:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [ ! -f $1 ]
then
    echo "Error: file does not exists"
    exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Check if file type is &lt;code&gt;text/plain&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [[ ${1##*.} != "txt" &amp;amp;&amp;amp; "$(file -b --mime-type "$1")" != "text/plain" ]]
then
  echo "Error: required file type is text"
  exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next thing is we will need to read through each line of the &lt;code&gt;users.txt&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read line by line of users.txt:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Read the FILE
while IFS= read -r line || [ -n "$line" ]; 
do

# Assign variable for &amp;lt;user&amp;gt;
username=$(printf "%s" "$line"| cut -d \; -f 1)

# Assign variable for &amp;lt;groups&amp;gt;
usergroups=$(printf "%s" "$line"| cut -d \; -f 2)

echo "----- Start process for: '$username' -----"

# Create user
create_user $username

# Create user groups
for group in ${usergroups//,/ } ; do 
    create_group $group
    add_user_to_group $username $group
done

echo "----- Done with '$username' -----"
echo ""
done &amp;lt; $1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code block, here we read each line of &lt;code&gt;users.txt&lt;/code&gt; file, and extract the &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;user groups&lt;/code&gt;, having done this it is easy for us to create the user and groups.&lt;/p&gt;

&lt;p&gt;It contains the following functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;create_user&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;create_group&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;add_user_to_group&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have broken down each functions below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create user: &lt;code&gt;create_user&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
I created a function my in script implementation named &lt;code&gt;create_user&lt;/code&gt;.&lt;br&gt;
Functions are good for easier code readability and clean code structure, also helps to reuse a certain logic in different sections of your code or scripts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create_user() {
    username=$1
    password=$(gen_random_password)

    # If username exists, do nothing
    if [ ! $(cat /etc/passwd | grep -w $username) ]; then
        # Create the user with the specified username
        # User is created with a group as their name
        sudo useradd -m -s /bin/bash $username

        # Set the user's password
        echo "$username:$password" | sudo chpasswd
        msg="User '$username' created with the password '*******'"
        echo $msg
        log $msg

        # Save user data
        dir=/home/$username/$user_data_path

        create_file_in_directory $dir

        save_user_data $username $password $dir

        # Set file group to user and give read only acces
        sudo chgrp $username $dir
        sudo chmod 040 $dir
    fi
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A user is created from the above code block, we assign a password to the user, so they always have access their account and directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;gen_random_password&lt;/code&gt; function is used to generate random password for the user&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gen_random_password() {
    &amp;lt; /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c12
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After user and password is created, I then stored the user password on the user directory, I used the path a path:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;[user home directory]/var/secure/user_passwords.txt&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;for user with name &lt;code&gt;soji&lt;/code&gt;, its saved at:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;/home/soji/var/secure/user_passwords.txt&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Only the user is set to have access to it and its a &lt;code&gt;read only file&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create group: &lt;code&gt;create_group&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
We then need to assign the user to the usergroups passed in the users.txt file, but first we need to create the groups.&lt;/p&gt;

&lt;p&gt;If group already exists, the code block does nothing, this help for proper error handling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create_group() {
    # Create group
    # If group exists, do nothing
    if [ ! $(cat /etc/group | grep -w $1) ]; then
        sudo groupadd $1
        msg="Group created '$1'"
        echo $msg
        log $msg
    fi
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Add user to group: &lt;code&gt;add_user_to_group&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
When the group is created we assign user to the created group&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add_user_to_group() {
    #  Add user to group
   sudo usermod -aG $2 $1
   msg="'$1' added to '$2'"
   echo $msg
   log $msg
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Log function &lt;code&gt;log&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
I used this to log all the actions by passing the log data/message. Logs where logged into &lt;code&gt;/var/log/user_management.log&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log() {
    sudo printf "$*\n" &amp;gt;&amp;gt; $log_path
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Thats all!!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note: One thing you will observe is how I did some error handling in the script, this is important to avoid errors when you run your script.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Run script file
&lt;/h2&gt;

&lt;p&gt;Now its time to test our script.&lt;br&gt;
Run file by using the command below on your terminal, make sure you are the the correct path/directory where the files exists.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bash create_users.sh users.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Script OK?&lt;/strong&gt; you should see result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;File and path created: /var/log/user_management.log
----- Start process for: 'soji' -----
User 'soji' created with the password '*******'
File and path created: /home/soji/var/secure/user_passwords.txt
Group created 'sudo'
'soji' added to 'sudo'
Group created 'dev'
'soji' added to 'dev'
'soji' added to 'www-data'
----- Done with 'soji' -----

----- Start process for: 'ade' -----
User 'ade' created with the password '*******'
File and path created: /home/ade/var/secure/user_passwords.txt
'ade' added to 'sudo'
'ade' added to 'dev'
----- Done with 'ade' -----

----- Start process for: 'ayo' -----
User 'ayo' created with the password '*******'
File and path created: /home/ayo/var/secure/user_passwords.txt
'ayo' added to 'dev'
----- Done with 'ayo' -----
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To see all groups created run:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cat /etc/group
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To see all users and groups they belong run:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cat /etc/passwd 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;My full code implementation is available on Github: &lt;a href="https://github.com/sodiadrhain/linux-user-creation"&gt;Linux user creation code&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  HNG Internships
&lt;/h2&gt;

&lt;p&gt;Looking for ways to improve and develop you skills with world class talents, check out &lt;strong&gt;&lt;a href="https://hng.tech/internship"&gt;HNG Internship website&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to hire world class freelancers and developers, check:  &lt;strong&gt;&lt;a href="https://hng.tech/hire"&gt;Hire from HNG&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Setting up users and user groups with linux is a pretty straight forward process that allows you to specify the actions you ant to perform. By taking these steps, you can easily create users in your environment and attach them to different groups.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy SysOps(ing).&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>sysops</category>
      <category>devops</category>
      <category>bash</category>
    </item>
  </channel>
</rss>
