Linux 零基础速查看完就能上手的基础命令合集7管理文件扩展权限两个常用属性append only (a)只能追加文件内容。immutable (i)不可变更属性。append only给文件加上a属性之后不可以删除、覆盖文件只能追加内容。 [rootCentOS7 ~ 17:02:02]# touch /opt/operator.log [rootCentOS7 ~ 17:02:20]# chattr a /opt/operator.log [rootCentOS7 ~ 17:02:33]# echo hello world /opt/operator.log -bash: /opt/operator.log: 不允许的操作 [rootCentOS7 ~ 17:02:50]# echo hello world 1 /opt/operator.log [rootCentOS7 ~ 17:03:11]# echo hello world 2 /opt/operator.log [rootCentOS7 ~ 17:03:17]# cat /opt/operator.log hello world 1 hello world 2 [rootCentOS7 ~ 17:03:40]# rm -f /opt/operator.log rm: 无法删除/opt/operator.log: 不允许的操作immutable 属性给文件加上i属性之后不可以删除、覆盖文件、追加内容。 [rootCentOS7 ~ 17:12:11]# chattr i passwd [rootCentOS7 ~ 17:12:13]# echo lw:x:1000:1000:lw:/home/lw:/bin/bash passwd -bash: passwd: 权限不够 [rootCentOS7 ~ 17:12:24]# rm -f passwd rm: 无法删除passwd: 不允许的操作 重要的文件内容改完后再把i属性加回去 [rootCentOS7 ~ 17:12:45]# chattr -i passwd [rootCentOS7 ~ 17:13:00]# echo lw:x:1000:1000:lw:/home/lw:/bin/bash passwd [rootCentOS7 ~ 17:13:07]# chattr i passwdLinux 进程管理进程介绍程序没有运行的可执行文件。软件包含程序、手册、配置文件等全套的东西。软件工程。进程程序运行后会使用cpu、内存、磁盘、网络等资源。线程将程序划分成更小的执行单元。进程是什么进程是已启动的可执行程序的运行中实例。进程由以下组成部分已分配内存的地址空间安全属性包括所有权凭据和特权程序代码的一个或多个执行线程进程状态进程的环境包括本地和全局变量当前调度上下文分配的系统资源如文件描述符和网络端口查看进程-ps作用查看系统中进程信息。限定查看哪些进程用来选择进程。对选定的进程查看他们哪些属性。# PID进程ID # TTY进程所在终端 # TIME进程使用cpu的有效时间并不是程序的运行时间。 # CMD进程名称 [gzkCentOS7 ~ 18:37:28]$ ps -e PID TTY TIME CMD 1 ? 00:00:02 systemd 2 ? 00:00:00 kthreadd 4 ? 00:00:00 kworker/0:0H 6 ? 00:00:00 ksoftirqd/0 # PID进程ID # TTY进程所在终端 # TIME进程使用cpu的有效时间并不是程序的运行时间。 # CMD进程名称 [gzkCentOS7 ~ 18:39:24]$ ps -ef |head -n 5 UID PID PPID C STIME TTY TIME CMD root 1 0 0 09:15 ? 00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 root 2 0 0 09:15 ? 00:00:00 [kthreadd] root 4 2 0 09:15 ? 00:00:00 [kworker/0:0H] root 6 2 0 09:15 ? 00:00:00 [ksoftirqd/0]查看特定用户运行的进程[gzkCentOS7 ~ 18:39:28]$ ps -u gzk u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND gzk 9688 0.0 0.0 161072 2536 ? S 15:12 0:00 sshd: gzkpts/0 gzk 9692 0.0 0.0 116528 3016 pts/0 Ss 15:12 0:00 -bash gzk 9793 0.0 0.0 161072 2552 ? S 15:17 0:00 sshd: gzkpts/1 gzk 9796 0.0 0.0 116448 2948 pts/1 Ss 15:17 0:00 -bash查看特定终端中进程[gzkCentOS7 ~ 18:41:32]$ ps -t pts/1 u USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND gzk 9796 0.0 0.0 116448 2948 pts/1 Ss 15:17 0:00 -bash查看所有进程[gzkCentOS7 ~ 18:41:33]$ ps axu USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 193904 7048 ? Ss 09:15 0:02 /usr/lib/systemd/s root 2 0.0 0.0 0 0 ? S 09:15 0:00 [kthreadd] root 4 0.0 0.0 0 0 ? S 09:15 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? S 09:15 0:00 [ksoftirqd/0]查看进程树[gzkCentOS7 ~ 18:43:07]$ ps axf PID TTY STAT TIME COMMAND 2 ? S 0:00 [kthreadd] 4 ? S 0:00 \_ [kworker/0:0H] 6 ? S 0:00 \_ [ksoftirqd/0] 7 ? S 0:00 \_ [migration/0] 8 ? S 0:00 \_ [rcu_bh] 9 ? S 0:01 \_ [rcu_sched] 10 ? S 0:00 \_ [lru-add-drain]查看特定进程特定属性通过PID限定 [gzkCentOS7 ~ 18:45:28]$ ps -o pid,%cpu,%mem,command 11523 PID %CPU %MEM COMMAND 11523 0.0 0.5 /usr/libexec/evolution-addressbook-factory-subprocess --factory all 通过命令名称限定 [gzkCentOS7 ~ 18:46:43]$ ps -C passwd -o pid,ruser,euser,command PID RUSER EUSER COMMAND对进程进行排序–sort %cpu升序 –sort -%cpu降序[gzkCentOS7 ~ 18:46:46]$ ps axu --sort -%cpu USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 733 0.1 0.1 295564 5304 ? Ssl 09:15 0:40 /usr/bin/vmtoolsd gzk 11595 0.1 0.6 608632 25196 ? Sl 16:58 0:06 /usr/bin/vmtoolsd root 13328 0.1 0.1 161072 5572 ? Ss 18:46 0:00 sshd: gzk [priv] root 1 0.0 0.1 193904 7048 ? Ss 09:15 0:02 /usr/lib/systemd/s root 2 0.0 0.0 0 0 ? S 09:15 0:00 [kthreadd]控制 jobs作业控制允许单个shell实例运行和管理多个命令。前台进程一个终端中只有一个前台进程该进程可以终端窗口中读取输入和响应键盘生成的信号。后台进程在后台运行的进程该进程不能从终端读取输入或接收键盘产生的中断。后台进程可能暂停也可能正在运行。如果正在运行的后台作业尝试从终端读取它将自动暂停。在ps列表中tty终端列显示的是?号那么该进程肯定是后台进程但并代表显示为其他终端的进程就不是后台进程。示例准备工作切换到root里操作 [rootCentOS7 ~ 17:13:20]# cat EOF /usr/local/bin/study #!/bin/bash while true do echo $(date): Im studying [ $ ] study.log sleep 1 done EOF [rootCentOS7 ~ 18:52:38]# chmod x /usr/local/bin/study任何一个进程都可以在后台启动通过增加一个符号。 bash会显示job号和进程的PID。shell不需要等待子进程运行结束而可以继续运行其他命令。 后台方式运行 [gzkCentOS7 ~ 18:55:23]$ study Linux [1] 13477 [gzkCentOS7 ~ 18:55:27]$ jobs [1] 运行中 study Linux 新开窗口动态监控文件study.log内容 [gzkCentOS7 ~ 18:55:30]$ tail -f study.log 2026年 07月 21日 星期二 18:55:48 CST: Im studying [ Linux ] 2026年 07月 21日 星期二 18:55:49 CST: Im studying [ Linux ] 2026年 07月 21日 星期二 18:55:50 CST: Im studying [ Linux ] 2026年 07月 21日 星期二 18:55:51 CST: Im studying [ Linux ] 2026年 07月 21日 星期二 18:55:52 CST: Im studying [ Linux ] 前台方式运行ctrlZ 暂停前台进程 [gzkCentOS7 ~ 18:55:36]$ study Python ^Z [2] 已停止 study Python 将job2放到后台运行 [gzkCentOS7 ~ 18:58:58]$ bg %2 [2] study Python [gzkCentOS7 ~ 19:00:36]$ jobs [1]- 运行中 study Linux [2] 运行中 study Python 将后台的进程拿到前台前台运行的进程按ctrlc终止进程运行 [gzkCentOS7 ~ 19:01:01]$ fg %1 study Linux ^C [gzkCentOS7 ~ 19:02:27]$ fg %2 study Python ^C [gzkCentOS7 ~ 19:02:37]$ jobs如何避免意外断网和关闭终端导致终端中后台运行的进程终止通过nohup命令可以实现关闭终端后仍然可以让进程运行。 nohup commnad [gzkCentOS7 ~ 19:06:12]$ nohup study English [1] 14888 [gzkCentOS7 ~ 19:06:16]$ nohup: 忽略输入并把输出追加到nohup.out [gzkCentOS7 ~ 19:06:24]$ jobs [1] 运行中 nohup study English 重启终端后因为不在之前那个终端了所以jobs查不到所以使用ps -C 结束进程用kill 端口号。 [gzkCentOS7 ~ 19:07:11]$ ps -C study PID TTY TIME CMD 14888 ? 00:00:00 study [gzkCentOS7 ~ 19:09:47]$ kill 14888 [gzkCentOS7 ~ 19:09:58]$ ps -C study PID TTY TIME CMDkill 命令作用给进程发信号。准备工作 [rootCentOS7 ~ 18:52:38]# chmod x /usr/local/bin/study [rootCentOS7 ~ 18:52:48]# cat EOF /usr/local/bin/study #!/bin/bash while true do echo $(date): Im studying [ $ ] study.log sleep 1 done EOF [rootCentOS7 ~ 19:20:03]# chmod x /usr/local/bin/study新开窗口动态监控文件study.log内容 [gzkCentOS7 ~ 19:22:22]$ tail -f study.log [gzkCentOS7 ~ 19:10:09]$ study Linux study Python [1] 15582 [2] 15583 [gzkCentOS7 ~ 19:23:05]$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN1 36) SIGRTMIN2 37) SIGRTMIN3 38) SIGRTMIN4 39) SIGRTMIN5 40) SIGRTMIN6 41) SIGRTMIN7 42) SIGRTMIN8 43) SIGRTMIN9 44) SIGRTMIN10 45) SIGRTMIN11 46) SIGRTMIN12 47) SIGRTMIN13 48) SIGRTMIN14 49) SIGRTMIN15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX 给job id为1的进程发19信号暂停运行 [gzkCentOS7 ~ 19:23:24]$ kill -19 %1 [1] 已停止 study Linux [gzkCentOS7 ~ 19:25:28]$ jobs [1] 已停止 study Linux [2]- 运行中 study Python 给job id为1的进程发18信号继续运行 [gzkCentOS7 ~ 19:25:39]$ kill -18 %1 [gzkCentOS7 ~ 19:25:47]$ jobs [1] 运行中 study Linux [2]- 运行中 study Python 给job id为2的进程发SIGTERM信号终止程序运行该信号是默认信号 [gzkCentOS7 ~ 19:25:49]$ kill -SIGTERM %2 [gzkCentOS7 ~ 19:27:42]$ jobs [1] 运行中 study Linux [2]- 已终止 study Python [gzkCentOS7 ~ 19:27:46]$ jobs [1] 运行中 study Linux SIGTERM 信号是默认信号 [gzkCentOS7 ~ 19:27:49]$ kill %1 [gzkCentOS7 ~ 19:29:37]$ jobs [1] 已终止 study Linux [gzkCentOS7 ~ 19:29:38]$ jobs给PID是17049的进程发默认信号 [gzkCentOS7 ~ 19:32:33]$ study MySQL [1] 17049 [gzkCentOS7 ~ 19:32:37]$ ps axu|grep study gzk 15581 0.0 0.0 108092 620 pts/1 S 19:22 0:00 tail -f study.log gzk 17049 0.0 0.0 113284 1440 pts/0 S 19:32 0:00 /bin/bash /usr/local/bin/study MySQL gzk 17077 0.0 0.0 112824 988 pts/0 S 19:32 0:00 grep --colorautostudy [gzkCentOS7 ~ 19:32:49]$ kill 17049 [1] 已终止 study MySQL [gzkCentOS7 ~ 19:32:57]$ jobspkill 和 pgrep 命令作用给多个进程发信号。[gzkCentOS7 ~ 19:38:15]$ sleep 1231 sleep 1232 sleep 1233 [1] 17141 [2] 17142 [3] 17143 [gzkCentOS7 ~ 19:38:18]$ pgrep sleep 17141 17142 17143 17151 [gzkCentOS7 ~ 19:38:28]$ pgrep -l sleep 17141 sleep 17142 sleep 17143 sleep 17151 sleep [gzkCentOS7 ~ 19:38:35]$ ps axu|grep sleep gzk 17141 0.0 0.0 108052 360 pts/0 S 19:38 0:00 sleep 1231 gzk 17142 0.0 0.0 108052 360 pts/0 S 19:38 0:00 sleep 1232 gzk 17143 0.0 0.0 108052 360 pts/0 S 19:38 0:00 sleep 1233 root 17151 0.0 0.0 108052 360 ? S 19:38 0:00 sleep 60 gzk 17155 0.0 0.0 112824 988 pts/0 S 19:38 0:00 grep --colorautosleep [gzkCentOS7 ~ 19:38:43]$ pkill sleep pkill: killing pid 17151 failed: 不允许的操作 [1] 已终止 sleep 1231 [2]- 已终止 sleep 1232 [3] 已终止 sleep 1233 [gzkCentOS7 ~ 19:38:55]$ ps axu|grep sleep root 17151 0.0 0.0 108052 360 ? S 19:38 0:00 sleep 60 gzk 17158 0.0 0.0 112824 988 pts/0 R 19:39 0:00 grep --colorautosleepkill注销用户根据用户名匹配程序 [gzkCentOS7 ~ 19:40:51]$ ps -u gzk [gzkCentOS7 ~ 19:40:56]$ pgrep -u gzk kill相应用户所有进程也就是注销用户 [gzkCentOS7 ~ 19:41:20]$ pkill -u gzk根据终端匹配 [gzkCentOS7 ~ 19:44:03]$ sleep 1231 sleep 1232 [1] 17606 [2] 17607 [gzkCentOS7 ~ 19:44:06]$ tty /dev/pts/0 [gzkCentOS7 ~ 19:44:13]$ pgrep -t pts/0 -l 17507 bash 17606 sleep 17607 sleep [gzkCentOS7 ~ 19:44:26]$ pkill -t pts/0 [1]- 已终止 sleep 1231 [2] 已终止 sleep 1232根据PPID给子进程发信号通过父id终止进程 [gzkCentOS7 ~ 19:44:59]$ sleep 1231 sleep 1232 [1] 17639 [2] 17640 [gzkCentOS7 ~ 19:46:42]$ ps jf PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 17564 17567 17567 17567 pts/1 17567 Ss 1000 0:00 -bash 17503 17507 17507 17507 pts/0 17641 Ss 1000 0:00 -bash 17507 17639 17639 17507 pts/0 17641 S 1000 0:00 \_ sleep 1231 17507 17640 17640 17507 pts/0 17641 S 1000 0:00 \_ sleep 1232 17507 17641 17641 17507 pts/0 17641 R 1000 0:00 \_ ps jf [gzkCentOS7 ~ 19:46:47]$ pkill -P 17507 [1]- 已终止 sleep 1231 [2] 已终止 sleep 1232whoami-who-w-last 命令当前系统登录的用户 [gzkCentOS7 ~ 19:49:08]$ whoami gzk 当前系统登录的用户详细信息 [gzkCentOS7 ~ 19:49:11]$ who gzk pts/0 2026-07-21 19:42 (10.1.8.1) gzk pts/1 2026-07-21 19:43 (10.1.8.1) root pts/2 2026-07-21 15:20 (10.1.8.1) 当前系统登录的用户详细信息 [gzkCentOS7 ~ 19:49:19]$ w 19:49:20 up 10:34, 4 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN IDLE JCPU PCPU WHAT gzk pts/0 10.1.8.1 19:42 0.00s 0.02s 0.00s w gzk pts/1 10.1.8.1 19:43 5:38 0.00s 0.00s -bash root pts/2 10.1.8.1 15:20 29:12 0.05s 0.05s -bash 系统中用户登录记录 [gzkCentOS7 ~ 19:49:20]$ last gzk pts/1 10.1.8.1 Tue Jul 21 19:43 still logged in gzk pts/0 10.1.8.1 Tue Jul 21 19:42 still logged in gzk pts/0 10.1.8.1 Tue Jul 21 19:06 - 19:41 (00:34) gzk pts/1 10.1.8.1 Tue Jul 21 18:55 - 19:41 (00:46) gzk pts/0 10.1.8.1 Tue Jul 21 18:46 - 19:06 (00:20) gzk :0 :0 Tue Jul 21 16:58 gone - no logout root pts/2 10.1.8.1 Tue Jul 21 15:20 still logged in gzk pts/1 10.1.8.1 Tue Jul 21 15:17 - 18:46 (03:28) gzk pts/0 10.1.8.1 Tue Jul 21 15:12 - 18:46 (03:33) gzk pts/0 10.1.8.1 Tue Jul 21 13:57 - 15:12 (01:15) gzk pts/0 10.1.8.1 Tue Jul 21 13:55 - 13:57 (00:01)kill 不掉的进程特殊示例 一个进程杀掉之后又出现了。怎么让这个程序永久 kill 掉 准备工作 [gzkCentOS7 ~ 19:51:45]$ mkdir bin [gzkCentOS7 ~ 19:51:49]$ cat bin/mm EOF #!/bin/bash while true do md5sum /dev/zero sleep 1 done EOF [gzkCentOS7 ~ 19:52:01]$ chmod x bin/mm [gzkCentOS7 ~ 19:52:09]$ nohup mm [1] 17713 [gzkCentOS7 ~ 19:52:48]$ nohup: 忽略输入并把输出追加到nohup.out查找进程 [gzkCentOS7 ~ 19:53:22]$ ps axo pid,%cpu,command --sort -%cpu |head -n 5 PID %CPU COMMAND 17714 99.0 md5sum /dev/zero 17277 0.2 /usr/bin/gnome-shell 733 0.1 /usr/bin/vmtoolsd 1 0.0 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 kill 进程再次查看,发现又新生成了一个 [gzkCentOS7 ~ 19:53:38]$ kill 17714 [gzkCentOS7 ~ 19:53:57]$ ps axo pid,%cpu,command --sort -%cpu |head -n 5 PID %CPU COMMAND 17727 151 md5sum /dev/zero 17277 0.2 /usr/bin/gnome-shell 733 0.1 /usr/bin/vmtoolsd 1 0.0 /usr/lib/systemd/systemd --switched-root --system --deserialize 22解决办法找到幕后推手也就是找到父进程 [gzkCentOS7 ~ 19:54:00]$ ps ax -f |grep -e md5sum -e PPID UID PID PPID C STIME TTY STAT TIME CMD gzk 17727 17713 99 19:53 pts/0 R 2:09 md5sum /dev/zero gzk 17749 17507 0 19:56 pts/0 S 0:00 grep --colorauto -e md5sum -ePPID 先终止父进程再终止子进程。 [gzkCentOS7 ~ 19:56:08]$ kill 17713 [gzkCentOS7 ~ 19:56:42]$ kill 17727 [1] 已终止 nohup mm [gzkCentOS7 ~ 19:56:52]$ ps axo pid,%cpu,command --sort -%cpu |head -n 5 PID %CPU COMMAND 733 0.1 /usr/bin/vmtoolsd 17277 0.1 /usr/bin/gnome-shell 1 0.0 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 2 0.0 [kthreadd]