crear usuario incluyendo un archivo predefinido en su directorio home.
Los parámetros por defecto para crear un usuario se encuentran en dos archivos:
/etc/default/useradd
root@localhost ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#
/etc/default/useradd
root@localhost ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#
listar
[root@localhost ~]# /sbin/useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#
Para cambiar estos valores por defecto se debe utilizar los parametros expicados en la documentación:
Changing the default values
When invoked with only the -D option, useradd will display the current default
values. When invoked with -D plus other options, useradd will update the default
values for the specified options. Valid default-changing options are:
-b, --base-dir BASE_DIR
The path prefix for a new user's home directory. The user's name will be affixed
to the end of BASE_DIR to form the new user's home directory name, if the -d
option is not used when creating a new account.
This option sets the HOME variable in /etc/default/useradd.
-e, --expiredate EXPIRE_DATE
The date on which the user account is disabled.
This option sets the EXPIRE variable in /etc/default/useradd.
-f, --inactive INACTIVE
The number of days after a password has expired before the account will be
disabled.
This option sets the INACTIVE variable in /etc/default/useradd.
-g, --gid GROUP
The group name or ID for a new user's initial group (when the -N/--no-user-group
is used or when the USERGROUPS_ENAB variable is set to no in /etc/login.defs).
The named group must exist, and a numerical group ID must have an existing entry.
This option sets the GROUP variable in /etc/default/useradd.
-s, --shell SHELL
The name of a new user's login shell.
This option sets the SHELL variable in /etc/default/useradd.
[root@localhost ~]# /sbin/useradd -D -e 100
[root@localhost ~]# /sbin/useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=100
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#
/etc/login.defs
[root@localhost ~]# cat /etc/login.defs
#
# Please note that the parameters in this configuration file control the
# behavior of the tools from the shadow-utils component. None of these
# tools uses the PAM mechanism, and the utilities that use PAM (such as the
# passwd command) should therefore be configured elsewhere. Refer to
# /etc/pam.d/system-auth for more information.
#
# *REQUIRED*
# Directory where mailboxes reside, _or_ name of file, relative to the
# home directory. If you _do_ define both, MAIL_DIR takes precedence.
# QMAIL_DIR is for Qmail
#
#QMAIL_DIR Maildir
MAIL_DIR /var/spool/mail
#MAIL_FILE .mail
# Password aging controls:
#
# PASS_MAX_DAYS Maximum number of days a password may be used.
# PASS_MIN_DAYS Minimum number of days allowed between password changes.
# PASS_MIN_LEN Minimum acceptable password length.
# PASS_WARN_AGE Number of days warning given before a password expires.
#
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
#
# Min/max values for automatic uid selection in useradd
#
UID_MIN 1000
UID_MAX 60000
# System accounts
SYS_UID_MIN 201
SYS_UID_MAX 999
#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN 1000
GID_MAX 60000
# System accounts
SYS_GID_MIN 201
SYS_GID_MAX 999
#
# If defined, this command is run when removing a user.
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD /usr/sbin/userdel_local
#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
#
CREATE_HOME yes
# The permission mask is initialized to this value. If not specified,
# the permission mask will be initialized to 022.
UMASK 077
# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes
# Use SHA512 to encrypt password.
ENCRYPT_METHOD SHA512
Para cambiar los valores de este archivo de configuración, de debe editar directamente.
El directorio /etc/skel es un esqueleto del directorio por default para cualquier usuario.
[root@centOS-testing02 ~]# cd /etc/skel/
[root@centOS-testing02 skel]# touch default.archive
[root@centOS-testing02 skel]# ls -ltr
total 0
-rw-r--r--. 1 root root 0 ene 25 15:41 default.archive
[root@centOS-testing02 skel]#
Al crear un usuario automáticamente se crea una copia del archivo en el respectivo directorio /home/user
[root@centOS-testing02 ~]# /usr/sbin/useradd estrellita
[root@centOS-testing02 ~]# /usr/bin/passwd estrellita
Changing password for user estrellita.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@centOS-testing02 ~]#
[root@centOS-testing02 ~]# su - estrellita
[estrellita@centOS-testing02 ~]$ pwd
/home/estrellita
[estrellita@centOS-testing02 ~]$ ls -ltr
total 0
-rw-r--r--. 1 estrellita estrellita 0 Jan 25 15:41 default.archive
[estrellita@centOS-testing02 ~]$
crear usuario con un directorio por default distinto al /home/user
en el siguiente ejemplo, el directorio home del usuario será /special/user4
Tener en cuanta que el directorio raíz /special debe existir al momento de la creación del usuario.
[root@centOS-testing02 ~]# ls -ld /special/
drwxr-xr-x. 4 root root 4096 ene 28 16:47 /special/
[root@centOS-testing02 ~]#
Tener en cuanta que el directorio raíz /special debe existir al momento de la creación del usuario.
[root@centOS-testing02 ~]# ls -ld /special/
drwxr-xr-x. 4 root root 4096 ene 28 16:47 /special/
[root@centOS-testing02 ~]#
[root@centOS-testing02 ~]# /usr/sbin/useradd user4 -d /special/user4
[root@centOS-testing02 ~]# ls -ltr /special/
total 8
drwx------. 2 user4 user4 4096 ene 28 16:47 user4
[root@centOS-testing02 ~]#
Crear usuario sin directorio home
[root@localhost ~]# /usr/sbin/useradd -M userest6Verificar
[root@localhost ~]# grep userest6 /etc/passwd
userest6:x:1003:1005::/home/userest6:/bin/bash
[root@localhost ~]# ls -ltr /home/
total 28
drwx------. 2 root root 16384 nov 17 15:59 lost+found
drwx------. 2 userest userest 4096 feb 5 15:55 userest
drwx------. 2 userest4 userest4 4096 feb 8 17:24 userest4
drwx------. 2 userest5 userest5 4096 feb 8 17:40 userest5
[root@localhost ~]#
Crear un usario de sistema(cuenta con restricciones):
Al crear una cuenta de sistema,
- no se añade info complementaria ni passwd al archivo /etc/shadow
- su identificador de usuario y grupo se rige bajo los parametros SYS_UID_MIN y SYS_UID_MAX
- no se crea un directorio home, ecepto si se especifica como parámetro.
- Se debe utilizar el parámetro "useradd -r"
[root@localhost ~]# /sbin/useradd -r accountrestricted
Verificar info añadida
[root@localhost ~]# cat /etc/shadow | grep -i accountrestricted
accountrestricted:!!:16862::::::
[root@localhost ~]# ls -d /home/accountrestricted
ls: cannot access /home/accountrestricted: No such file or directory
[root@localhost ~]# cat /etc/passwd | grep accountrestricted
accountrestricted:x:505:505::/home/accountrestricted:/bin/bash
[root@localhost ~]#
[root@localhost ~]# id accountrestricted
uid=505(accountrestricted) gid=505(accountrestricted) groups=505(accountrestricted)
[root@localhost ~]#
login del usuario accountrestricted
[root@localhost ~]# su - accountrestricted
su: warning: cannot change directory to /home/accountrestricted: No such file or directory
-bash-4.2$
crear usuario con restricciones de shell
crear un usuario con shell restringida le quita la posibilidad al usuario de login, siendo una de las razones más comunes para emplear esta función.Verificar el listado de shells disponibles, entre las que se encuentra "nologin"
[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
[root@localhost ~]#
Crear el usuario indicando el tipo de shell de la lista "nologin"
[root@localhost ~]# /sbin/useradd -s /usr/sbin/nologin user99
[root@localhost ~]# cat /etc/passwd | grep user99
user99:x:1004:1006::/home/user99:/usr/sbin/nologin
[root@localhost ~]#
[root@localhost ~]# su - user99
This account is currently not available.
[root@localhost ~]#
intento de login remoto (polite refuse)
[gcepeda@santorini ~]$ ssh -l user99 192.168.56.103
user99@192.168.56.103's password:
Permission denied, please try again.
user99@192.168.56.103's password:
Permission denied, please try again.
user99@192.168.56.103's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[gcepeda@santorini ~]$
user99@192.168.56.103's password:
Permission denied, please try again.
user99@192.168.56.103's password:
Permission denied, please try again.
user99@192.168.56.103's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[gcepeda@santorini ~]$
crear usuario con determinado User ID
[root@localhost ~]# /usr/sbin/useradd -u 508 userest4
[root@localhost ~]# grep userest4 /etc/passwd
userest4:x:508:1003::/home/userest4:/bin/bash
[root@localhost ~]#
Intento de creación de otro user con el mismo userid
[root@localhost ~]# /sbin/useradd -u 508 user100
useradd: UID 508 is not unique
[root@localhost ~]#
[root@localhost ~]# /sbin/useradd -o -u 1005 user101
[root@localhost ~]# /sbin/useradd -o -u 1005 user102
[root@localhost ~]# /sbin/useradd -o -u 1005 user103
[root@localhost ~]# id user101
uid=1005(user101) gid=1007(user101) groups=1007(user101)
[root@localhost ~]# id user102
uid=1005(user101) gid=1007(user101) groups=1007(user101)
[root@localhost ~]# id user103
uid=1005(user101) gid=1007(user101) groups=1007(user101)
[root@localhost ~]#
En el siguiente ejemplo se despliega los campos usuario:UID:GID, con UID=1005
[root@localhost ~]# grep userest4 /etc/passwd
userest4:x:508:1003::/home/userest4:/bin/bash
[root@localhost ~]#
Intento de creación de otro user con el mismo userid
[root@localhost ~]# /sbin/useradd -u 508 user100
useradd: UID 508 is not unique
[root@localhost ~]#
crear múltiples usuarios con el mismo User ID
se añade el parámetro "useradd -o"
[root@localhost ~]# /sbin/useradd -o -u 1005 user101
[root@localhost ~]# /sbin/useradd -o -u 1005 user102
[root@localhost ~]# /sbin/useradd -o -u 1005 user103
[root@localhost ~]# id user101
uid=1005(user101) gid=1007(user101) groups=1007(user101)
[root@localhost ~]# id user102
uid=1005(user101) gid=1007(user101) groups=1007(user101)
[root@localhost ~]# id user103
uid=1005(user101) gid=1007(user101) groups=1007(user101)
[root@localhost ~]#
[root@localhost ~]# cut -d':' -f1,3,4 /etc/passwd | grep 1005
user101:1005:1007
user102:1005:1008
user103:1005:1009
[root@localhost ~]#
user101:1005:1007
user102:1005:1008
user103:1005:1009
[root@localhost ~]#
Crear usuario con un grupo del mismo nombre
[root@localhost ~]# /sbin/useradd user106 -U
[root@localhost ~]# id user106
uid=1006(user106) gid=1010(user106) groups=1010(user106)
[root@localhost ~]# cat /etc/passwd | grep user106
user106:x:1006:1010::/home/user106:/bin/bash
[root@localhost ~]#
[root@localhost ~]# cat /etc/group | grep user106
user106:x:1010:
[root@localhost ~]#
Crear usuario añadiendolo a varios grupos
Verificar los grupos disponibles
[root@localhost ~]# cat /etc/group
...
copito:x:1000:
webadmin:x:1001:copito
userest:x:1002:
userest4:x:1003:
[root@localhost ~]#
Añadir el usuario a los grupos 1000,1001,1002,1003
[root@localhost ~]# /sbin/useradd -G 1000,1001,1002 userest5
Verificar
En el archivo /etc/passwd se verifica la creación del usuario y el grupo primario al cual pertenece.
[root@localhost ~]# grep userest5 /etc/passwd
userest5:x:1002:1004::/home/userest5:/bin/bash
En el archivo /etc/group se verifica que se haya anexado el ausuario a los grupos requeridos.
[root@localhost ~]# grep userest5 /etc/group
copito:x:1000:userest5
webadmin:x:1001:copito,userest5
userest:x:1002:userest5
userest5:x:1004:
[root@localhost ~]#
Fuente:
http://www.tecmint.com/add-users-in-linux/
http://www.golinuxhub.com/2014/04/10-practical-examples-to-use-useradd.html
http://www.golinuxhub.com/2014/04/10-practical-examples-to-use-useradd.html
No hay comentarios:
Publicar un comentario