Master Linux User Management: Creating and Managing Users in Linux

Prerequisites

Basic Linux command-line knowledge
Access to a terminal or SSH Session
Root or sudo privileges

Introduction

Managing users on a Linux system is an essential skill for administrators and power users alike. Whether you're setting up a server, securing a multi-user environment, or simply customizing user permissions, understanding how to effectively create, modify, and manage user accounts is crucial. Linux provides a variety of tools for user management, making it flexible and powerful, but also requiring a certain level of understanding to use efficiently.

In this guide, we'll walk you through the basics of creating and managing users in Linux, from simple user creation commands to more advanced management techniques. By the end, you'll have a solid grasp of how to maintain a secure and organized system with the right user permissions. Let's dive in!

How to Create New User Accounts in Linux and Unix Using the useradd Command

To create new user accounts in Linux and Unix systems, you can use the useradd command. This command is used by administrators to add new users to the system. Here's a step-by-step guide on how to use useradd:

Basic Syntax of useradd Command

sudo useradd [options] username

Creating a Basic User

sudo useradd username

Setting a User’s Password

sudo passwd username

Checking the User Information

id username

Deleting a User

sudo userdel username

If you want to remove the user’s home directory as well, use the -r option:

sudo userdel -r username

To verify that a user has been deleted in Linux, you can check for the absence of the user in several places. Here are the steps to confirm:

  • check the /etc/passwd file
    • grep username /etc/passwd
  • check the /etc/shadow file
    • grep username /etc/shadow
  • check the user's home directory
    • ls /home/username
  • check for running processes
    • ps -u username
  • verify groups
    • grep username /etc/group

Create a new administrative user

To create a new user with administrative privileges, follow these steps:

Create a new user

Use the useradd command to create a new user. Replace adminuser with your desired username.

sudo useradd -m -s /bin/bash adminuser

Set a password for the user

sudo passwd adminuser

Add the user to the sudo group

sudo usermod -aG sudo adminuser

If the sudo group does not exist on your system, it may be using a different group to grant administrative privileges. On some Linux distributions, the group that has sudo privileges is often called wheel (especially on CentOS, Red Hat, or Fedora-based systems).

sudo usermod -aG wheel adminuser

Verify the User's Sudo Privileges

su - adminuser
sudo whoami

If the user has sudo privileges, the output should be root.

  • su - adminuser
    • su: Stands for substitute user. It allows you to switch to another account.
  • - (hyphen): When used with su, it simulates a login as if you were logging in from scratch.

Disable Root Login (Optional)

The root account is often targeted by automated scanning tools that search for publicly accessible systems. Disabling direct root login via SSH reduces the attack surface and makes it more difficult for attackers to exploit potential weaknesses or misconfigurations.

  • Edit the SSH Configuration File (for SSH logins)
    • sudo vim /etc/ssh/sshd_config
    • Find the following line and change it to no:
      • PermitRootLogin no
    • Restart the SSH service
      • systemctl restart sshd

The administrative user you created (with sudo privileges) should still be able to execute administrative commands by prefixing them with sudo.

Monitoring User Activity

To track user login activity, use the last command to see recent logins:

last username

This shows login history for the user.

Locking the User Account

Locking the User Account with passwd command

  • lock the user account
    • sudo passwd - l username
  • unlock the user account
    • sudo passwd -u username

Final Thoughts

Mastering user management is a key component of maintaining a secure and efficient Linux system. By understanding how to create, modify, and manage users, as well as assign the appropriate permissions, you can ensure that your system runs smoothly and securely. Whether you're managing a personal machine or administering a multi-user server, these skills will help you maintain control over user access and system resources.

Remember, user management is not just about adding or removing accounts – it’s about creating a structured, organized environment where each user has the appropriate level of access to do their job without compromising system security. Practice these commands and tools to get comfortable with managing users, and soon, it will become second nature. Happy managing!

Popular (all time)

Related articles

How to Edit Your WordPress Admin Username and Author Slug via MySQL

In this guide, we’ll walk you through the process of editing both the admin username and author slug using MySQL. Whether you’re looking to strengthen your site's security or simply personalize your author URL, this straightforward method will help you make the changes with ease.

Getting Started with React

In this guide, we will cover the basics of setting up a local development environment, starting a simple React project, and the basics of how it works.

How to Simplify Your Terminal with Custom Bash Aliases

By creating custom shortcuts for your most-used commands, you can save time, reduce errors, and make your terminal experience faster and more enjoyable. In this guide, we’ll show you how to create and manage your own Bash aliases to simplify your terminal workflow and boost your productivity.