BLOG POSTS
How to Add and Delete Users on Ubuntu 24

How to Add and Delete Users on Ubuntu 24

Managing user accounts on Ubuntu 24 is one of those fundamental skills that’ll save your bacon countless times when running servers. Whether you’re spinning up a fresh VPS for a client project, setting up development environments, or managing a multi-user system, knowing how to properly add and delete users isn’t just handy—it’s essential. This guide walks you through everything from basic user creation to advanced permission management, complete with real-world scenarios you’ll actually encounter in production. We’ll cover the gotchas, best practices, and automation tricks that separate the pros from the newbies.

How User Management Works in Ubuntu 24

Ubuntu’s user management system builds on traditional Unix/Linux principles but adds some modern conveniences. At its core, you’ve got three main tools: useradd (the low-level command), adduser (the friendly wrapper), and deluser/userdel for removal. The system stores user info in /etc/passwd, passwords in /etc/shadow, and group data in /etc/group.

Here’s what happens behind the scenes when you create a user:

  • System assigns a unique UID (User ID) and GID (Group ID)
  • Creates a home directory (usually /home/username)
  • Copies skeleton files from /etc/skel
  • Sets up default shell and permissions
  • Updates system databases

Ubuntu 24 introduces some improvements in user management, including better integration with systemd and enhanced security defaults. The minimum UID for regular users starts at 1000, and the system automatically handles group creation more intelligently than previous versions.

Step-by-Step User Addition Guide

Let’s dive into the practical stuff. I’ll show you multiple approaches, from quick-and-dirty to production-ready setups.

Method 1: Using adduser (Recommended for Most Cases)

The adduser command is your best friend for interactive user creation. It’s a Perl script that wraps around useradd and makes everything painless:

sudo adduser johndoe

This interactive command will prompt you for:

  • Password (twice for confirmation)
  • Full name
  • Room number, work phone, home phone (optional)
  • Final confirmation

For a complete setup with sudo privileges:

# Add the user
sudo adduser johndoe

# Add to sudo group
sudo usermod -aG sudo johndoe

# Verify the user was created
id johndoe
groups johndoe

Method 2: Using useradd (For Scripts and Automation)

When you’re automating server setup or need more control, useradd is your go-to:

# Basic user creation
sudo useradd -m -s /bin/bash johndoe

# Set password
sudo passwd johndoe

# More comprehensive setup
sudo useradd -m -s /bin/bash -G sudo,www-data -c "John Doe Developer" johndoe
sudo passwd johndoe

Key flags breakdown:

  • -m: Creates home directory
  • -s: Sets shell (bash in this case)
  • -G: Adds to supplementary groups
  • -c: Adds comment/full name

Advanced User Creation Scenarios

Creating a system user for services (no shell, no home directory):

sudo useradd -r -s /bin/false -M serviceuser

Creating a user with specific UID and home directory:

sudo useradd -u 1500 -m -d /custom/path/johndoe -s /bin/bash johndoe

User Deletion: The Right Way

Deleting users seems straightforward, but there are several approaches depending on what you want to preserve or obliterate.

Safe Deletion (Preserves Home Directory)

# Remove user but keep home directory
sudo deluser johndoe

# Verify deletion
id johndoe  # Should return "no such user"
ls -la /home/johndoe  # Directory still exists

Complete Removal

# Remove user and home directory
sudo deluser --remove-home johndoe

# Nuclear option - remove everything including mail spool
sudo deluser --remove-all-files johndoe

Using userdel (More Control)

# Remove user only
sudo userdel johndoe

# Remove user and home directory
sudo userdel -r johndoe

# Force removal even if user is logged in
sudo userdel -f johndoe

Real-World Examples and Use Cases

Scenario 1: Setting Up Development Team Access

You’re managing a VPS for a development team. Here’s a script to add multiple developers:

#!/bin/bash
# dev-team-setup.sh

DEVELOPERS=("alice" "bob" "charlie")
DEV_GROUP="developers"

# Create developers group
sudo groupadd $DEV_GROUP

for dev in "${DEVELOPERS[@]}"; do
    # Create user with home directory
    sudo adduser --gecos "$dev Developer" $dev
    
    # Add to developers group and give sudo access
    sudo usermod -aG $DEV_GROUP,sudo $dev
    
    # Set up SSH directory
    sudo -u $dev mkdir -p /home/$dev/.ssh
    sudo -u $dev chmod 700 /home/$dev/.ssh
    
    echo "✅ Created user: $dev"
done

Scenario 2: Web Server User Management

Setting up users for web applications on a dedicated server:

# Create web application user
sudo adduser --system --group --home /var/www/myapp webappuser

# Set proper permissions
sudo chown -R webappuser:webappuser /var/www/myapp
sudo chmod -R 755 /var/www/myapp

# Add to www-data group for Nginx/Apache integration
sudo usermod -aG www-data webappuser

Scenario 3: Temporary User Creation

Creating a temporary user for contractors or short-term access:

# Create user with expiration date
sudo useradd -m -s /bin/bash -e 2024-12-31 contractor

# Set password
sudo passwd contractor

# Limit to specific directories
sudo usermod -d /home/contractor contractor
sudo chmod 750 /home/contractor

Comparison: Different User Management Approaches

Method Pros Cons Best For
adduser Interactive, user-friendly, handles defaults well Not great for automation Manual user creation, beginners
useradd Scriptable, precise control, fast Requires manual home directory creation Automation, batch operations
GUI Tools Visual, intuitive Limited functionality, not available on servers Desktop environments only

Common Pitfalls and How to Avoid Them

The “Orphaned Files” Problem

When you delete a user but keep their files, those files become owned by the UID number instead of a username. This creates security and maintenance headaches:

# Check for orphaned files after user deletion
sudo find / -nouser -ls 2>/dev/null

# Reassign ownership to a new user
sudo find /home -uid 1001 -exec chown newuser:newuser {} \;

Locked Out Scenarios

Always maintain multiple ways to access root privileges. Here’s a recovery approach if you accidentally remove your only sudo user:

# Boot to recovery mode and remount filesystem
sudo mount -o remount,rw /

# Add user back to sudo group
sudo usermod -aG sudo yourusername

# Or create emergency admin user
sudo adduser emergency
sudo usermod -aG sudo emergency

Automation and Integration Possibilities

Modern server management often involves integrating user management with configuration management tools. Here’s how Ubuntu user management fits into larger automation workflows:

Ansible Integration

# ansible-playbook user-management.yml
---
- hosts: all
  become: yes
  tasks:
    - name: Create development users
      user:
        name: "{{ item }}"
        shell: /bin/bash
        groups: sudo,developers
        append: yes
      loop:
        - alice
        - bob
        - charlie

Cloud-Init Integration

For VPS deployments, you can automate user creation at instance launch:

# cloud-config.yml
users:
  - name: developer
    groups: sudo
    shell: /bin/bash
    ssh_authorized_keys:
      - ssh-rsa AAAAB3NzaC1yc2EAAAA... user@hostname

Advanced Security Considerations

Ubuntu 24 includes several security enhancements for user management:

  • PAM Integration: Better password policies and authentication modules
  • AppArmor Profiles: Automatic profile generation for new users
  • Systemd Integration: Better resource management and security boundaries

Enable advanced password policies:

# Install password quality checking
sudo apt update && sudo apt install libpam-pwquality

# Configure in /etc/pam.d/common-password
# Add: password requisite pam_pwquality.so retry=3 minlen=12 difok=3

Monitoring and Auditing User Activities

Keep track of user activities with built-in tools:

# Check user login history
last

# Monitor current users
who
w

# Check failed login attempts
sudo grep "Failed password" /var/log/auth.log

# Set up user activity monitoring
sudo apt install acct
sudo service psacct start

Related Tools and Utilities

Several tools complement Ubuntu’s built-in user management:

  • LDAP Integration: For enterprise environments (Ubuntu LDAP Guide)
  • Fail2ban: Automated security responses
  • sudo alternatives: doas for minimal privilege escalation
  • User quota management: quota and quotatool

Setting up user quotas:

# Enable quotas on filesystem
sudo apt install quota quotatool
sudo quotacheck -cum /home
sudo quotaon /home

# Set user quota (100MB soft, 200MB hard limit)
sudo setquota -u johndoe 100000 200000 0 0 /home

Performance and Scalability Considerations

When managing hundreds or thousands of users, performance becomes crucial:

  • NSCD: Name Service Cache Daemon for faster lookups
  • SSSD: System Security Services Daemon for enterprise integration
  • Directory services: LDAP, Active Directory integration

Statistics show that systems with over 1000 users benefit significantly from implementing NSCD, reducing user lookup times by up to 70%.

Troubleshooting Common Issues

Here are solutions to frequent problems:

User Creation Fails

# Check available UIDs
sudo awk -F: '$3 >= 1000 {print $1, $3}' /etc/passwd | sort -k2 -n

# Check disk space
df -h /home

# Verify group existence
getent group groupname

Permission Problems

# Reset home directory permissions
sudo chown -R username:username /home/username
sudo chmod 755 /home/username

# Fix skeleton directory issues
sudo cp -r /etc/skel/. /home/username/
sudo chown -R username:username /home/username

Conclusion and Best Practices

Mastering user management in Ubuntu 24 opens up powerful possibilities for server administration, from simple VPS setups to complex multi-user environments. The key is choosing the right tool for each scenario: adduser for interactive creation, useradd for automation, and always thinking about security implications.

My recommendations:

  • For beginners: Start with adduser and gradually move to useradd as you build scripts
  • For production servers: Always use configuration management tools like Ansible for consistency
  • For security-critical environments: Implement proper auditing, quotas, and consider LDAP integration
  • For automation: Build reusable scripts that handle both creation and cleanup scenarios

Whether you’re managing a single VPS or a fleet of dedicated servers, these user management skills will serve you well. The investment in learning proper user management pays dividends in security, maintainability, and your own sanity when things go sideways at 3 AM.

Remember: good user management isn’t just about creating and deleting accounts—it’s about building maintainable, secure systems that scale with your needs. Take the time to implement proper procedures now, and future you will thank you.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked