Skip to main content

2. Adding user accounts

  1. Let's create a new user:

    useradd --help
    useradd -m -s /bin/bash test1

    Only the superuser can create new accounts, so let's use sudo:

    sudo useradd -m -s /bin/bash test1

    The option -m tells it to create a home directory for the user, which is by default located at /home/, and the option -s tells it what shell to use for this user.

    ls /home/
    ls -al /home/test1/

    Users normally cannot access the content of each other. Superuser can access everything.

    sudo ls -al /home/test1/

    We should also set a password for test1:

    sudo passwd test1
  2. Let's switch to this user and try some commands:

    sudo su -l test1

    su means: 'switch user'

    pwd
    whoami
    id

    When a user account is created, the system assigns it a number called user ID or uid, which is mapped to a username for the sake of humans. Each user is also assigned a primary group ID (or gid) and may belong to additional groups.

    exit

    Back to the first user.

    pwd
    whoami
    id
  3. User accounts are defined in /etc/passwd and groups in /etc/group. However the passwords of the users are stored in /etc/shadow:

    ls -l /etc/passwd
    file /etc/passwd
    less /etc/passwd

    You can see that besides the normal users there are also some system users, including the superuser (or root), with uid=0.

    ls -l /etc/group
    file /etc/group
    less /etc/group
    ls -l /etc/shadow
    file /etc/shadow
    less /etc/shadow

    You don't have permission to see the content of this file.

    sudo less /etc/shadow
  4. The command chown can be used to change the owner and/or the group of a file. Let's try it:

    chown root: foo.txt
    whoami

    Superuser privileges are required to use it. Let's try with sudo.

    sudo chown root: foo.txt
    ls -l foo.txt
    chown test1:root foo.txt
    ls -l foo.txt
Loading asciinema cast...