#203 Linux 命名空间

2017-05-11

命名空间是什么

对内核资源进行分区,为进程隔离提供支持。

命名空间类型

Namespace CloneFlag Version Year
Mount (mnt) CLONE_NEWNS 2.4.19 2002
UTS CLONE_NEWUTS 2.6.19 2006
Inter-process Communication (ipc) CLONE_NEWIPC 2.6.19 2006
Process ID (pid) CLONE_NEWPID 2.6.24 2008
Network (net) CLONE_NEWNET 2.6.29 2009
User ID (user) CLONE_NEWUSER 3.8 2013
Control group (cgroup) CLONE_NEWCGROUP 4.6 2016
Time CLONE_NEWTIME 5.6 2020

Mount

挂载点可以在不同命名空间之间共享。

UTS

UNIX Time-Sharing

不知道为什么叫这个名字。作用是隔离主机名和域名。

IPC

不同 IPC 命名空间的进程不能通过 shm 系列方法共享内存。

PID

Network

每个网络命名空间都有一组自己的网络资源,包括 IP 地址、路由表、套接字列表、连接跟踪表、防火墙等等。

User

主要作用是权限的隔离。

Cgroup

进程控制组。

Time

时间的隔离。

系统调用

  • clone
  • unshare
  • setns

参考资料与拓展阅读

#202 Python 加密解密

2017-05-04

#201 英文花体

2017-04-27
| 𝓐 | 𝓑 | 𝓒 | 𝓓 | 𝓔 | 𝓕 | 𝓖 | | :-: | :-: | :-: | :-: | :-: | :-: | :----: | | 𝓗 | 𝓘 | 𝓙 | 𝓚 | 𝓛 | 𝓜 | 𝓝 | | 𝓞 | 𝓟 | 𝓠 | 𝓡 | 𝓢 | 𝓣 |   | | 𝓤 | 𝓥 | 𝓦 | 𝓧 | 𝓨 | 𝓩 |   | | 𝓪 | 𝓫 | 𝓬 | 𝓭 | 𝓮 | 𝓯 | 𝓰 | | 𝓱 | 𝓲 | 𝓳 | 𝓴 | 𝓵 | 𝓶 | 𝓷 | | 𝓸 | 𝓹 | 𝓺 | 𝓻 | 𝓼 | 𝓽 |   | | 𝓾 | 𝓿 | 𝔀 | 𝔁 | 𝔂 | 𝔃 |   |

#200 sdkman 的使用

2017-04-17
# 安装
curl -s "https://get.sdkman.io" | bash

# 查看当前版本
sdk version

# 更新到最新版本
sdk update

# 列出可用 Java 版本
sdk list java

# 安装指定 Java 版本
sdk install java 11.0.16-albba

# 切换到另一个已安装 Java 版本
sdk use java 8.8.9-albba

除了管理 Java 版本,还可以用来管理其他开发工具,比如 Gradle,Maven,Scale 等。

#199 惊群与 accept_mutex

2017-04-16

多个进程共享一个 bind socket 时,会发生这样的情况:一个连接进来,所有相关进程都从睡眠状态被唤醒。然后其中一个进程拿到这个连接,其他进程就继续 sleep。
这种现象就叫做惊群。好处就是如果来了大量连接的话,大家一起干活(可能可以算是正向的影响),坏处就是,如果只来了少量的连接,白白吵醒一众进程,消耗 CPU 资源。
这个好处像没有一样,资源的浪费确是实打实的。

注意,新版本 Nginx 上不需要互斥锁,默认没有惊群现象。

accept_mutex

Syntax: accept_mutex on | off;
Default: accept_mutex off;
Context: events

If accept_mutex is enabled, worker processes will accept new connections by turn. Otherwise, all worker processes will be notified about new connections, and if volume of new connections is low, some of the worker processes may just waste system resources.

There is no need to enable accept_mutex on systems that support the EPOLLEXCLUSIVE flag (1.11.3) or when using reuseport.

Prior to version 1.11.3, the default value was on.

如果 accept_mutex 启用,worker 进程会按顺序接受连接。否则,所有 worker 进程都会收到新连接通知,也就是惊群了。

1.11.3 之前,accept_mutex 默认是 on。为什么现在默认 off 了呢?
文档有写, 因为 1.11.3 之后,支持 EPOLLEXCLUSIVE 标记的平台,默认就不会发生惊群了,不需要 accept_mutex。

此外:如果使用了 TCP reuseport(需要 Linux 内核支持), 就不需要启用 accept_mutex。

如果 reuseport 参考: 2019/02/05, reuseport: 端口复用
PS: 2015/05 发布的 1.9.1 及之后版本,Nginx 引入了 reuseport 指令。

accept_mutex_delay

Syntax: accept_mutex_delay time;
Default: accept_mutex_delay 500ms;
Context: events

If accept_mutex is enabled, specifies the maximum time during which a worker process will try to restart accepting new connections if another worker process is currently accepting new connections.

如果 accept_mutex 启用,那么这个参数指定了 worker 进程申请 accept_mutex 锁失败之后的 sleep 时长。

也就是说如果出现竞争,没有抢到锁的进程就要睡眠一段时间,避免无效的反复申请锁。不过,感觉默认的这个 0.5 秒感觉有点长,50ms 应该就够了吧。

实现

参考:深入浅出 Linux 惊群:现象、原因和解决方案

附: 睡眠队列(等待队列)

#198 ASCII 码表

2017-04-15
Bin Oct Dec Hex 缩写/字符 解释
0000 0000 000 0 00 NUL(null) 空字符
0000 0001 001 1 01 SOH(start of headline) 标题开始
0000 0010 002 2 02 STX (start of text) 正文开始
0000 0011 003 3 03 ETX (end of text) 正文结束
0000 0100 004 4 04 EOT (end of transmission) 传输结束
0000 0101 005 5 05 ENQ (enquiry) 请求
0000 0110 006 6 06 ACK (acknowledge) 收到通知
0000 0111 007 7 07 BEL (bell) 响铃
0000 1000 010 8 08 BS (backspace) 退格
0000 1001 011 9 09 HT (horizontal tab) 水平制表符
0000 1010 012 10 0A LF (NL line feed, new line) 换行键
0000 1011 013 11 0B VT (vertical tab) 垂直制表符
0000 1100 014 12 0C FF (NP form feed, new page) 换页键
0000 1101 015 13 0D CR (carriage return) 回车键
0000 1110 016 14 0E SO (shift out) 不用切换
0000 1111 017 15 0F SI (shift in) 启用切换
0001 0000 020 16 10 DLE (data link escape) 数据链路转义
0001 0001 021 17 11 DC1 (device control 1) 设备控制 1
0001 0010 022 18 12 DC2 (device control 2) 设备控制 2
0001 0011 023 19 13 DC3 (device control 3) 设备控制 3
0001 0100 024 20 14 DC4 (device control 4) 设备控制 4
0001 0101 025 21 15 NAK (negative acknowledge) 拒绝接收
0001 0110 026 22 16 SYN (synchronous idle) 同步空闲
0001 0111 027 23 17 ETB (end of trans. block) 结束传输块
0001 1000 030 24 18 CAN (cancel) 取消
0001 1001 031 25 19 EM (end of medium) 媒介结束
0001 1010 032 26 1A SUB (substitute) 代替
0001 1011 033 27 1B ESC (escape) 换码(溢出)
0001 1100 034 28 1C FS (file separator) 文件分隔符
0001 1101 035 29 1D GS (group separator) 分组符
0001 1110 036 30 1E RS (record separator) 记录分隔符
0001 1111 037 31 1F US (unit separator) 单元分隔符
0010 0000 040 32 20 (space) 空格
0010 0001 041 33 21 ! 叹号
0010 0010 042 34 22 " 双引号
0010 0011 043 35 23 # 井号
0010 0100 044 36 24 $ 美元符
0010 0101 045 37 25 % 百分号
0010 0110 046 38 26 & 和号
0010 0111 047 39 27 ' 闭单引号
0010 1000 050 40 28 ( 开括号
0010 1001 051 41 29 ) 闭括号
0010 1010 052 42 2A * 星号
0010 1011 053 43 2B + 加号
0010 1100 054 44 2C , 逗号
0010 1101 055 45 2D - 减号/破折号
0010 1110 056 46 2E . 句号
0010 1111 057 47 2F / 斜杠
0011 0000 060 48 30 0 数字 0
0011 0001 061 49 31 1 数字 1
0011 0010 062 50 32 2 数字 2
0011 0011 063 51 33 3 数字 3
0011 0100 064 52 34 4 数字 4
0011 0101 065 53 35 5 数字 5
0011 0110 066 54 36 6 数字 6
0011 0111 067 55 37 7 数字 7
0011 1000 070 56 38 8 数字 8
0011 1001 071 57 39 9 数字 9
0011 1010 072 58 3A : 冒号
0011 1011 073 59 3B ; 分号
0011 1100 074 60 3C < 小于
0011 1101 075 61 3D = 等号
0011 1110 076 62 3E > 大于
0011 1111 077 63 3F ? 问号
0100 0000 100 64 40 @ 电子邮件符号
0100 0001 101 65 41 A 大写字母 A
0100 0010 102 66 42 B 大写字母 B
0100 0011 103 67 43 C 大写字母 C
0100 0100 104 68 44 D 大写字母 D
0100 0101 105 69 45 E 大写字母 E
0100 0110 106 70 46 F 大写字母 F
0100 0111 107 71 47 G 大写字母 G
0100 1000 110 72 48 H 大写字母 H
0100 1001 111 73 49 I 大写字母 I
0100 1010 112 74 4A J 大写字母 J
0100 1011 113 75 4B K 大写字母 K
0100 1100 114 76 4C L 大写字母 L
0100 1101 115 77 4D M 大写字母 M
0100 1110 116 78 4E N 大写字母 N
0100 1111 117 79 4F O 大写字母 O
0101 0000 120 80 50 P 大写字母 P
0101 0001 121 81 51 Q 大写字母 Q
0101 0010 122 82 52 R 大写字母 R
0101 0011 123 83 53 S 大写字母 S
0101 0100 124 84 54 T 大写字母 T
0101 0101 125 85 55 U 大写字母 U
0101 0110 126 86 56 V 大写字母 V
0101 0111 127 87 57 W 大写字母 W
0101 1000 130 88 58 X 大写字母 X
0101 1001 131 89 59 Y 大写字母 Y
0101 1010 132 90 5A Z 大写字母 Z
0101 1011 133 91 5B [ 开方括号
0101 1100 134 92 5C \ 反斜杠
0101 1101 135 93 5D ] 闭方括号
0101 1110 136 94 5E ^ 脱字符
0101 1111 137 95 5F _ 下划线
0110 0000 140 96 60 ` 开单引号
0110 0001 141 97 61 a 小写字母 a
0110 0010 142 98 62 b 小写字母 b
0110 0011 143 99 63 c 小写字母 c
0110 0100 144 100 64 d 小写字母 d
0110 0101 145 101 65 e 小写字母 e
0110 0110 146 102 66 f 小写字母 f
0110 0111 147 103 67 g 小写字母 g
0110 1000 150 104 68 h 小写字母 h
0110 1001 151 105 69 i 小写字母 i
0110 1010 152 106 6A j 小写字母 j
0110 1011 153 107 6B k 小写字母 k
0110 1100 154 108 6C l 小写字母 l
0110 1101 155 109 6D m 小写字母 m
0110 1110 156 110 6E n 小写字母 n
0110 1111 157 111 6F o 小写字母 o
0111 0000 160 112 70 p 小写字母 p
0111 0001 161 113 71 q 小写字母 q
0111 0010 162 114 72 r 小写字母 r
0111 0011 163 115 73 s 小写字母 s
0111 0100 164 116 74 t 小写字母 t
0111 0101 165 117 75 u 小写字母 u
0111 0110 166 118 76 v 小写字母 v
0111 0111 167 119 77 w 小写字母 w
0111 1000 170 120 78 x 小写字母 x
0111 1001 171 121 79 y 小写字母 y
0111 1010 172 122 7A z 小写字母 z
0111 1011 173 123 7B { 开花括号
0111 1100 174 124 7C | 垂线
0111 1101 175 125 7D } 闭花括号
0111 1110 176 126 7E ~ 波浪号
0111 1111 177 127 7F DEL (delete) 删除

#197 virtualenv 的使用

2017-04-04

安装

pip install virtualenvwrapper

把以下内容加入 ~/.bashrc~/.zshrc 等 Shell 初始化代码中:

export WORKON_HOME="~/.virtualenvs"
export VIRTUALENVWRAPPER_PYTHON="/usr/bin/python3"
source virtualenvwrapper.sh
  1. source virtualenvwrapper_lazy.sh 也可以,只是不会自动补全了。
  2. 这两个脚本默认会放到 /usr/local/bin/ 目录,或者 ~/.local/bin/ 目录,所以可以直接 source 加载。

简单使用

# 创建并进入虚拟环境
mkvirtualenv myproject

# 进入指定虚拟环境
workon myproject

# 退出虚拟环境
deactivate

# 列出虚拟环境
workon

# 删除虚拟环境
rmvirtualenv myproject

其他命令

virtualenvwrapper

virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv
tool.  The extensions include wrappers for creating and deleting
virtual environments and otherwise managing your development workflow,
making it easier to work on more than one project at a time without
introducing conflicts in their dependencies.

For more information please refer to the documentation:

    http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html

Commands available:

  add2virtualenv: add directory to the import path

  allvirtualenv: run a command in all virtualenvs

  cdproject: change directory to the active project

  cdsitepackages: change to the site-packages directory

  cdvirtualenv: change to the $VIRTUAL_ENV directory

  cpvirtualenv: duplicate the named virtualenv to make a new one

  lssitepackages: list contents of the site-packages directory

  lsvirtualenv: list virtualenvs

  mkproject: create a new project directory and its associated virtualenv

  mktmpenv: create a temporary virtualenv

  mkvirtualenv: Create a new virtualenv in $WORKON_HOME

  rmvirtualenv: Remove a virtualenv

  setvirtualenvproject: associate a project directory with a virtualenv

  showvirtualenv: show details of a single virtualenv

  toggleglobalsitepackages: turn access to global site-packages on/off

  virtualenvwrapper: show this help message

  wipeenv: remove all packages installed in the current virtualenv

  workon: list or change working virtualenvs

#196 MySQL 技巧

2017-04-02

安装之后初始化

忘记密码

改密码

查看版本

  1. mysql 命令行登录的时候显示的欢迎信息
  2. select @@version
  3. select version()
  4. SHOW VARIABLES WHERE variable_name LIKE 'version%';
  5. status

导出数据为 JSON

导出数据为 CSV

#195 Linux 文件属性(stat)

2017-03-29
  • atime:access time,访问时间
  • mtime:change time,修改时间,内容修改
  • ctime:modify time,变更时间,内容修改,或者元数据修改
  • crtime:create time / birth time,创建时间,部分文件系统支持(Ext4,btrfs,xfs)
[root@localhost]# stat abc.txt
  File: `abc.txt'
  Size: 11214           Blocks: 24         IO Block: 4096   regular file
Device: fc10h/64528d    Inode: 3153180     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-09 19:35:42.321279859 +0800
Modify: 2016-07-09 19:35:42.321279859 +0800
Change: 2016-07-09 19:35:42.321279859 +0800
[root@localhost]# cat abc.txt > /dev/null
[root@localhost]# stat abc.txt
  File: `abc.txt'
  Size: 11214           Blocks: 24         IO Block: 4096   regular file
Device: fc10h/64528d    Inode: 3153180     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-09 19:35:42.321279859 +0800
Modify: 2016-07-09 19:35:42.321279859 +0800
Change: 2016-07-09 19:35:42.321279859 +0800
[root@localhost]# mount
/dev/vda1 on / type ext4 (rw,noatime,errors=remount-ro)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/vdb on /data type ext4 (rw,noatime)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

重点是 noatime 选项,表示文件系统忽略 atime 的变更,因为频繁的 atime 变更对性能有影响。

#194 Linux 常用压缩命令 CheatSheet

2017-03-22

rar

rar a xxx.rar files...
rar e xxx.rar ./

rar a -p123456 xxx.rar files...
rar e -p123456 xxx.rar ./

rar a -v20m xxx.rar files...

tar

常用的参数说明(grep "^ \-[a-zA-Z0-9]" <(tar --help) | sort):

  • vf 分别表示输出详细信息、指定压缩包名称
  • x 解压
  • c 压缩
  • t 列出压缩包里面的文件列表
  • 压缩文件类型(Linux 下最常用的两种):
  • z:gz
  • j:bz2
# To extract an uncompressed archive:
tar -xvf /path/to/foo.tar

# To create an uncompressed archive:
tar -cvf /path/to/foo.tar /path/to/foo/

# To extract a .gz archive:
tar -xzvf /path/to/foo.tgz

# To create a .gz archive:
tar -czvf /path/to/foo.tgz /path/to/foo/

# To list the content of an .gz archive:
tar -ztvf /path/to/foo.tgz

# To extract a .bz2 archive:
tar -xjvf /path/to/foo.tgz

# To create a .bz2 archive:
tar -cjvf /path/to/foo.tgz /path/to/foo/

# To list the content of an .bz2 archive:
tar -jtvf /path/to/foo.tgz

# To create a .gz archive and exclude all jpg,gif,... from the tgz
tar czvf /path/to/foo.tgz --exclude=\*.{jpg,gif,png,wmv,flv,tar.gz,zip} /path/to/foo/

# To use parallel (multi-threaded) implementation of compression algorithms:
tar -z ... -> tar -Ipigz ...
tar -j ... -> tar -Ipbzip2 ...
tar -J ... -> tar -Ipixz ...

zip

# Create zip file
zip archive.zip file1 directory/

# To list, test and extract zip archives, see unzip
cheat unzip

# Extract archive
unzip archive.zip

# Windows 下创建的压缩包放到 Ubuntu 下解压可能会有编码问题
unzip archive.zip -O gbk

# Test integrity of archive
unzip -tq archive.zip

# List files and directories in a file
unzip -l archive.zip

7z

sudo apt install p7zip-full
dpkg -L p7zip-full p7zip | grep bin/
# /usr/bin/7z
# /usr/bin/7za
# /usr/bin/7zr
# /usr/bin/p7zip

文档中说是支持 7z,xz,tar,gz,zip,bz2,iso,rpm,deb 等等等等格式,不过没用过。

# 压缩
7z a  xxxx.7z files...
# 解压
7z e  xxxx.7z
# 查看文件列表
7z l  xxxx.7z

参考

  1. man
  2. --help
  3. cheat / tldr