Linux 13 - Users, Environment and Permissions in Linux

By Sheldon L Published at 2020-04-29 Updated at 2020-04-29


Accounts, Users, and Groups

Identifying Users

# Identifying the Current User
whoami
who
who -a
id
id username

User Startup Files

~/.bash_profile
~/.bash_login
~/.profile

Basics of Users and Groups

Adding and Removing Users

# Add user in Debian, see /etc/default/useradd
sudo useradd -m -c "[Full Name]" -s /bin/bash [username]
# m: make home directory
# c: Full Name
# s: shell
sudo passwd [username]
grep [username] /etc/passwd /etc/group
ssh [username]@localhost
ls -al    # everything here come from /etc/skel
ls -al /etc/skel

# Add user in SUSE
sudo /usr/sbin/useradd [username]

# This, by default, sets the home directory to:
# /home/[username]
# populates it with some basic files, copied from:
# /etc/skel
# and adds a line to:
# /etc/passwd
# such as:
# [username]:x:1002:1002::/home/[username]:/bin/bashc
# and sets the default shell to:
# /bin/bash

# Delete user
sudo userdel [username]     # will leave /home/[username]
sudo userdel -r [username]  # will remove recursively

Adding and Removing Groups

# Add group
sudo /usr/sbin/groupadd [groupname]
# OR
addgroup [group]

# Delete group
sudo /usr/sbin/groupdel [groupname]
# OR
delgroup [group]

# Check the groups a user belongs to
groups [username]

# Add a user to a group
sudo /usr/sbin/usermod -a -G [groupname] [username]  
# a: append, don't remove anything already exist
# G: giv a complete list of groups
# OR
adduser [user] [group] # conf file in /etc/adduser.conf
# WARNING:
sudo /usr/sbin/usermod -G anewgroup [username] # user will only belong to 'anewgroup'

# Add a user to sudo
sudo /usr/sbin/usermod -a -G sudo [username]
# OR
touch /etc/sudoers.d/[username]
echo "newuser      ALL=(ALL)     ALL" >> /etc/sudoers.d/[username]

The root Account

su                # login root, Dangerous!
sudo [command]    # every time command is complete will return to normal unprivileged user

su {username}
su - {username}
su -l {username}
su --login {username}

exit
getent passwd [user]   # return the information from the user database

chfn [user]            # CHange Full Name
chsh [user]            # CHange SHell among /etc/shells
chage [user]           # CHange passwd AGE
chage -l [user]        # list passwd age
passwd -e [user]       # forces the user to change their password
passwd -<l|u> [user]   # lock/unlock

groupmod -g [newgid]   # change group config
gpasswd [group]        # change
gpasswd -r [group]     # remove

Environment Variables

# View the values of currently set environment variables
set
env
export

# Create a variable only available in current shell
var="My Var"
echo "$$"
echo $var   # "My Var"
# new a bash and check:
bash
echo "$$"
echo $var   # Nonthing
exit

# Export a new variable available in current shell and its sub shell
export VARIABLE=value
# OR
VARIABLE=value
export VARIABLE

export all_proxy=socks5:127.0.0.1:1080

export address=10.11.1.120
ping -c 2 $address

# Add a variable permanently
vim ~/.bashrc
export VARIABLE=value
. ~/.bashrc   # = `sourc ~/.bashrc`

# Set environment variables to be fed as a one shot to a command
SDIRS=s_0* KROOT=/lib/modules/$(uname -r)/build make modules_install
# feeds the values of the SDIRS and KROOT environment variables to `make modules_install`
echo "echo HELLO, this is the phony ls program." > /tmp/ls
cat /tmp/ls
chmod +x /tmp/ls
chmod u+x /temp/ls

bash
export PATH=$PATH:/tmp
echo $PATH
ls  # which ls run?

bash
export PATH=/tmp:$PATH  # DANGEROUSE!
echo $PATH
ls  # which ls run?

# NOTE: the second form is a very dangerous thing to do!
# is a trivial way to insert a Trojan Horse program;
# if someone can put a malicious program in /tmp, they can trick you into running it accidentally.

Common Variables Preset

echo $SHELL # The SHELL Variable, user's default command shell

echo $PATH  # The PATH Variable, an ordered list of directories (the path) which is scanned when a command is given
export PATH=$HOME/bin:$PATH  
# Each directory in the path is separated by `:`
# A null (`::` or empty befor the first `:`) directory
# or `./` indicates the current directory at any given time.

echo $HOME  # The HOME Variable, = `~`
echo $PWD   # The PWD Variable, = `pwd`
echo $USER  # The USER Variable
echo "$$"   # process ID

The PS1 Variable and the Command Line Prompt

# always reminded of who you are and what machine you are on.
echo $PS1
OLD_PS1=$PS1

PS1="\u@\h:\w$ "
echo $PS1

PS1="$ "
echo $PS1

PS1=$OLD_PS1
echo $PS1

Recalling Previous Commands

[command1]
[command2]
[command3]
history
history | tail -20
![n]      # case [n] in history
!!        # repeat last command line
!$        # repeat last command word
![string] # the most recent command starting with [string]
CTRL+R  # search previously used commands

echo $HISTFILE      # location of the history file
echo $HISTSIZE      # maximum number of lines in the history file (default 500)
echo $HISTFILESIZE  # maximum number of commands in the history file
echo $HISTCONTROL   # how commands are stored
echo $HISTIGNORE    # command lines can be unsaved
# see help in:
man bash

# # Search in history
# J: scroll backwards
# L: scroll forwards

Permissions

# Opt1. Symbolic representation
chmod <-R> <a|u|g|o><-|+|=><w|r|x|X> [file] # -R = Recursive, a = ugo, X = applies only to directories
chmod -R a+X [directory]
ch

# Opt2. The (octal) numeric representation
chmod 467 [file]
# The most frequent right combinations are 755 for executable files and directories, and 644 for data files.
chmod 4467 [file]  # add bit 4, Granting Temporary Root User’s Permissions with `setuid`
chmod 2467 [file]  # add bit 2, Granting Temporary Root User’s Permissions with `setgid`
chmod 1467 [file]  # add bit 1, Granting Temporary Root User’s Permissions with `sticky`
# The use of octal notation only allows you to set all the rights at once on a file
umask # will see a mask such as 0022, used to restrict permissions on newly created files
chown [user]:[group] [file]
chgrp [group] [file]
chmod [rights] [file]