User Management

  • View existing users — cat /etc/passwd
  • Check if an user exist — grep {username} /etc/passwd
  • User passwords and password expiry information is stored in: — cat /etc/shadow
  • Create a new user — adduser {username}
  • Setup password for a user — passwd {username}
  • Remove User Account — userdel {username}
  • Remove the user’s home directory — userdel -r {username}
  • Rename user name — usermod -l new_user_name old_user_name
  • Change user’s ID — usermod -u UID username

Change User’s Home Directory

  • Change Home Directory for new users
    The default home directory for new users is defined by HOME variable in /etc/default/useradd
    — vi /etc/default/useradd
    #Replace “HOME=/home” with
    HOME=/new/path
  • Change Home Directory for existing users — usermod -m -d /path/to/new/home/dir userNameHere Where
    -d : Path to new home directory. -m : Moves contents of the current home directory to the new home directory.

Group Management

  • User group information is stored in — cat /etc/group
  • Add a new user to primary group — useradd -g {group-name} {username}
  • Add a new user to secondary group — useradd -G {group-name} {username}

  • Find out users group identity
    — id -ng {user-name}
    id -nG {user-name}
  • Add an existing user to secondary group — usermod -a -G {group-name} {username}
  • Change users primary group — usermod -g {group-name} {username}
  • Check if a group exists) — grep {groupname} /etc/group
  • Add a group — groupadd {groupname}
  • Delete a group — groupdel {groupname}
  • View users’ group membeship — id {username}
  • Add user to multiple groups — useradd -G {group1},{group2},{group3} {username}
  • Remove user from a group by entering the groups to be preserved. If the user is currently a member of a group which is not listed, the user will be removed from the group. — usermod -G {group1-to be preserved,group2,group3} {user-name}

Change file/folder owner/group

  • Change files ownership to a user — chown {username} {file or folder}
  • Change files ownership to a user on files and directories recursively — chown -R {username} /var/www/html
  • Change files ownership to a group — chgrp {groupname} {file or folder}
  • To setup a file readable/executable by everyone and writable by the owner only — chmod 755 {file or folder}
  • change permissions for all files and directories within a directory — chmod -R 755 directory-name/

Delegate Permissions

  • Edit sudo config file — vi /etc/sudoers or run #visudo
  • If you run the command vi /etc/sudoers
    Sudoers file is read-only!
    To save and quit the sudoers file.
    — press esc
    type :w !sudo tee %
    type q!
  • Append rights — USER HOSTNAME=COMMAND Where, USER: Name of normal user HOSTNAME: Where command is allowed to run. It is the hostname of the system where this rule applies. sudo is designed so you can use one sudoers file on all of your systems. This space allows you to set per-host rules. COMMAND: A simple filename allows the user to run the command with any arguments he/she wishes. However, you may also specify command line arguments (including wildcards). Alternately, you can specify “” to indicate that the command may only be run without command line arguments.
  • Allow a user to sudo, sudo allows a user to temporarily take on the role as root. — #visudo Find the line: root ALL=(ALL) ALL Under this line, add: {username} ALL=(ALL) ALL