This article is Part 4 in a series of cheat-sheets on the command line shell, bash. (Previous Page | Next Page)
See list of users:
$ cat /etc/passwd
Opens a new shell as user username:
$ su username
Opens a new shell as user username, using their environment:
$ su - username
Create new user username:
$ useradd username
Some options include:
-c
(Specify comment for account)-k
(Specify alternative skeleton directory)-M
(Don't create a home dir for user)-r
(Create user as system account)-s
(Specify alternative shell for user)More examples:
$ useradd -Ms /bin/bash -c Test Comment username
(Multiflag syntax)$ useradd -D
(View default values for new users)$ useradd -D -s /sbin/nologin
(Change default shell for new users)$ userdel username
Deletes user account.
-r
(Deletes user's home and mail directories as well)$ passwd username
Changes user's password. Some options include:
-e
(Expires password and forces user to make new one at next login)-S
(Outputs the status of the password for a given account)–stdin
(Pull the new password from standard input)$ usermod -flags username
Used for modifying accounts. Some options below:
-l
(Changes username)-c
(Changes comment)-L
(Locks account)-U
(Unlocks account)-g
(Primary group)-G
(Secondary group)Example, to change a user's shell:
$ usermod -s /sbin/nologin username
See list of groups:
$ cat /etc/group
Create new group newgroupname:
$ groupadd newgroupname
Change name of group oldgroupname to newgroupname:
$ groupmod -n newgroupname oldgroupname
Remove user username from group oldgroupname:
$ gpasswd -d username oldgroupname
Change user's primary group:
$ usermod -g newgroupname username
Add user to a supplementary group:
$ usermod -aG newgroupname username
Set default permissions to 600 on new files and 700 on new directories:
$ umask 0077
$ chmod 600 filename
Changes permissions of filename to 600. Further uses of chmod:
$ chmod a+r filename
(Adds read permissions for everyone.)$ chmod u-x,g-wx,o-rwx filename
(Removes read permissions from others, write permissions from group and others, and execute permissions for everyone.)$ chmod u+rwx,g+rx filename
(Adds read and execute permissions for owner and group, and write permissions for owner.)$ chmod -R u+X directoryname/
(Recursively adds execute permissions for owner if appropriate, such as a directory, but ignores if inappropriate, such as most files.)$ chmod u+s,g+s filename
(Sets the SUID and SGID execution bits.)Change owner of filename:
$ chown user filename
Change owner and group of filename:
$ chown user:group filename
Change group of filename:
$ chgrp group filename
-R
(recursive) flag can be used on chown
, chgrp
, and chmod
, to apply changes to all subdirectories and files on the folder specified in the command.
This article is part of a series on Command Line | |
Linux, MacOS & BSD | |
---|---|
Shells: | Bash (Getting Started - Shortcuts & Piping - Managing Processes - Users & Permissions - Files & Archives - Customization) - zsh |
Emulators/Multiplexers: | tmux |
Windows | |
PowerShell |