Merikanto

一簫一劍平生意,負盡狂名十五年

Linux Shell Commands - 02 Env Vars & Users

This post is Part Two of the summary of some common Linux Shell Commands.



Environment Variables

The Path var determines where shell finds those commands, or processes.

The vars point to directories, and processes find related files based on the assigned directories.


System Vars

Two paths:

  • /etc/profile: First to execute when log into shell

  • /etc/environment: System environment var. assignment

e.g. 两种写法:

1
sudo sh -c 'echo "export <VAR>=<path>" >> /etc/profile'
1
2
sudo vim /etc/profile
export <VAR>='<path 1>:<path 2>...'

User Vars

Two paths:

  • ~/.profile: Execute once each time the user logs in

  • ~/.bashrc: Every time when a new shell window is opened

e.g. 两种写法:

1
sudo sh -c 'echo "export <VAR>=<path>" >> .bashrc'
1
2
vim ~/.bashrc
export <VAR>='<path 1>:<path 2>...'

Permanent & Temporary Vars

  • Permanent: Add vars in system files

  • Temp: Use export in bash

e.g.

1
2
3
4
5
# Set new var
export NAME='hello'

# Change existing var
NAME='world'

  • env: Check all environmental vars

  • set: Check all vars in the current shell

  • unset <VAR>: delete vars

  • vimdiff: Compare differences

  • e.g. ./bashrc: Make changes effective immediately


Common Vars

  • PATH

  • HOME: User’s main directory

  • HISTSIZE: Number of stored commands

  • LOGNAME: Current username

  • HOSTNAME

  • SHELL

  • PS1: Prompt String 1. # for root, $ for normal users



Users & Usergroups


Userinfo & UID

1
sudo cat /etc/passwd

e.g. root : x : 0 : 0 : root : /root : /bin/bash

7 parts in total.

Username : Password : User ID (UID) : Group ID (GID) : Comments : Main dir. : Shell


Password Info

1
sudo cat /etc/shadow

root : * : 17590 : 0 : 99999 : 7 : : :

9 parts in total.

Username : Password : Last change : Min : Max : Warn : Inactive time : Expire time : (Reserved)

  • Last change: Last password change

  • Min / Max: Minimum / Maximum days for the password to be effective

  • Warn: Warnings before password expires


Usergroup Info:

1
sudo cat /etc/group

root : x : 0 :

4 parts in total.

Group name : Password: GID : List of users (1, 2, …)


Manage Users & Usergroups:

Create Users:

  • Methods: adduser <NAME> or sudo useradd

  • Set home directory: useradd -d /home/<USER> <NAME>

  • Set usergroup: useradd -g <GROUP> <NAME>

  • Set login shell: useradd -s /bin/bash <NAME>

  • Modify existing user info: usermod (same options with useradd)


sudo passwd:

  • Modify password: passwd <NAME>

  • Delete password: passwd -d <NAME>

  • Block user from logging in: passwd -l <NAME>

  • Unlock: passwd -u <NAME>

  • Password status: passwd -S <NAME>


Check Status:

  • sudo tail -5 /etc/passwd: System file

  • id <NAME>: User info

  • w: Current user

  • lastlog: login info

  • Switch user: su <NAME>

  • Check user’s all groups: groups <NAME>


Usergroup:

  • Add user to multiple groups: usermod -aG <Group1, Group2, ...> <NAME> (a: append)

  • Add new GID: e.g. groupadd -g 1024 <GROUP>

  • Modify group name: groupdmod -n <NEW> <OLD>