#13 Linux 文件描述符

2016-02-01

文件描述符

File Descriptor

“一切皆文件” 设计思想来自早期 Unix,这是现在 Unix/Linux 界的一个非常重要的概念(根据 Plan9 操作系统的相关资料,这一条没有得到彻底的贯彻)。

允许打开的最大文件数

系统限制

cat /proc/sys/fs/file-nr
# 17781 0   9223372036854775807
# 已分配, 已使用, 最大值

cat /proc/sys/fs/file-max
# 9223372036854775807

sudo sysctl -a | grep file
# fs.file-max = 9223372036854775807
# fs.file-nr = 17813    0   9223372036854775807

修改:

echo 100000000 > /proc/sys/fs/file-max
sysctl fs.file-max 100000000

进程限制

# ulimit 是一个 Shell 内建命令
ulimit -a       # 输出所有的相关限制
ulimit -n       # 当前进程的最大打开文件数
ulimit -n 10240 # 修改当前进程的最大打开文件数

ps -ef | grep sshd | grep -Fv 'grep '
root        1080       1  0 10月16 ?      00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
sudo ls -l /proc/`pgrep sshd`/fd
总用量 0
lr-x------ 1 root root 64 10月 17 12:04 0 -> /dev/null
lrwx------ 1 root root 64 10月 17 12:04 1 -> 'socket:[25487]'
lrwx------ 1 root root 64 10月 17 12:04 2 -> 'socket:[25487]'
lrwx------ 1 root root 64 10月 17 12:04 3 -> 'socket:[28674]'
lrwx------ 1 root root 64 10月 17 12:04 4 -> 'socket:[28676]'

/etc/security/limits.conf

echo "* soft nofile 65535" | sudo tee -a /etc/security/limits.conf
echo "* hard nofile 65535" | sudo tee -a /etc/security/limits.conf

系统会根据 /etc/pam.d/login 中的 PAM 配置来为登录用户设置,
如果配置了 session required /lib/security/pam_limits.so, 就会加载 /etc/security/limits.conf 设置用户的各种限制值。

<domain>        <type>  <item>  <value>
  1. domain
  2. a user name
  3. a group name, with @group syntax
  4. the wildcard *, for default entry
  5. the wildcard %, can be also used with %group syntax, for maxlogin limit
  6. NOTE: group and wildcard limits are not applied to root.
    To apply a limit to the root user, must be the literal username root.
  7. type
  8. "soft" for enforcing the soft limits
  9. "hard" for enforcing hard limits
  10. item
  11. core - limits the core file size (KB)
  12. data - max data size (KB)
  13. fsize - maximum filesize (KB)
  14. memlock - max locked-in-memory address space (KB)
  15. nofile - max number of open file descriptors
  16. rss - max resident set size (KB)
  17. stack - max stack size (KB)
  18. cpu - max CPU time (MIN)
  19. nproc - max number of processes
  20. as - address space limit (KB)
  21. maxlogins - max number of logins for this user
  22. maxsyslogins - max number of logins on the system
  23. priority - the priority to run user process with
  24. locks - max number of file locks the user can hold
  25. sigpending - max number of pending signals
  26. msgqueue - max memory used by POSIX message queues (bytes)
  27. nice - max nice priority allowed to raise to values: [-20, 19]
  28. rtprio - max realtime priority
  29. chroot - change root to directory (Debian-specific)

#11 bc 计算器

2016-01-18

可能这是 Linux 命令行使用频率最高的一个计算器工具了。

其语法可以说比较复杂,可以定义变量,有各种逻辑控制语句,甚至可以定义函数,简直可以拿来做程序设计了。但是我们一般可能用不了这么多功能,就用来做一些简单计算就行。

PS:奇怪的是,有这么多复杂的功能,竟然没有包含位运算。

bc <<< "1 + 3 + 5 + 7 + 9"
26

bc <<< "scale=3; 1 / 3"
.333

bc <<< "obase=2; 14"
1110

bc <<< "ibase=16;obase=2; FF - 1A"
11100101

#10 Shell:长字符串切割成数组

2016-01-16
long_string="line 1
line 2
line 3"

# bash ========
# readarray -t lines <<< "$long_string"
# IFS=$'\n' lines=($long_string)

# zsh =========
# lines=("${(@f)long_string}")
setopt sh_word_split

for line in "${lines[@]}"
do
    echo "$line =="
done

#9 为 CentOS 升级 Linux 内核

2015-11-19

查看内核版本

  • cat /proc/version
  • uname -a-r 参数可以只查看内核版本号

查看发行版版本

  • lsb_release -a
  • cat /etc/redhat-release(CentOS)

升级

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -ivh https://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-lt -y  # long-term 长期支持版本
# yum --enablerepo=elrepo-kernel install kernel-ml -y  # mainline 主线版本

参考

#8 使用 col 命令删除退格符

2015-09-12

如果通过定向符号输出 man 文档到文件,然后通过 vi 打开会看到很多蓝色的 ^H 符号。
如果是用 catlessmore 等命令查看(输出到屏幕)就不会看到。

#7 unzip 压缩工具

2015-01-19
zip -e archive.zip file1.txt file2.txt

unzip -l archive.zip # 列出

# 解压缩
unzip archive.zip
unzip -o archive.zip # 覆盖
unzip archive.zip file1.txt file2.txt -d /path/to/directory/ # 指定文件,指定目录
unzip -P password archive.zip # 有密码的压缩文件

#6 Linux 信号

2015-01-17

信号

信号可以被捕获,或者被忽略,除了 9 KILL 和 19 STOP

信号值

Ubuntu:

$ kill -l
HUP INT QUIT ILL TRAP IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS

$ type kill
kill is a shell builtin

$ /usr/bin/kill -L
 1 HUP      2 INT      3 QUIT     4 ILL      5 TRAP     6 ABRT     7 BUS
 8 FPE      9 KILL    10 USR1    11 SEGV    12 USR2    13 PIPE    14 ALRM
15 TERM    16 STKFLT  17 CHLD    18 CONT    19 STOP    20 TSTP    21 TTIN
22 TTOU    23 URG     24 XCPU    25 XFSZ    26 VTALRM  27 PROF    28 WINCH
29 POLL    30 PWR     31 SYS

CentOS:

$ 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) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 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
  1. HUP 终端关闭
  2. INT Ctrl + C
  3. QUIT 终端退出 Ctrl + 4 or \
  4. ILL
  5. TRAP
  6. IOT/ABRT
  7. BUS
  8. FPE
  9. KILL 常来自 kill -9
  10. USR1
  11. SEGV
  12. USR2
  13. PIPE
  14. ALRM
  15. TERM
  16. STKFLT
  17. CHLD 子进程终止
  18. CONT
  19. STOP
  20. TSTP 终端挂起 Ctrl + Z
  21. TTIN
  22. TTOU
  23. URG
  24. XCPU
  25. XFSZ
  26. VTALRM
  27. PROF
  28. WINCH 终端窗口大小改变
  29. POLL
  30. PWR
  31. SYS

由于 9 KILL 不能被捕获,设计软件时可以捕获 15 TERM, 进行清理操作,然后需要杀进程时,先尝试 kill -15

附: 信号表(来自 man 7 signal

Signal x86/ARM
most others
Alpha/
SPARC
MIPS PARISC Notes
SIGHUP 1 1 1 1
SIGINT 2 2 2 2
SIGQUIT 3 3 3 3
SIGILL 4 4 4 4
SIGTRAP 5 5 5 5
SIGABRT 6 6 6 6
SIGIOT 6 6 6 6
SIGBUS 7 10 10 10
SIGEMT - 7 7 -
SIGFPE 8 8 8 8
SIGKILL 9 9 9 9
SIGUSR1 10 30 16 16
SIGSEGV 11 11 11 11
SIGUSR2 12 31 17 17
SIGPIPE 13 13 13 13
SIGALRM 14 14 14 14
SIGTERM 15 15 15 15
SIGSTKFLT 16 - - 7
SIGCHLD 17 20 18 18
SIGCLD - - 18 -
SIGCONT 18 19 25 26
SIGSTOP 19 17 23 24
SIGTSTP 20 18 24 25
SIGTTIN 21 21 26 27
SIGTTOU 22 22 27 28
SIGURG 23 16 21 29
SIGXCPU 24 24 30 12
SIGXFSZ 25 25 31 30
SIGVTALRM 26 26 28 20
SIGPROF 27 27 29 21
SIGWINCH 28 28 20 23
SIGIO 29 23 22 22
SIGPOLL Same as SIGIO
SIGPWR 30 29/- 19 19
SIGINFO - 29/- - -
SIGLOST - -/29 - -
SIGSYS 31 12 12 31
SIGUNUSED 31 - - 31

#5 Linux 时间: atime, mtime, ctime

2015-01-03

Linux 文件的三种时间:

  • atime 访问时间 Access
  • ctime 修改时间 Change, 文件状态变化
  • mtime 修改时间 Modify, 文件内容变化

注意:如果 chmod,chown,mv 等不改变文件内容的操作,ctime 变化,mtime 不变。
但是文件内容变化的时候,ctime 和 mtime 都会变化,文件大小没有变化也是一样。
如果通过充定向的方式改变文件内容(> or >>),atime 不会发生变化。

之前有一个测试,可以参考: 2013/04/07, ls 按时间排序

Linux 并没有创建时间,这是令人费解的,可能是早期文件系统没有这个信息吧。

PS: 关于文件的创建时间,参考:2021-12-12 Linux 文件创建时间

#4 Linux 权限管理总结

2014-02-27

文件类型

  • -, 普通文件
  • l, 链接文件
  • d, 目录
  • c, 字符设备文件
  • b, 块设备文件
  • p, 管道文件
  • s, Socket
  • t, 特殊文件

基础

  • 权限类型:
  • r,读 4
  • w,写 2
  • x,可执行 1
  • 用户类型:
  • user, 文件所有者
  • group, 文件所有组的所有用户
  • other,其他用户

目录的权限

  • r: 列出目录下的文件(ls
  • w: 在目录中,创建、删除、重命名文件或子目录
  • x: 进入目录(cd

示例

touch /tmp/testPerm

chmod 7777 /tmp/testPerm

ll /tmp/testPerm
-rwsrwsrwt 1 markjour markjour 0 2014-02-27 17:41:06 /tmp/testPerm

Linux 权限的两种表示方法:

  • 数字:
  • 字母:
    第一位表示文件类型
    第 2 - 4 位表示文件所有者的权限
    第 5 - 7 位表示文件所有组的权限
    第 8 - 10 位表示其他用户的权限

特殊权限

Linux 中的权限一共用 12 位来表示,除了上面 u g o 各 3 位(rwx)一共 9 位之外,还有 3 位:

  • setuid / suid, 4,显示为 S(显示在所有者权限中的第三位,如果有 x 的话,就显示 s)
  • setgid / sgid,2,显示为 S(显示在所有组权限中的第三位,如果有 x 的话,就显示 s)
  • sticky bit ,1,显示为 T

  • suid, sgid 是用来给文件设置临时提权用的。如果设置, 所有用户都会继承文件所有者或所有组的权限。

  • 如果目录设置了 sgid,那么该目录中新创建的文件和子目录会继承目录的所在组。
  • sticky bit 一般翻译为粘滞位,设置在目录上, 仅允许文件所有者(或 root)才能删除改目录下的文件。
    Linux 或 FreeBSD 都会忽略文件上的 sticky bit。
  suid sgid sbit
文件
目录
$ ls -alh /bin/ | grep rws
-rwsr-sr-x  1 daemon  daemon      55K 2022-04-14 08:23:36 at
-rwsr-xr-x  1 root    root        72K 2022-11-24 20:05:18 chfn
-rwsr-xr-x  1 root    root        44K 2022-11-24 20:05:18 chsh
-rwsr-xr-x  1 root    root        35K 2022-03-23 21:53:14 fusermount3
-rwsr-xr-x  1 root    root        71K 2022-11-24 20:05:18 gpasswd
-rwsr-xr-x  1 root    root        47K 2023-04-07 06:21:06 ksu
-rwsr-xr-x  1 root    root        47K 2022-02-21 09:49:57 mount
-rwsr-xr-x  1 root    root        28K 2022-11-24 20:05:18 newgidmap
-rwsr-xr-x  1 root    root        40K 2022-11-24 20:05:18 newgrp
-rwsr-xr-x  1 root    root        28K 2022-11-24 20:05:18 newuidmap
-rwsr-xr-x  1 root    root        31K 2022-02-07 21:05:06 nvidia-modprobe
-rwsr-xr-x  1 root    root        59K 2022-11-24 20:05:18 passwd
-rwsr-xr-x  1 root    root        31K 2022-02-26 19:11:57 pkexec
-rwsr-xr-x  1 root    root        55K 2022-02-21 09:49:57 su
-rwsr-xr-x  1 root    root       227K 2023-04-04 02:00:44 sudo
-rwsr-xr-x  1 root    root        35K 2022-02-21 09:49:57 umount
$ find /bin/ -type f -perm /4000 -ls

  7342874     36 -rwsr-xr-x   1 root     root        35200 Mar 23  2022 /bin/fusermount3
  7352563     28 -rwsr-xr-x   1 root     root        28136 Nov 24  2022 /bin/newuidmap
  7341794     32 -rwsr-xr-x   1 root     root        30872 Feb 26  2022 /bin/pkexec
  7347918     36 -rwsr-xr-x   1 root     root        35192 Feb 21  2022 /bin/umount
  7347747     48 -rwsr-xr-x   1 root     root        47480 Feb 21  2022 /bin/mount
  7344663     28 -rwsr-xr-x   1 root     root        28136 Nov 24  2022 /bin/newgidmap
  7349993     72 -rwsr-xr-x   1 root     root        72072 Nov 24  2022 /bin/gpasswd
  7350008     60 -rwsr-xr-x   1 root     root        59976 Nov 24  2022 /bin/passwd
  7347439    228 -rwsr-xr-x   1 root     root       232416 Apr  4 02:00 /bin/sudo
  7344105     32 -rwsr-xr-x   1 root     root        30936 Feb  7  2022 /bin/nvidia-modprobe
  7345135     56 -rwsr-xr-x   1 root     root        55672 Feb 21  2022 /bin/su
  7347241     72 -rwsr-xr-x   1 root     root        72712 Nov 24  2022 /bin/chfn
  7352704     40 -rwsr-xr-x   1 root     root        40496 Nov 24  2022 /bin/newgrp
  7347300     44 -rwsr-xr-x   1 root     root        44808 Nov 24  2022 /bin/chsh
  7382902     48 -rwsr-xr-x   1 root     root        47416 Apr  7 06:21 /bin/ksu
  7343017     56 -rwsr-sr-x   1 daemon   daemon      55624 Apr 14  2022 /bin/at

$ find /bin/ -type f -perm /2000 -ls

  7342246     16 -rwxr-sr-x   1 root     root        15504 Jan 12  2022 /bin/dotlock.mailutils
  7347219     72 -rwxr-sr-x   1 root     shadow      72184 Nov 24  2022 /bin/chage
  7346616     24 -rwxr-sr-x   1 root     tty         22912 Feb 21  2022 /bin/write.ul
  7340649    308 -rwxr-sr-x   1 root     plocate    313904 Feb 17  2022 /bin/plocate
  7341420     40 -rwxr-sr-x   1 root     crontab     39568 Mar 23  2022 /bin/crontab
  7349934     24 -rwxr-sr-x   1 root     shadow      23136 Nov 24  2022 /bin/expiry
  7347279     24 -rwxr-sr-x   1 root     tty         22904 Feb 21  2022 /bin/wall
  7345047    288 -rwxr-sr-x   1 root     _ssh       293304 Nov 23  2022 /bin/ssh-agent
  7343017     56 -rwsr-sr-x   1 daemon   daemon      55624 Apr 14  2022 /bin/at

https://en.wikipedia.org/wiki/Setuid
https://en.wikipedia.org/wiki/Sticky_bit
https://web.archive.org/web/20130204053849/http://content.hccfl.edu/pollock/aunix1/filepermissions.htm

操作

  • chown 更改文件所有者和文件所有组
  • chgrp 更改文件所有组
  • chmod 更改权限
chmod 755 filePath
chmod 644 filePath
chmod u+x filePath
chmod g+r filePath
chmod o+w filePath
chmod a+r filePath