الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

useradd

Create a new user account (and by default its private group and home folder).

المستوى
امتحان RHCSA
المجموعة
المستخدمون والمجموعات
يحتاج root
نعم
الحزمة
shadow-utils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

useradd writes the new account to /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow. On RHEL it also creates a group with the same name and a home folder in /home, with a copy of the files in /etc/skel.

The new account has no password yet, so nobody can log in with it. Set one with passwd.

Values you do not give come from /etc/login.defs (UID range, password aging, home mode) and /etc/default/useradd (shell, home base, skel). useradd -D shows and changes the second file.

طريقة الكتابة

useradd [OPTION]... LOGIN
useradd -D
useradd -D [-b BASE] [-e DATE] [-f DAYS] [-g GROUP] [-s SHELL]

الخيارات 26

الأكثر استخدامًا

Account data

-c, --comment COMMENT
Comment, mostly the full name (GECOS field).
-u, --uid UID
Use this UID.
-o, --non-unique
Allow a UID that is already used (with -u).
-s, --shell SHELL
Login shell, like /bin/bash or /sbin/nologin.
-e, --expiredate DATE
The account expires on this date (YYYY-MM-DD).
-f, --inactive DAYS
Lock the account this many days after the password expired (-1 = never).
-p, --password HASH
Set an already hashed password (not the clear text; not recommended).
-r, --system
Create a system account: UID under 1000, no aging, no home folder by default.
--badname
Allow names that do not follow the normal rules.

Groups

-g, --gid GROUP
The primary group (name or GID; it must exist).
-G, --groups GROUP1[,GROUP2...]
Extra (supplementary) groups.
-U, --user-group
Create a group with the same name as the user (default on RHEL).
-N, --no-user-group
Do not create a private group; use -g or the default GROUP.

Home folder

-d, --home-dir DIR
Use this home folder.
-m, --create-home
Create the home folder (default on RHEL via CREATE_HOME).
-M, --no-create-home
Do not create the home folder.
-b, --base-dir DIR
Base folder for homes (home = DIR/LOGIN).
-k, --skel DIR
Copy the start files from this folder instead of /etc/skel (with -m).

Other

-D, --defaults
Show or change the defaults in /etc/default/useradd.
-K, --key KEY=VALUE
Override a value of /etc/login.defs, like -K UID_MIN=5000.
-l, --no-log-init
Do not add the user to the lastlog and faillog files.
-F, --add-subids-for-system
Add subuid/subgid ranges also for system accounts.
-Z, --selinux-user SEUSER
The SELinux user for this login (like staff_u).
-R, --root DIR
Work in this chroot folder.
-P, --prefix DIR
Use the files under this prefix folder.
-h, --help
Show help.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Create a user with a full name

    useradd -c "Lisa Hassan" lisa
    passwd lisa

    Without a password nobody can log in.

  2. 2Create a user with extra groups

    useradd -G wheel,webadmins omar
    id omar
    uid=1002(omar) gid=1002(omar) groups=1002(omar),10(wheel),1100(webadmins)

    The groups must exist.

  3. 3Choose the UID

    useradd -u 1500 sam

    Same UID on all servers keeps NFS file owners right.

  4. 4Create a service account

    useradd -r -s /sbin/nologin -d /var/lib/myapp -M myapp

    No login shell, no home, UID under 1000 (least privilege for an app).

  5. 5Create a temporary account that expires

    useradd -e 2026-12-31 -c "Contractor" contractor1

    After this date the account cannot log in.

  6. 6Show the defaults

    useradd -D
    GROUP=100
    HOME=/home
    INACTIVE=-1
    EXPIRE=
    SHELL=/bin/bash
    SKEL=/etc/skel

    These values come from /etc/default/useradd.

  7. 7Change the default shell for new users

    useradd -D -s /bin/zsh

    Only new users get it.

  8. 8Use another start folder for a team

    useradd -m -k /etc/skel-dev dev1

    Developers get their own .bashrc and tools config.

  9. 9Map a user to a confined SELinux user

    useradd -Z staff_u admin2

    A confined admin cannot do everything even as root (hardening).

  10. 10Create many users from a list

    for u in anna bob carol; do useradd -G sales "$u"; echo password | passwd --stdin "$u"; done

    Only for labs: real users need a good password each.

  11. 11Check the new account

    getent passwd lisa; ls -la /home/lisa
    lisa:x:1001:1001:Lisa Hassan:/home/lisa:/bin/bash

    The home folder has the files from /etc/skel.

انتبه

  • useradd does not set a password. Use passwd after it.
  • -p wants a hash, not the password text. useradd -p secret gives an account that cannot log in.
  • Changes in /etc/skel or login.defs only affect new users.

الملفات

/etc/passwd, /etc/shadow
Account and password data.
/etc/group, /etc/gshadow
Group data.
/etc/login.defs
UID/GID ranges, password aging, home mode, umask.
/etc/default/useradd
Defaults for shell, home base and skel.
/etc/skel/
Start files for new home folders.

رموز الخروج

0 :: Done. 1 :: Could not update the password file. 2 :: Wrong syntax. 3 :: Wrong value for an option. 4 :: UID already used. 6 :: The group does not exist. 9 :: The user name is already used. 12 :: Could not create the home folder. 14 :: Could not update the SELinux user mapping.