Notes on Unix and Linux user admin such as managing users and groups.

Users and Groups

create a new user

useradd -m -s /bin/bash -U -G <groups> USER_NAME

-m: create new home directory
-s <SHELL> : assign default shell
    * use `/usr/sbin/nologin`
-U: create group with same username
-G <GROUPS>: add to groups (comma separated)
    * add 'sudo' if needed

Example:

useradd -m -s /bin/bash -U -G adm,cdrom,sudo,audio,video,users,i2c johndoe

add user to other groups

# usermod -a -G <GROUPS> <USER>
# usermod -a -G ftp,sudo   johndoe

change user’s default shell

# usermod -s <SHELL> <USER>

Or use cshs as a user

chsh -s $(which zsh)

disable login by changing shell

# usermod -s /usr/sbin/nologin

create a new user (Debian only), easy version of useradd.

# adduser <USER> <GROUP>

/etc/adduser.conf : Default configuration for new users.

create group (Debian only), easy version of groupadd.

# addgroup <GROUP>

Login as another user

# su <USERNAME>

password

change password

passwd [<USER>] 

# change my password
passwd   

# change someone else's password
sudo passwd johndoe

view password status

passwd -S  #not in BSD

See man passwd for more , such as locking, unlocking, disabling, expiring

Special files

/etc/passwd: contains user’s name and shell.

Format:

<username>:<encrypted password>:<UID>:<GID>:<fullname>:<homedir>
  • <UID> : unique # identifying a user. Adduser will generate unique #.
  • <GID> : group that the user belongs to. Every user belongs to at least one group. To belong to more than 1 group, use ‘/etc/group’.

To disable account without deleting it, prepend * in password field. In Linux, it might be “!” instead of “*“. Also this can be done via passwd command

Poole:*XVdfsfjk$23:102:100:Laura

/etc/shadow: Encrypted user’s passwords. This corresponds exactly to /etc/passwd file.

/etc/group: Contains lists of all the groups and the user that belongs to that group.

Format:

<group name>:<password>:<GID>:<users>

student:*:300:dkim,dsanders,psam
faculty:*:301:bpeterson
  • <Password>: usually *.
  • <GID>: group id ie unique group #.
  • some groups are reserved for system like root, etc.
  • User may belong to more than 1 group.
  • addgroup, delgroup can be used but editing this file is easy.

Other Commands

deluser: Delete user (or userdel on some other system). To disable account, see ‘/etc/passwd’

ulimit : limits max processes per user http://tldp.org/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/x4733.html

Monitor user activity

  • psacct
  • acct

TTY, Console, Who is logged in?

  • /dev/tty*: tty, native terminal device.
  • /dev/vcs* : virtual console, Linux only.
  • /dev/pts/* : SSH, Telnet (pseudo terminal slave)

tty or who or w :find out which tty/vcs/pts I am on.

$ tty
/dev/pts/0

who : who was LAST logged on

$ who
vagrant  pts/0        2016-03-01 17:03 (10.0.2.2)

w: who’s currently logged on

$ w
18:00:11 up  1:02,  1 user,  load average: 0.00, 0.01, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         17:03    0.00s  0.06s  0.00s w