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 | sudo vim /etc/profile |
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 | vim ~/.bashrc |
Permanent & Temporary Vars
Permanent: Add vars in system files
Temp: Use
export
in bash
e.g.
1 | # Set new var |
Related Commands
env
: Check all environmental varsset
: Check all vars in the current shellunset <VAR>
: delete varsvimdiff
: Compare differencese.g.
./bashrc
: Make changes effective immediately
Common Vars
PATH
HOME
: User’s main directoryHISTSIZE
: Number of stored commandsLOGNAME
: Current usernameHOSTNAME
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>
orsudo 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 withuseradd
)
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 fileid <NAME>
: User infow
: Current userlastlog
: login infoSwitch 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>