目录
0 前言
1 sudo and su
1.1 su: 切换用户
1.2 sudo: 切换用户
2 权限设置:chmod, chown
2.1 chmod:访问权限设置
2.2 chown:设置文件或目录的主人
2.3 chgrp:设计文件或者目录属于哪个组
2.4 查询组成员
2.5 查询某用户属于哪个(哪些)组
3 磁盘空间状况查询:df, du
3.1 df
3.2 du (Disk Usage)
4 进程状态查询管理:ps, top, kill, job
4.1 ps
4.2 top
4.3 kill command
4.5 jobs command
6 用户加入和删除:useradd, userdel
7 修改密码
10 查询系统版本信息
11 安装软件
12 系统状态查看管理
13 group管理
13.1 创建一个组
13.2 删除一个组
13.3 追加用户到组
13.4 从组中删除用户
13.5 改变用户的primary group
13.6 创建一个新用户并同时追加到用户组中
13.7 显示用户的组信息
14 系统信息文件
0 前言
日常使用中的点点滴滴的积累。不求系统全面,但求简洁实用。
1 sudo and su
sudo: short for “SuperUser Do”, this command enables you to perform tasks that require administrative or root permissions. However, it is not advisable to use this command for daily use because it might be easy for an error to occur if you did something wrong.
1.1 su: 切换用户
su username # 切换到指定用户名,当然会要求输入密码
su # 不带参数的话,表示要求切换为root user
如果你只需要临时以其它用户名执行一条命令的话,可以不用切换过去,而是以以下方式执行,'command'表示要执行的命令:
su -c command username
例如:
su -c chmod +w /Downloads testuser
su当然还有其它种种选项,这里不再赘述。
1.2 sudo: 切换用户
sudo与su的作用大同小异,差异在于选项的不同,以下为一些典型的使用方法:
sudo -u username
sudo -u username command
sudo -u testuser chmod 777 /Documents
sudo
2 权限设置:chmod, chown
2.1 chmod:访问权限设置
chmod is used to change the read, write, and execute permissions of files and directories. As this command is rather complicated, you can read the full tutorial in order to execute it properly.
加上“-R”可以递归地改变一个目录底下所有文件或目录对象的访问权限:
Example:
chmod -R 777 /Path/To/Folder
2.2 chown:设置文件或目录的主人
In Linux, all files are owned by a specific user. The chown command enables you to change or transfer the ownership of a file to the specified username. For instance, chown linuxuser2 file.ext will make linuxuser2 as the owner of the file.ext.
同样可以使用“-R”选项进行递归式的设置。
2.3 chgrp:设计文件或者目录属于哪个组
chgrp groupname file/folder
同样可以使用“-R”选项进行递归式的设置。
2.4 查询组成员
getent group groupname
2.5 查询某用户属于哪个(哪些)组
groups username
3 磁盘空间状况查询:df, du
3.1 df
Use df command to get a report on the system’s disk space usage, shown in percentage and KBs. If you want to see the report in megabytes, type df -m.
由于是关于系统盘的信息,跟目录无关。不需要退到根目录,在任何地方执行都可(都一样)。
有意思的是“-h”选项和“-H”选项的区别。
其差异是,“-h”的1K是指1024,而“-H”的1K就是1000,所以两种选项得到的大小的显示值是不一样的,如下例所示:
3.2 du (Disk Usage)
常见用法:
du -h --max-depth=k
du -h -d k
du -sh /path/to/folder # s: summary, 报告指定目录总的size
-h是指生成human-readable格式的输出
"-d k" 或者“--max-depth=k”是指生成几级深度的信息,0表示仅生成当前目录的总的空间大小,1表示同时回到第1级子目录的信息,以下依此类推。。。当然如果不用该参数的话会缺省把所有层次信息都给出来,这样反而会很难看。
注意:是max-depth而不是max_depth!
查询当前目录下总的磁盘空间使用量(“.”表示当前目录):
du -sh .
查询当前目录下各文件或者子目录的磁盘空间使用量:
du -sh *
4 进程状态查询管理:ps, top, kill, job
4.1 ps
最常用的使用方式,列出用户自己的进程:
ps -aux
注意以上所显示的PID号。kill命令即是基于PID来进行进程消杀。
再加上管道连接grep命令即可查看指定用户运行的进程
ps aux | grep -v `whoami`
4.2 top
As a terminal equivalent to Task Manager in Windows, the top command will display a list of running processes and how much CPU each process uses. It’s very useful to monitor system resource usage, especially knowing which process needs to be terminated because it consumes too many resources.
4.3 kill command
结合ps命令使用,参见上面ps命令的说明。
If you have an unresponsive program, you can terminate it manually by using the kill command. It will send a certain signal to the misbehaving app and instructs the app to terminate itself.
There is a total of sixty-four signals that you can use, but people usually only use two signals:
SIGTERM (15) — requests a program to stop running and gives it some time to save all of its progress. If you don’t specify the signal when entering the kill command, this signal will be used.
SIGKILL (9) — forces programs to stop immediately. Unsaved progress will be lost.
Besides knowing the signals, you also need to know the process identification number (PID) of the program you want to kill. If you don’t know the PID, simply run the command ps ux.
After knowing what signal you want to use and the PID of the program, enter the following syntax:
kill [signal option] PID
4.5 jobs command
jobs command will display all current jobs along with their statuses. A job is basically a process that is started by the shell.
6 用户加入和删除:useradd, userdel
ref: How to Create Users in Linux (useradd Command) | Linuxize
Since Linux is a multi-user system, this means more than one person can interact with the same system at the same time. useradd is used to create a new user, while passwd is adding a password to that user’s account.
追加一个名为John的用户账号(当然需要管理员权限。如果你有sudo权限,则可以在一下命令之前加上sudo ):
useradd John
然后再为他设置密码则使用passwd命令(如果是自己修改密码则不需要加用户名):
passwd John
You will be prompted to enter and confirm the password. Make sure you use a strong password.
Changing password for user username.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
To remove a user is very similar to adding a new user. To delete the users account type:
userdel UserName
7 修改密码
#passwd 修改密码,再次确认,区分大小写
#whoami 就是一条查看自己用户名的命令
#users/who/w 就是查看当前在线用户
SYntax: passwd [-k] [-l] [-u [-f]] [-d] [-S] [username]
Parameter:
-d 删除密码
-f 强迫用户下次登录时必须修改口令
-w 口令要到期提前警告的天数
-k 更新只能发送在过期之后
-l 停止账号使用
-S 显示密码信息
-u 启用已被停止的账户
-x 指定口令最长存活期
-g 修改群组密码
指定口令最短存活期
-i 口令过期后多少天停用账户
--help 显示帮助信息
--version 显示版本信息
显示账号密码信息
# passwd -S john
john PS 2020-07-02 0 99999 7 -1 (Password set, MD5 crypt.)
删除用户密码
# passwd -d lx138
passwd: password expiry information changed.
10 查询系统版本信息
uname -a
输出信息如以下例所示:
Linux server8 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
lsb_release -a
输出信息如以下例所示:
LSB Version: :core-4.1-amd64:core-4.1-ia32:core-4.1-noarch
Distributor ID: n/a
Description: NAME="Red Hat Enterprise Linux Server"
Release: n/a
Codename: n/a
11 安装软件
安装软件需要系统管理员权限。在linux不同发行版中软件安装命令也不同。
redhat中用的是yum命令。安装使用示例参见。。。
12 系统状态查看管理
12.1 uptime
12.2 w command
12.3 users command
12.4 who command
12.5 whoami
12.6 crontab
12.6 ssh (secure shell)
12.7 systemctl
12.8 free
12.9 last
13 group管理
Linux groups are organization units that are used to organize and administer user accounts in Linux. The primary purpose of groups is to define a set of privileges such as reading, writing, or executing permission for a given resource that can be shared among the users within the group.
There are two types of groups in Linux operating systems:
The Primary group – When a user creates a file, the file’s group is set to the user’s primary group. Usually, the name of the group is the same as the name of the user. The information about the user’s primary group is stored in the /etc/passwd file.
Secondary or supplementary group - Useful when you want to grant certain file permissions to a set of users who are members of the group. For example, if you add a specific user to the docker group, the user will inherit the group’s access rights and be able to run docker commands.
Each user can belong to exactly one primary group and zero or more secondary groups.
Only root or users with sudo access can add a user to a group.
13.1 创建一个组
To create a new group , use the groupadd command followed by the group name:
sudo groupadd groupname
13.2 删除一个组
To delete an existing group, use the groupdel command followed by the group name:
sudo groupdel groupname
13.3 追加用户到组
sudo usermod -a -G groupname username
Always use the -a (append) option when adding a user to a new group. If you omit the -a option, the user will be removed from any groups not listed after the -G option.
On success, the usermod command does not display any output. It warns you only if the user or group doesn’t exist.
可以一条命令将一个用户同时追加到多个组中,如下所示:
sudo usermod -a -G group1,group2 username
13.4 从组中删除用户
可以用gpasswd带“-d”选项来将一个用户从一个组中删除,如下所示:
sudo gpasswd -d username groupname
13.5 改变用户的primary group
To change a user primary group, use the usermod command followed by the -g option:
sudo usermod -g groupname username
13.6 创建一个新用户并同时追加到用户组中
The following useradd command creates a new user named new_user with primary group group1 and secondary groups group2 and group3.
sudo useradd -g group1 -G group2, group3 new_user
13.7 显示用户的组信息
可以用id命令显示一个用户的全部信息:
id username
如果id命令不带参数的话,会显示当前登录的所有用户的信息。
如果只是显示某用户的组信息,可以用groups命令,比如:
groups chenxy
如果groups命令不指定用户参数,则会显示当前登录所有用户的组信息。
14 系统信息文件
/etc/passwd | User account information. |
/etc/shadow | Secure user account information. |
/etc/group | Group account information. |
/etc/gshadow | Secure group account information. |
/etc/default/useradd | Default values for account creation. |
/etc/skel | Directory containing default files. |
/etc/login.defs | Shadow password suite configuration. |
ref1: 30 Useful Linux Commands for System Administrators (tecmint.com)