31 ago 2011

Eliminar / Borrar una cuenta de usuario en Linux

Como administradores de GNU/Linux, la tarea de eliminar una cuenta de usuario es muy importante. Es tan importante que merece la pena escribir un protocolo para ello. Por seguridad, por administración, por lo que sea hay que tomar en serio ésta tarea.

Para esto existe el comando 'userdel', el cual tiene que ser ejecutado como root. Por ejemplo:

[root@server ~]# /usr/sbin/userdel usuario

Un ejemplo ampliado de esto sería: borrar la cuenta de usuario 'angel' y su home asociado (opción -r):

[root@server ~]# /usr/sbin/userdel -r angel

El comando anterior borrará la cuenta de usuario, su home y los mensajes de correo existentes. Si existen archivos creados en otros directorios estos tendrán que ser buscados y borrados manualmente.

Archivo /etc/login.defs y /etc/deluser.conf

Los valores, por default, del comando 'userdel' son tomados del archivo /etc/login.defs en Red Hat, Fedora, CEntOS:

# *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     500
UID_MAX   60000

#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN     500
GID_MAX   60000

#
# 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 MD5 or DES to encrypt password? Red Hat use MD5 by default.
MD5_CRYPT_ENAB yes

Y /etc/deluser.conf en Debian y Ubuntu Linux:

# /etc/deluser.conf: `deluser' configuration.

# Remove home directory and mail spool when user is removed
REMOVE_HOME = 0

# Remove all files on the system owned by the user to be removed
REMOVE_ALL_FILES = 0

# Backup files before removing them. This options has only an effect if
# REMOVE_HOME or REMOVE_ALL_FILES is set.
BACKUP = 0

# target directory for the backup file
BACKUP_TO = "."

# delete a group even there are still users in this group
ONLY_IF_EMPTY = 0

# exclude these filesystem types when searching for files of a user to backup
EXCLUDE_FSTYPES = "(proc|sysfs|usbfs|devpts|tmpfs)"

Procedimiento:

A continuación les presento el procedimiento recomendado para borrar una cuenta de usuario. Primero hay que bloquear la cuenta:

[root@server ~]# /usr/bin/passwd -l angel

De ser necesario, respaldar los archivos del usuario:

[root@server ~]# tar -zcvf /backup/angel.tar.gz /home/angel/

'Matar' los procesos que pertenecen al usuario:

[root@server ~]# ps -fp $(pgrep -u angel)
[root@server ~]# killall -KILL -u angel

Borrar la cuenta del usuario:

[root@server ~]# /usr/sbin/userdel -r angel

Borrar las tareas at:

[root@server ~]# # find /var/spool/at/ -name "[^.]*" -type f -user angel -delete

Borrar las tareas de cron:

[root@server ~]# crontab -r -u angel

Borrar los trabajos de impresión:

[root@server ~]# lprm angel

Buscar todos los archivos propiedad del usuario:

[root@server ~]# find / -user angel -print

Por último, podemos buscar todos los archivos propiedad del usuario y cambiarlos de propietario:

[root@server ~]# find / -user angel -exec chown usuario:grupo {} \;

Listo, nos vemos en la siguiente entrada.

No hay comentarios:

Publicar un comentario