User
Add user
  • sudo adduser userName, add a single user and create the home directory with default bash
  • sudo useradd -d homeDirectory -u userID -g groupID -s loginShell
    #!/bin/bash
    # add a single user
    
    # read username and password
    read -p "Enter user name: " username
    
    # add the user and create the home directory
    useradd -m $username -s /bin/bash
    
    # create password for the user
    passwd $username
    
    # switch to the created user
    # copy a .bashrc template to the home directory of the created user
    sudo -u $username cp -f /home/lchen/.bashrc /home/$username
    			
    #!/bin/bash
    # add multiple users
    
    for i in `more namelist.txt`
    do
            useradd -m $i -s /bin/bash
            echo -e "$i\n$i" | passwd $i
    done
    			
  • Switch to a user
  • whoami, check the current username
  • sudo -u userName bash, switch to a specific user
  • sudo -u userName command; command; carry out commands with permission of a specific user
  • Display users
  • cat /etc/passwd, username, password, user ID, group ID, user info, home directory, bash shell
  • Remove user
  • sudo userdel -r userName, remove a user and the home directory
  • #!/bin/bash
    # remove multiple users
    
    for i in `more namelist.txt`
    do
            userdel -r $i
    done
    			
    Add a user to sudo
  • sudo adduser userName sudo
  • Reference