课程地址

Linux发行版分类

  • Debian系: Debian, Ubuntu. 包管理器apt.

  • Red Hat系: Red Hat Enterprise Linux(RHEL),Fedora,CentOS. 包管理器dnf.

  • Arch Linux系: Arch Linux, Manjaro. 包管理器pacman.

  • 其他: Gentoo,包管理器portage. openSUSE 包管理器zypper.

Linux命令基础

pwd: print working directory

cd: change directory. . means current directory. .. means parent directory. ~ home directory.

ls: list files

touch: create file

file: show file type

cat: show file content

less: show file content in pages, with vim-like navigation.

history: show command history. ctrl+r to search history. !! means the whole last command.

cp: copy file

mv: move file

mkdir: make directory. -p make directory with parent directory.

rm: remove file. -r remove files recursively. -f force remove.

help: Bash built-in command to show help for some commands.

man: show manual page.

whatis: show short manual page info of command.

alias: define or display alias.

exit: exit, same as ctrl+d.

文本操作

0,1,2代表stdin,stdout,stderr,标准输入,标准输出和标准错误。

重定向符 >, <,用于重定向命令的输入输出,这里介绍 stdout 和 stderr。

>>可以追加写入而不是覆盖

将 stderr 定向到 error.txt

ls /fake/directory 2> error.txt

将 stderr、stdout 都定向到 error.txt

ls /fake/directory > error.txt 2>&1

也可以简写为:

ls /fake/directory &> error.txt

丢弃 stderr:

ls /fake/directory 2> /dev/null

管道符 |,将前一个命令的 stdout 作为后一个命令的stdin。

tee: 读取标准输入并写入到标准输出和文件。

env: 查看环境变量,可以通过 export 添加新的环境变量。

cut: extract text.

head: show lines from the head of file, default 10 lines.

tail: show lines from the tail of file, default 10 lines.

sort: sort file content.

tr: transform characters to others.

wc: word count, show lines, words, and bytes(统计行数、单词数和字节数)。

grep: powerful search command(强大的搜索命令)。

权限管理

文件权限,权限分为四部分,第一部分是文件类型,接下来三部分是实际的权限。权限每3位一组。前3位是用户权限,然后是组权限,然后是其他权限。

-|rwx|rwx|rwx

第一位-表示普通文件,d表示文件夹,l表示符号链接。然后每组表示read,write,execute权限。

修改权限使用chmod,可以使用+-修改权限,可以修改用户权限,组权限,比如

chmod u+x myfile
chmod u-x myfile
chmod ug+w

也可以用每组二进制的十进制数修改,比如755表示三个组成分别为111,101,101,即-rwxr-xr-x.

chmod 755 myfile
ls -l myfile

chown: 修改所有权

umask: 删除权限,计算方法:将umask取反然后与默认权限按位与。比如普通文件666,umask 022,得到文件644;文件夹777,得到文件夹755。

设置SUID,以文件拥有者身份执行文件,比如修改用户自己的密码时,使用passwd,这样会以root身份执行而不是用户身份。修改方法chmod u+schmod 4755,SUID用4表示。显示为-rwsr-xr-x

设置SGID,以组成员的身份执行文件,chmod g+schmod 2555。显示为-rwxr-sr-x

进程权限。Linux每个进程关联3个UID,有效用户ID(Effective UID),真实用户ID(Real UID),保存的用户ID(Saved Set-User-ID)。

RUID表示启动该进程的用户ID,EUID用于操作系统检查权限,进程以EUID执行,SSUID在EUID变化时储存先前的身份,降权之后安全返回。

粘滞位,表示只有所有者或者root才能删除或修改文件,常用于共享文件夹。使用chmod +tchmod 1755,粘滞位用1表示,显示为drwxrwxrwx+t

粘滞位只对目录有效,对文件执行该操作没有任何意义。

进程

大部分是底层操作系统的问题,以后再说。

信号

No name describe
1 SIGHUP 挂起中断,当终端关闭时发送给终端关联的进程
2 SIGINT 中断,ctrl+c,优雅地结束进程
3 SIGQUIT 退出,ctrl+\,强制中断进程,并生成核心转储文件(core dump)
9 SIGKILL 强制立即终止进程,不能被捕获,阻塞或忽略
15 SIGTERM kill的默认信号,要求进程清理资源并退出
18 SIGCOUT 使被暂停的进程恢复运行
19 SIGSTOP 暂停进程,由另一个进程发送,不能被捕获,阻塞或忽略
20 SIGTSTP 暂停进程,ctrl+z,请求进程暂停运行并放入后台,可以被程序捕获或处理

/proc文件系统,在 Linux 中,一切皆文件,甚至进程也是。进程信息存储在一个特殊的 文件系统 中,称为 /proc 文件系统。ls /proc可以查看每一个PID,可以查看详细信息。

作业控制

在命令后加一个&符号使命令在后台运行,使用jobs查看后台作业。+表示最近的后台作业,-表示倒数第二个。

对已经执行的进程,使用ctrl+z暂停,然后使用bg放到后台继续执行。

使用fg %num将后台进程移到前台。使用kill %num终止进程。注意加上%标识符。