Linux
Linux 命令
Linux 的知识是非常多的,一篇文章肯定是不够的。 这里只列出常用的命令,更多命令可以结合使用需求及公司场景进行学习和使用。
基础命令
ls: 列出目录及文件名
-l
长信息,可以查看权限等信息-h
人性化显示文件大小(可以结合上一条使用)-a
显示所有文件,包括隐藏文件-F
区分文件和目录,目录末尾加'/',链接文件末尾加'@'-R
递归显示-r
反序显示-t
按时间排序-S
按文件大小排序-d
显示目录属性而非目录内容-i
显示文件的 inode 号--color=auto
颜色显示cd: 切换目录
-
: 返回上一次所在的目录pwd: 显示目前的目录
touch: 创建文件
mkdir: 创建一个新的目录
-p
可以创建多级目录-m
可以指定权限rmdir: 删除一个空的目录
cp: 复制文件或目录
-r
递归复制(复制目录时推荐加上)-a
相当于-dpR
,保留文件属性,递归复制mv: 移动文件与目录,或修改文件与目录的名称
rm: 删除文件
-r
可以删除目录rmdir: 删除空目录chmod -R 775 filename: 更改文件或者目录权限为 775
-R
表示递归,可以同时修改文件夹及子文件夹和文件chwon -R USER:GROUP filename: 更改文件或者目录的所有者和所属组
-R
表示递归,可以同时修改文件夹及子文件夹和文件chattr: 修改文件属性
+/-/=
添加/删除/设定属性(忽略当前属性)+i
设置文件为只读+a
设置文件只能追加内容curl: 用于传输数据
-I
只显示响应头-L
跟踪重定向-o
将下载的内容保存到文件-O
将下载的内容保存到文件,文件名为 URL 的最后部分-s
静默模式-v
显示详细信息ln: 创建链接文件(类似于 Windows 的快捷方式)
-s
创建软链接(符号链接)-n
如果目标文件已存在,不覆盖(避免和 source 文件冲突用)-f
强制创建env: 显示环境变量
文件相关
cat filename: 查看文件内容
less filename: 逐页查看文件内容(空格翻页,支持额外操作)
more filename: 逐页查看文件内容(空格翻页)
head filename: 查看文件头部内容
tail filename: 查看文件尾部内容
wc filename: 统计文件的行数、字数、字符数
-l
只显示行数-w
只显示字数-c
只显示字符数grep: 查找文件内容
grep [选项] [搜索内容] [文件名]
-i
忽略大小写-n
显示行号-v
反向选择-r
递归查找du: 查看文件大小
-h
人性化显示文件大小-s
只显示总大小-a
显示所有文件大小-c
显示总大小find: 查找文件
find [搜索范围] [搜索条件] [操作]
如: /etc -name "passwd" 查找 /etc 目录下的 passwd 文件
/
表示搜索全部目录.
表示搜索当前目录-name
指定文件名(支持通配符*
)-iname
忽略大小写-type
指定类型(f: 文件, d: 目录, l: 链接文件)-user
指定所有者-group
指定所属组-size
指定文件大小-mtime
指定修改时间-exec
执行操作-print
显示搜索结果sort: 排序文件内容
sort [选项] [文件名]
-r
逆序-h
人性化排序-n
按数字排序-k
指定列排序
用户相关
id: 查看用户 ID 和所属组 ID
-u
查看用户 ID,后面可以指定用户名-g
查看所属组 ID,后面可以指定用户名who: 查看当前登录用户
-u
显示详细信息whoami: 查看当前用户
sudo -i: 提升用户权限
su: 切换用户
-l
切换到 root 用户-s
指定 shellgroupadd: 添加组
-g
指定组 IDuseradd: 添加用户
-g
指定用户所属组-G
指定用户所属附加组-m
创建用户的家目录-e
设置用户的过期时间userdel: 删除用户
-r
删除用户的家目录usermod: 修改用户
-l
修改用户名-g
修改用户所属组-G
修改用户所属附加组-d
修改用户家目录-s
修改用户 shell-e
修改用户过期时间passwd: 修改密码
getent: 获取用户信息
passwd
获取用户密码信息group
获取组信息shadow
获取用户密码信息users: 查看当前登录系统的用户
系统运行状态
- 查看系统信息:
hostnamectl
: 显示系统的基本信息,包括主机名、操作系统、内核版本等。🌟🌟uname -a
: 显示当前系统的内核版本和其他信息。hostname
: 显示主机名。uptime
: 显示系统的运行时间、用户数量、负载平均值等。df -h
: 显示磁盘空间使用情况。
- 查看 CPU 运行情况:
- 查看进程情况:
ps
: 显示当前系统中的进程列表。ps aux
: 显示所有进程的详细信息。🌟🌟ps -ef
: 显示所有进程的详细信息,包括父进程 ID。
pstree
: 显示进程树,以树状结构展示进程之间的关系。pgrep
: 根据进程名或者其他条件查找进程 ID。kill
: 终止指定进程。🌟🌟killall
: 终止指定名称的所有进程。
- 查看内存使用情况:
free -h
: 显示系统内存的空闲和已用情况。🌟🌟vmstat
: 显示虚拟内存统计信息,包括内存、进程、IO 等。
- 查看网络情况:
netstat
: 显示网络连接、路由表、接口统计等。🌟🌟ss
: 类似于netstat
,用于获取套接字统计信息。nslookup
: 查询 DNS 服务器,获取域名对应的 IP 地址。
crontab 定时任务
crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表。
crontab [ -u user ]: 定时任务管理
-e
编辑定时任务-l
列出定时任务-r
删除定时任务-u
指定用户(可以列出指定用户的定时任务)-u user
是指设定指定 user 的时程表,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话,就是表示设定自己的时程表。MIN HOUR DOM MON DOW [USER] CMD: 分别表示分钟、小时、日期、月份、星期,
*
表示任意时间。bash# ┌───────────── 分鐘 (0 - 59) # │ ┌─────────── 小時 (0 - 23) # │ │ ┌───────── 日 (1 - 31) # │ │ │ ┌─────── 月 (1 - 12) # │ │ │ │ ┌───── 星期幾 (0 - 7,0 是週日,6 是週六,7 也是週日) # │ │ │ │ │ # * * * * * /path/to/command --your --parameter
特殊规则
通常以@
开头,以下是特殊规则及说明:
排程規則 | 說明 |
---|---|
@reboot | 每次重新開機之後,執行一次。 |
@yearly | 每年執行一次,亦即 0 0 1 1 * 。 |
@annually | 每年執行一次,亦即 0 0 1 1 * 。 |
@monthly | 每月執行一次,亦即 0 0 1 * * 。 |
@weekly | 每週執行一次,亦即 0 0 * * 0 。 |
@daily | 每天執行一次,亦即 0 0 * * * 。 |
@hourly | 每小時執行一次,亦即 0 * * * * 。 |
例如每天執行一次,就可以這樣寫:
# 每天執行一次
@daily /home/user/script.sh --your --parameter
防火墙及端口
firewall-cmd: 防火墙管理工具
--zone=public
: 指定防火墙区域--add-port=80/tcp
: 添加端口--permanent
: 永久生效--reload
: 重新加载防火墙配置--list-ports
: 列出所有开放的端口
解压缩操作
tar: 打包文件
-c
创建新的归档文件-x
解开归档文件-v
显示详细信息-f
指定归档文件名-z
使用 gzip 压缩-j
使用 bzip2 压缩-J
使用 xz 压缩-C
指定解压缩目录(解压到指定目录)-r
向归档文件中添加文件-t
显示归档文件中的内容zip: 压缩文件
-v
显示详细信息-r
递归压缩-m
移动文件到 zip 文件中-q
静默模式-o
覆盖已有文件-u
更新已有文件-d
删除 zip 文件中的文件-l
显示文件列表-T
测试 zip 文件-x
排除文件unzip: 解开压缩文件
基础知识
你应该首先了解的
1、文件名如果带空格,则需要通过双引号将文件名引起来(自己创建文件或目录时不推荐空格)。
例如: rm “test case.py”
2、.
表示当前目录,..
表示上一级目录,/
开头的路径表示根目录,~
为用户目录。
3、你可以使用 man [命令] 来查看各个命令的使用文档,
如 : man cp
如果你不是很确定命令的具体名称,可以使用 apropos [关键字] 来搜索相关的命令。 也支持参数 -k
来寻找相关性高的命令。
文件目录
/etc: 系统配置文件
通常包括系统的配置文件和程序的配置文件
/var: 系统运行时需要改变的文件
通常包括日志文件、缓存文件、软件包文件等
常用的文件
配置文件
/etc/hosts: 主机名和 IP 地址映射文件
/etc/reslov.conf: DNS 配置文件
/etc/sysconfig/network-scripts/ifcfg-eth0: 网络配置文件
/etc/cron.allow[/deny]: 允许/拒绝用户使用 crontab 命令(每行一个账号名称)
敏感文件
/etc/passwd: 用户信息文件
/etc/group: 组信息文件
/etc/shadow: 用户密码文件
/etc/sudoers: sudo 配置文件,用于查看哪些用户可以使用 sudo 命令
/etc/crontab[cron.d]: 定时任务配置文件
/etc/ssh/sshd_config: SSH 配置文件
日志文件
/var/log/secure: 安全日志文件,如 SSH 登录日志
/var/log/syslog: 系统日志文件
/var/log/messages: 系统消息日志文件
/var/log/kern.log: 内核日志文件,如硬件故障日志
/var/log/maillog: 邮件日志文件
/var/log/cron: 计划任务日志文件
/var/log/boot.log: 启动日志文件
/var/log/access.log: Web 访问日志文件,如 Nginx 访问日志
/var/log/error.log: Web 错误日志文件
/root[~]/.bash_history: 用户历史命令记录文件,记录用户执行过的命令
Bash shell 中的变量
在 Bash shell 中,变量是用来存储数据值的命名内存位置。这些变量可以存储各种类型的数据,如数字、文本字符串、文件名等。在 Bash 中,变量通常用于存储配置信息、运行脚本时的参数、临时数据等。
环境变量
Bash shell 包含一些预定义的环境变量,用于存储系统和用户环境的信息。以下是一些常用的环境变量:
$HOME
: 当前用户的主目录路径。$PATH
: 执行命令的搜索路径。当你输入一个命令时,系统会在这些路径下搜索可执行文件。$PWD
: 当前工作目录的路径。$USER
: 当前用户名。$SHELL
: 当前使用的 shell 的路径。$TERM
: 当前终端的类型。$LANG
: 当前语言环境设置。$OSTYPE
: 当前操作系统的类型。
环境变量可以用env
命令查看。
env | less
定义变量
VARIABLE_NAME=value
这样设置的变量只在当前 shell 中有效,如果想要在其他 shell 中也生效,可以使用export
命令。
export VARIABLE_NAME=value
如果你希望永远生效,可以将变量写入配置文件中,如~/.bashrc
。
特殊变量
Bash shell 还提供了一些特殊的内置变量,例如:
$0
: 当前脚本的名称。$1
、$2
、$3
等: 表示传递给脚本或函数的参数。$#
: 表示传递给脚本或函数的参数的个数。$@
: 表示传递给脚本或函数的所有参数的列表。$?
: 表示上一个命令的退出状态。
设置 path
当您键入命令时,您的 PATH 变量定义 shell 将在哪些目录中查找您键入的命令。 它是一个以冒号分隔的目录列表,在 SPECTRE 上可能会很长,反映了您可能已加载的应用程序和程序模块的数量。
echo $PATH
将会显示你的 PATH 变量的内容。
你可以继续在已经存在的 path 变量后添加新的:
可以在一行上添加多个,只需要用“:”进行分隔开。
export PATH=$PATH:~/units174/bin
如果要永久生效,在.bashrc
后面添加:
PATH=$PATH:~/units174/bin
which
which 命令可以显示命令的完整路径(前提是该文件位于该路径中)。
which command
which wget
如果有多个同名的应用,可以加上参数-a
来显示全部。
(同名的应用,只有列表中的第一个会生效)
Bash 的常用快捷方式
Tab: 自动补全命令或者文件/目录名称
Ctrl + C: 终止当前命令
Ctrl + D: 退出当前 shell(比如 sort 进入读键盘操作后)
Ctrl + L: 清屏(常用)
Ctrl + W: 删除光标前的一个单词
Ctrl + U: 删除光标前的内容
Ctrl + K: 删除光标后的内容
Ctrl + Y: 粘贴删除的内容
Ctrl + A: 光标移动到行首
Ctrl + E: 光标移动到行尾
Ctrl + R: 搜索历史命令
系统启动需要加载的配置文件
主要包括 bash 的配置文件,环境变量配置文件,以及脚本文件等。
修改环境变量配置后,需要用
source /PATH/TO/FILE_NAME
来重新加载配置使其生效
/root/.bash_profile
/root/.bashrc
~/.bash_profile
~/.bashrc
/etc/profile
/etc/bashrc
/etc/rc.local(/etc/rc.d/rc.local): 开机启动文件,不推荐使用,可以通过服务实现
/etc/profile.d/*.sh: 系统启动后自动执行的脚本文件夹
/etc/sysconfig/i18n
常用的网络应用目录
PHP 配置: /etc/php.ini
PHP-FPM 配置目录: /etc/php-fpm.d/
MySQL 配置: /etc/my.cnf
Nginx 配置: /etc/nginx/
Apache 配置目录: /etc/apache2/
Apache 默认网站目录: /var/www/html/(可以通过配置文件 httpd.conf 中 DocumentRoot 来修改)
‼️ 权限相关的数字及字母
如: chmod 755 filename
,其中 755 代表文件的权限,分别代表所有者、所属组、其他用户的权限,7 代表读写执行,5 代表读和执行,0 代表无权限;
也可以使用类似ug=rwx, o=rw
的方式来修改权限。
比如: chmod u=rwx, g=rw, o=rw filename
也可以用类似+x
来增加权限,
比如: chmod +x filename
下面是所有符号的含义:
+
: 添加权限-
: 删除权限=
: 设置权限r
: 读权限w
: 写权限x
: 执行权限u
: 所有者g
: 所属组o
: 其他用户a
: 所有用户
service 和 systemctl 的区别
systemctl
和 service
命令都用于管理系统服务,包括启动、停止、重启和检查服务状态等。它们的不同之处在于它们是不同的服务管理工具,适用于不同的系统。
systemctl
是 systemd 的主要命令,用于管理系统的服务和其他系统资源。systemctl
可以启动、停止、重启和重新加载系统服务,并提供了更多高级功能,如启用/禁用服务、查看服务状态和日志等。service
命令是一个简单的系统服务管理工具,用于启动、停止和重启系统服务。service
命令主要用于 SysVinit 系统,这是旧版本的 Linux 系统使用的 init 系统。
虽然在一些新的 Linux 发行版中,systemctl
取代了 service
命令成为了管理系统服务的首选工具,但在一些较旧的系统中,service
命令仍然可以使用,因为系统可能仍然使用着 SysVinit 系统。
ls -l 长目录结果代表的意思
下面是 ls -l
命令输出的示例:
-rw-r--r-- 1 user group 4096 May 1 10:00 file.txt
drwxr-xr-x 2 user group 4096 May 1 09:59 directory
现在让我们逐个解释每列的含义:
- 第一列: 文件权限和文件类型
- 第一个字符:
-
表示这是一个普通文件。d
表示这是一个目录。l
表示这是一个符号链接。c
表示这是一个字符设备文件。b
表示这是一个块设备文件。s
表示这是一个套接字文件。p
表示这是一个命名管道。
- 后续的九个字符(每三个一组)表示文件的权限,分别代表了文件所有者、文件所属组和其他用户的读、写和执行权限。
- 第一个字符:
- 第二列: 硬链接数
- 表示指向此文件或目录的硬链接的数量。
- 第三列: 文件所有者
- 表示此文件或目录的所有者。
- 第四列: 文件所属组
- 表示此文件或目录所属的组。
- 第五列: 文件大小(以字节为单位)
- 对于目录来说,这个字段一般没有实际意义,因为目录大小并不代表其中包含的文件大小总和。
- 第六列至第八列: 最后修改的日期和时间
- 表示文件或目录最后一次修改的日期和时间。
- 最后一列: 文件或目录的名称
使用案例
文件相关
查找大文件和日志
第一种方式是使用du
命令查找大文件,du
命令用于查看文件和目录的磁盘使用情况。
du -ah /path/to/directory | sort -rh | head -n 10
这条命令用于查找指定目录下最大的 10 个文件和目录。du -ah /path/to/directory
用于查看指定目录下所有文件和目录的大小,sort -rh
用于按照文件大小逆序排序,head -n 10
用于显示前 10 行。
第二种方式是使用find
命令查找大文件,find
命令用于查找文件和目录。
find /path/to/directory -type f -size +100M -print0 | xargs -0 ls -lh | awk '{ print $9 ": " $5 }'
这条命令用于查找指定目录下大于 100MB 的文件。find /path/to/directory -type f -size +100M -print0
用于查找大于 100MB 的文件,xargs -0 ls -lh
用于显示文件的详细信息,awk '{ print $9 ": " $5 }'
用于显示文件名和大小。
或者:
find /path/to/directory -type f -size +20M -print0 | xargs -0 du -h | sort -nr
这条命令用于查找指定目录下大于 20MB 的文件。find /path/to/directory -type f -size +20M -print0
用于查找大于 20MB 的文件,xargs -0 du -h
用于显示文件的大小,sort -nr
用于按照文件大小逆序排序。
还可以使用ncdu
命令查找大文件,ncdu
是一个交互式的磁盘使用情况分析工具,需要安装。
ncdu /path/to/directory
使用 cat 合并追加文件
cat file1 file2 > target_file: 将多个文件合并到目标文件中。
cat file1 file2 > target_file
cat file1 file2 >> target_file: 将几个文件附加到目标文件中。
cat file1 file2 >> target_file
快速创建多个文件或者目录
可以使用{1,2,3}
这种方式来创建多个文件或者目录
mkdir newDir{1,2,3}
如果想创建多个层级的目录,可以使用-p
参数
mkdir -p newDir/{subDir1,subDir2}
快速统计目录内文件个数
可以先列出目录下的所有文件和子目录,并且通过grep
命令筛选出文件行(以-
开头),最后使用wc -l
命令统计行数。
ls -l | grep "^-" | wc -l
less 查看文件支持更多操作
- 搜索:
- 按下
/
键后输入要搜索的内容,然后按 Enter 键。less
将高亮显示匹配的内容,并使用n
和N
键分别向前和向后跳转到下一个或上一个匹配项。
- 按下
- 跳转:
- 使用
g
命令跳转到文件的开头。 - 使用
G
命令跳转到文件的末尾。 - 使用
1G
命令跳转到文件的第一行。 - 使用
n<Enter>
命令跳转到文件的第 n 行。
- 使用
- 标记位置:
- 按下
m
键后输入一个字母(比如a
)以在当前位置标记。然后,可以使用'a
命令回到该标记位置。
- 按下
- 显示行号:
- 按下
-N
(大写 N)命令可以显示行号。
- 按下
- 执行外部命令:
- 按下
!
键后输入要执行的外部命令,然后按 Enter 键。例如,!ls
可以执行ls
命令并显示结果。
- 按下
- 切换布局:
- 按下
v
键可以切换到编辑器模式,以便在vi
或其他编辑器中编辑当前文件。
- 按下
- 退出:
- 按下
q
键退出less
。
- 按下
使用 grep 查找文本
查找文本在某个文件中
可以加-a
参数,表示以文本方式查看二进制文件
grep -a "text" filename
其中字符串内容可以使用正则表达式
grep -a "sc[0-9]*" filename
解压缩文件
一般可以用tar
命令解压缩文件,例如:
创建压缩文件
tar -czvf archive.tar.gz file1 file2 file3
# 压缩目录
tar -czvf archive.tar.gz /path/to/directory
解压缩文件
tar -xzvf archive.tar.gz -C /path/to/extract
要注意参数-z
表示使用 gzip 压缩,如果是其它格式的压缩文件,可以使用-j
(bzip2)或者-J
(xz)。
查找文件
查找文件/文件夹并进行排序
可以结合find
和sort
实现
- 通过文件名查找文件并按照名称排序
find . -name "*.service" -maxdepth 1 -type f | sort
- 查找文件并按照文件大小排序
find . -type f -exec ls -l {} + | sort -k 5n
这个命令会先使用 find
命令找到所有文件,然后通过 ls -l
命令获取它们的详细信息,并通过管道将结果传递给 sort
命令。sort -k 5n
会按照第五列(文件大小)进行数字排序。
你也可以直接在 find
命令中使用 -exec
选项和 -printf
来获取文件的大小信息,然后再排序。例如:
find . -type f -printf "%s %p\n" | sort -n
locate
它是直接从预先构建好的数据库中进行搜索,而不是像 find
命令一样实时遍历文件系统,可以实现更快搜索。
安装: yum install mlocate
updatedb
: 更新文件及目录索引数据库(第一次使用可能需要执行这条命令)
之后就可以使用locate
命令来查找文件了。
locate filename
远程复制文件到本地
可以使用scp
(secure copy) 来实现。
scp
命令是一个用于在 Linux 系统之间复制文件和目录的命令行工具。它使用 SSH 协议来加密传输数据,因此非常安全。scp
命令通常用于将文件从本地系统复制到远程系统,也可以用于在远程系统之间复制文件。
# 远程文件复制到本地
scp username@remote_host:/path/to/remote/file /path/to/local/destination
# 本地文件复制到远程服务器(指定远程服务器端口)
scp -P 22 /path/to/local/file username@remote_host:/path/to/remote/destination
还可以添加参数:
-r
递归复制-P
指定端口-p
保留文件属性-q
静默模式-C
压缩传输数据
也可以使用rsync
命令来实现。
rsync
比scp
更加强大,它可以在本地和远程系统之间同步文件和目录,支持增量传输,可以快速复制大量文件。
# 本地文件复制到远程服务器
rsync -avz -e "ssh -p 22" /path/to/local/file username@remote_host:/path/to/remote/destination
# 远程文件复制到本地
rsync -avz username@remote_host:/path/to/remote/file /path/to/local/destination
avz
分别代表着archive
、verbose
和compress
选项,它们分别用于保留文件属性、显示详细信息和压缩传输数据。-e
参数,它允许你指定一个自定义的 SSH 端口,如果是默认的 22 端口,可以省略。
默认情况下,rsync
是没有进度条的,如果想要显示进度条,可以添加--progress
参数,或者使用--info=progress2
参数。
应用管理
yum 管理工具的使用
查看所有软件列表: yum repolist all | grep mysql
查看已安装的软件列表: yum list installed
卸载应用: yum remove [package_name]
深度卸载: yum autoremove [package_name]
清除缓存: yum clean all
重新生成缓存: yum makecache
(更换源后需要执行)
apt 管理工具的使用
在 Debian 系统中,apt
是一个用于管理软件包的高级工具,它提供了一组用于搜索、安装、更新和删除软件包的命令。
以下是一些常用的 apt
命令:
apt update: 更新软件包列表,以获取最新的软件包信息。
apt upgrade: 升级所有已安装的软件包到最新版本。
apt install PACKAGE_NAME: 安装一个特定的软件包。
apt remove PACKAGE_NAME: 卸载一个特定的软件包。
apt search KEYWORD: 搜索软件包。
apt show PACKAGE_NAME: 显示软件包的详细信息。
apt list --installed: 列出所有已安装的软件包。
apt list --upgradable: 列出所有可以升级的软件包。
apt autoremove: 删除不再需要的软件包。
apt clean: 清理下载的软件包文件。
apt autoclean: 清理过期的软件包文件。
服务管理
使用 ln 命令为服务创建快捷方式
例如: ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
,则可以通过nginx
命令启动 Nginx 服务
使用 systemctl 命令管理服务
systemd 是一个 init 系统和系统管理守护进程,用于启动、停止和管理系统中的各种服务和进程。
systemctl
是一个 Linux 系统中用于管理 systemd 服务的命令行工具。
对应的服务目录: /usr/lib/systemd/system/*.service
以下是 systemctl
命令的一些常见用法和解释:
启动服务: 启动一个特定的服务。
bashsystemctl start SERVICE_NAME
停止服务: 停止一个特定的服务。
bashsystemctl stop SERVICE_NAME
重启服务: 停止并重新启动一个特定的服务。
bashsystemctl restart SERVICE_NAME
重新加载配置: 重新加载一个特定服务的配置文件,使新的配置生效,而不需要重启服务。
bashsystemctl reload SERVICE_NAME
查看服务状态: 查看特定服务的状态,包括是否正在运行。
bashsystemctl status SERVICE_NAME
启用服务: 将一个服务设置为在系统启动时自动启动。
bashsystemctl enable SERVICE_NAME
禁用服务: 将一个服务设置为在系统启动时不自动启动。
bashsystemctl disable SERVICE_NAME
查看服务是否启用: 查看一个服务是否已经设置为在系统启动时自动启动。
bashsystemctl is-enabled SERVICE_NAME
查看所有已启用的服务: 列出所有已经设置为在系统启动时自动启动的服务。
bashsystemctl list-unit-files --type=service | grep enabled
查看所有正在运行的服务: 列出当前正在运行的所有服务。
bashsystemctl list-units --type=service | grep running
以下是一些常用的 Web 服务已经它对应的服务名称
Nginx: nginx
Apache: httpd and apache2
MySQL: mysqld
PHP-FPM: php-fpm
Redis: redis
Memcached: memcached
使用 Cron 定时任务
Cron 是一个用于在固定时间、日期或者间隔时间执行命令或者脚本的工具。 Cron 任务可以用于定期备份文件、清理日志、定时运行脚本等。
Cron 任务的配置文件通常位于 /etc/crontab
或者 /var/spool/cron
目录中。
以下是一些常见的 Cron 任务的配置示例:
每天凌晨 3 点执行备份脚本:
bash0 3 * * * /path/to/backup.sh
每 3 小时执行一次脚本:
bash0 */3 * * * /path/to/script.sh
每周一凌晨 2 点执行清理日志脚本:
bash0 2 * * 1 /path/to/clean_logs.sh
每隔 30 分钟执行一次脚本:
bash*/30 * * * * /path/to/script.sh
每小时执行一次脚本:
bash0 * * * * /path/to/script.sh
每月 1 号凌晨 1 点执行脚本:
bash0 1 1 * * /path/to/script.sh
每月 1 日、15 日、29 日晚上 9 點 30 分各执行一次
bash30 21 1,15,29 * * /path/to/script.sh
从早上 9 点到下午 6 点,凡遇到整点就执行
bash0 9-18 * * * /path/to/script.sh
使用@reboot 标记在系统启动时执行脚本:
bash@reboot /path/to/script.sh
使用@daily 标记每天执行脚本:
@daily /path/to/script.sh
网络相关
获取本机的网络信息
获取本机的 IP 地址
ip addr show
使用 cURL 或者 wget 下载文件
cURL 是一个用于传输数据的命令行工具,支持多种协议,如 HTTP、HTTPS、FTP 等。
下面是使用 cURL 下载文件的一些常见用法:
curl -f -SOJL -H "License: license_key" \
https://chevereto.com/api/download/latest
-f
参数表示如果下载失败,则不创建文件。
-S
参数表示显示错误信息。
-O
参数表示将下载的文件保存到本地,并使用远程文件的文件名。
-J
参数表示使用远程文件的文件名。
-L
参数表示跟踪重定向。
-H
参数表示添加请求头。
-s
参数表示安静模式,不显示进度。
-k
参数表示允许不安全的 SSL 连接。
也可以使用wget来下载文件
wget -O filename https://example.com/file
常用的 wget 参数一般为:
-O
:指定下载文件的名称-P
:指定下载目录-c
:断点续传-b
:后台下载-q
:安静模式,不输出下载进度-t
:重试次数-T
:超时时间-i
:从文件中读取下载地址(后面跟文件,里面都是 url,以行分割)-r
:递归下载
测试服务器是否可以连接某网站
可以通过自带的连接工具,比如使用wget
来下载页面内容。
wget -p http://site.com
P
参数表示下载页面的所有资源
或者使用curl
命令,可以查看响应头信息
time curl -I http://yourpage.com
-I
参数表示只显示响应头信息
查看网站的 SSL 证书及部署情况
可以使用openssl
来模拟请求,查看证书详细信息:
echo | openssl s_client -connect localhost:443 -servername your_domain.com 2>/dev/null
echo
命令用于向管道发送空字符串,给openssl
命令提供输入-connect
参数表示连接的地址和端口,这里是本地 443 端口,如果是远程的话,需要替换为远程地址,可以为域名或者 IP 地址-servername
参数表示指定域名,远程服务器可能会根据域名来返回不同的证书2>/dev/null
表示将错误信息重定向到/dev/null
,这样就不会显示错误信息
也可以继续把输出信息交给openssl
来只显示证书有效期:
echo | openssl s_client -connect localhost:443 -servername your_domain.com 2>/dev/null | openssl x509 -noout -dates
查看 TCP 连接数
1)统计 80 端口连接数
netstat -nat|grep -i "80"|wc -l
2)统计 httpd 协议连接数
ps -ef|grep httpd|wc -l
3)统计已连接上的,状态为“established
netstat -na|grep ESTABLISHED|wc -l
4)查出哪个 IP 地址连接最多
# 分析当前系统上的网络连接情况,并统计每个来源 IP 地址的连接数量
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -r -n
# 按照连接次数从高到低进行排序,显示出每个 IP 地址建立的连接数量
netstat -na | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c|sort -r -n
netstat -ntu
: 这个部分执行了 netstat 命令来显示当前系统的网络连接情况。其中 -n
参数表示以数字形式显示 IP 地址和端口号,-t
参数表示显示 TCP 协议的连接,-u
参数表示显示 UDP 协议的连接。
5)显示所有监听的 TCP 端口
netstat -ntlp
6)显示特定端口的监听状态:
netstat -ntlp | grep 80
7)显示所有活动的 UNIX 域套接字:
netstat -axu
8)统计 TCP 连接状态的类型和数量:
netstat -n | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}'
9)显示连接的进程 ID 和程序名称:
netstat -natp
10)显示路由表
netstat -nr
进程管理
使用 PS 查找进程
ps aux
: 查看所有进程ps aux|grep nginx
: 查找 nginx 进程ps aux|grep php-fpm
: 查找 php-fpm 进程还可以使用
lsof -i :port
来查看指定端口的进程情况(可能需要安装 lsof)。
使用 kill 关闭进程
kill -9 PID
: 关闭指定 PID 的进程killall -9 nginx
: 关闭所有 nginx 进程-9
表示强制关闭进程(不进行清理操作)kill 后面可以跟多个进程 ID,用空格隔开
防火墙
使用 ufw 管理防火墙
如果没有就先安装在 ufw
基础命令
ufw status verbose: 查看当前防火墙状态
ufw app list 查看服务列表
ufw status [numbered]: 列出防火墙规则(可以选择加上序号)
ufw delete {num}: 有序号后就可以指定删除某条规则
常用案例
允许 22 端口的 TCP 请求访问(不加“/tcp”为允许 tcp 和 udp)
ufw allow 22/tcp
拒绝指定端口访问
# []表示可选项
ufw deny 25[/tcp comment 'Block access to smptd by default']
更多案例
To allow IP address 192.168.1.10 access to port 22 for all protocols sudo ufw allow from 192.168.1.10 to any port 22
Open port 74.86.26.69:443 (SSL 443 nginx/apache/lighttpd server) for all, enter: sudo ufw allow from any to 74.86.26.69 port 443 proto tcp
To allows subnet 192.168.1.0/24 to Sabma services, enter: ufw allow from 192.168.1.0/24 to any app Samba
To get information on Squid profile/app, run: ufw app info Squid
主机相关
查看及修改主机信息
主机的名称是服务器的标识,可以通过主机名来访问服务器,配置文件在 /etc/hostname
hostname
: 查看主机名hostname newname
: 修改主机名(临时)hostnamectl set-hostname newname
: 修改主机名hostnamectl set-hostname newname --static
: 修改静态主机名hostnamectl set-hostname newname --pretty
: 修改主机名的美观名称hostnamectl set-hostname newname --transient
: 修改临时主机名