Current user

Using a built-in variable:

$ echo $USER
mcurrin

Using a command:

$ whoami
mcurrin

Current user’s groups

From thread.

$ id -g
1000
$ id -g -n
michael

All groups:

$ groups
michael sudo docker
$ # OR
$ id -G -n
michael sudo docker

Home directory

Using a built-in variable:

$ echo $HOME
/Users/mcurrin

Using a tilde:

$ echo ~
/Users/mcurrin

Literal if inside quotes:

$ echo "~"
~

Switch user

Change to a target user - enter their password.

$ su some-user

Or using sudo if you are in the sudoers file:

$ sudo su some-user

If you leave out the username, then you will change to root.

$ sudo su

Note this is dangerous as every command you will run as root and you could run a command accidentally without being asked for your password.

Create user with sudo access

  1. Create user.
     $ sudo adduser newuser
    
  2. Add user to sudo group. The -aG option tells the system to append the user to the specified group. The -a option is only used with G.
     $ sudo usermod -aG sudo newuser
    
  3. Verify group.
     $ groups newuser
    
  4. Verify sudo access - become the user and then try a sudo command.
     $ su - newuser
     $ sudo ls /root