ChrisKim
Do not go gentle into that good night.
颢天

Linux 使用常识备忘录 [UPD: 24.02.07]

虽说没有系统学习 Linux 的使用,但在运维博客和各种折腾的过程中也算是零零散散学习了许多 Linux 的知识。有些东西很常用但是总是忘记,在这里写篇博客,就当作自己的备忘录吧,可能会持续更新。

文章标题按撰写顺序排列,并非按重要程度排序,可点击左上角打开目录跳转查看。本文也非系统总结,而是备忘录。若想要系统学习,可以查询 linux-command: https://wangchujiang.com/linux-command/

目录结构

我总结一些我经常用的目录:

目录描述
/dev[Device] 外部设备目录。Linux 将外部设备视为文件,这个文件便是访问设备的端口。
/dev/null该设备可以接受无限的数据,接收的数据将会丢弃,好似黑洞一般。有时候命令可以用 > /dev/null 丢弃输出,有时候可以用 cat /dev/null > 清空一个 log 文件。
/etc[Editable Text Configuration] 系统级的配置文件。许多软件的配置文件都在这个目录内,可以进入该目录进行修改。
/home用户目录。例如该系统有一个用户 ChrisKim,那么该用户的目录就是 /home/ChrisKim/
/media可移动储存媒体的挂载点。比如 U 盘、CD 等等,插入后会自动挂载到此。
/mnt[Mount] 临时挂载点。挂载一些临时文件系统,这个一般是管理员手动挂载的。
/proc[Process] 系统内存的映射。可以通过该目录访问进程和内核信息,例如可以输出 /proc/uptime 来查看 Linux 的持续运行时长。
/usr[UNIX Software Resource] 系统的软件资源。安装的软件一般都会在这里。
/usr/bin存放着很多应用程序的二进制文件。
/usr/local给一般用户的程序安装目录,一般程序装在这里面。该目录里也有 bin、etc、sbin 等等目录。

下面是 Linux 文件目录结构的详细说明,供查阅:

DirectoryDescription
/Primary hierarchy root and root directory of the entire file system hierarchy.
/binEssential command binaries that need to be available in single user mode; for all users, e.g., cat, ls, cp.
/bootBoot loader files, e.g., kernels, initrd.
/devDevice files, e.g., /dev/null , /dev/disk0 , /dev/sda1 , /dev/tty , /dev/random.
/etcHost-specific system-wide configuration files.There has been controversy over the meaning of the name itself. In early versions of the UNIX Implementation Document from Bell labs, /etc is referred to as the etcetera directory, as this directory historically held everything that did not belong elsewhere (however, the FHS restricts /etc to static configuration files and may not contain binaries). Since the publication of early documentation, the directory name has been re-explained in various ways. Recent interpretations include acronyms such as “Editable Text Configuration” or “Extended Tool Chest.”
/etc/optConfiguration files for add-on packages that are stored in /opt .
/etc/sgmlConfiguration files, such as catalogs, for software that processes SGML.
/etc/X11Configuration files for the X Window System, version 11.
/etc/xmlConfiguration files, such as catalogs, for software that processes XML.
/homeUsers’ home directories, containing saved files, personal settings, etc.
/libLibraries essential for the binaries in /bin and /sbin .
/lib<qual>Alternative format essential libraries. Such directories are optional, but if they exist, they have some requirements.
/mediaMount points for removable media such as CD-ROMs (appeared in FHS-2.3 in 2004).
/mntTemporarily mounted filesystems.
/optOptional application software packages.
/procVirtual filesystem providing process and kernel information as files. In Linux, corresponds to a procfs mount. Generally automatically generated and populated by the system, on the fly.
/rootHome directory for the root user.
/runRun-time variable data: Information about the running system since last boot, e.g., currently logged-in users and running daemons. Files under this directory must be either removed or truncated at the beginning of the boot process, but this is not necessary on systems that provide this directory as a temporary filesystem (tmpfs).
/sbinEssential system binaries, e.g., fsck, init, route.
/srvSite-specific data served by this system, such as data and scripts for web servers, data offered by FTP servers, and repositories for version control systems (appeared in FHS-2.3 in 2004).
/sysContains information about devices, drivers, and some kernel features.
/tmpTemporary files (see also /var/tmp). Often not preserved between system reboots, and may be severely size restricted.
/usrSecondary hierarchy for read-only user data; contains the majority of (multi-)user utilities and applications.
/usr/binNon-essential command binaries (not needed in single user mode); for all users.
/usr/includeStandard include files.
/usr/libLibraries for the binaries in /usr/bin and /usr/sbin.
/usr/lib<qual>Alternative format libraries, e.g. /usr/lib32 for 32-bit libraries on a 64-bit machine (optional).
/usr/localTertiary hierarchy for local data, specific to this host. Typically has further subdirectories, e.g., bin , lib , share .
/usr/sbinNon-essential system binaries, e.g., daemons for various network-services.
/usr/shareArchitecture-independent (shared) data.
/usr/srcSource code, e.g., the kernel source code with its header files.
/usr/X11R6X Window System, Version 11, Release 6 (up to FHS-2.3, optional).
/varVariable files—files whose content is expected to continually change during normal operation of the system—such as logs, spool files, and temporary e-mail files.
/var/cacheApplication cache data. Such data are locally generated as a result of time-consuming I/O or calculation. The application must be able to regenerate or restore the data. The cached files can be deleted without loss of data.
/var/libState information. Persistent data modified by programs as they run, e.g., databases, packaging system metadata, etc.
/var/lockLock files. Files keeping track of resources currently in use.
/var/logLog files. Various logs.
/var/mailMailbox files. In some distributions, these files may be located in the deprecated /var/spool/mail .
/var/optVariable data from add-on packages that are stored in /opt .
/var/runRun-time variable data. This directory contains system information data describing the system since it was booted.In FHS 3.0, /var/run is replaced by /run ; a system should either continue to provide a /var/run directory, or provide a symbolic link from /var/run to /run , for backwards compatibility.
/var/spoolSpool for tasks waiting to be processed, e.g., print queues and outgoing mail queue.
/var/spool/mailDeprecated location for users’ mailboxes.
/var/tmpTemporary files to be preserved between reboots.

文件权限

身份

Linux 下文件和目录有三种身份:

  • [u, user] 拥有者:该文件或目录所属的用户。
  • [g, group] 拥有群组:该文件或目录所属的用户组。
  • [o, other] 其他人:除上面的两种用户外的其他用户。

调整身份

我们使用 chown 指令来调整文件或目录的身份。

调整所有者

chown chriskim test.txt:将文件所有者设置为 chriskim.

调整拥有群组

chown chriskim:group1 test.txt:将文件所有者设置为 chriskim,群组设置为 group1.

调整目录

只需给指令加上 -R 开关,即可递归地设置目录和目录内文件的身份。

chown -R chriskim:chriskim test/

权限

对于每个身份,分有四种权限:

  • [r, read] 读:读取文件内容的权限。
  • [w, write] 写:修改写入文件的权限。
  • [x, execute] 执行:执行该文件的权限。
  • [s, special] 特殊(我还没用到过,因此就先不写了)

注:管理员(超级用户)无视权限限制,拥有所有权限。

rwx 三种权限的开关共形成了 8 种状态如下,- 代表无该权限,字母代表有该权限:

字符形式八进制形式
0
–x1
-w-2
-wx3
r–4
r-x5
rw-6
rwx7

三种身份三种权限组成一个长度为 9 的字符串,我们在 ls 的时候可以看到。例如 rwxr-xr-x 代表:文件所有者可读、写、执行,用户组可读、执行,其他人可读、执行。

有些情况也用八进制数字表示,三种身份分别用一位表示。例如上面的例子转换为八进制表示为:755

调整权限

我们使用 chmod 指令来调整文件或目录的权限。

八进制形式

chmod 755 test.sh :将文件调整为 755 权限。

字符形式

chmod +x test.sh :给三种身份都加上执行权限。
chmod -w test.sh :给三种身份都去掉写入权限。
chmod o-x test.sh :给 o 身份去掉执行权限。

调整目录

只需给指令加上 -R 开关,即可递归地设置目录和目录内文件的权限。

chmod -R 755 test/

用户管理

新建用户

新建用户并创建用户home目录(/home/[Username])

useradd -m [Username]

删除用户

完全删除用户

userdel -r [Username]

设置密码

输入命令后重复输入两次新密码

passwd [Username]

切换用户

su - [Username]

指定用户执行命令

通常对于无登录权限的用户,使用这个来执行命令

sudo -u [Username] [Command]

常用指令操作

压缩和解压

压缩和解压使用 tar (Tape Archive) 指令,后面有非常丰富的参数开关。这是刚开始用 Linux 最头疼的一点,后面的参数记不下来,每次都得百度(不过现在还真记住了…)

tar [参数] [压缩包] [文件或文件夹]

  • c: 创建归档
  • x: 提取归档
  • f: 创建给定名字的归档
  • t: 展示或列举出归档文件中的文件
  • u: 归档,打包已存在的归档文件
  • v: 展示冗长的信息
  • A: 拼接归档文件
  • z: 告诉tar命令使用gzip来创建文件
  • j: 使用tbzip来归档文件
  • W: 验证归档文件
  • r: 向一个已生成的.tar文件中更新或添加文件或文件夹

来源:https://www.jianshu.com/p/c5b70af4412d

打包和解包

tar cvf file.tar *.txt:将 .txt 文件打包为 file.tar
tar xvf file.tar:解包 file.tar

压缩和解压

将上方参数加上 z 即可调用 gzip 来进行压缩打包。

tar cvzf file.tar.gz *.txt:将 .txt 文件压缩为 file.tar.gz
tar xvzf file.tar.gz:解压 file.tar.gz

这是最常用的方式,大可以将上面的两个参数背下来,参数顺序没有关系,可以调成最顺口的方式记忆。

多线程压缩和解压

gzip 只支持单线程,对于有多核多线程的服务器来说,无法利用全部性能,此时我们可以替换为支持多线程的 pigz 来进行压缩和解压。

首先得安装 pigz:yum install pigzapt install pigz

然后指定使用 pigz 来进行压缩:添加参数 --use-compress-program=pigz,可简写为 --use=pigz

完整指令(注意使用 p 参数):

tar --use=pigz -cvpf file.tar.gz *.txt:将 .txt 文件压缩为 file.tar.gz
tar --use=pigz -xvpf file.tar.gz:解压 file.tar.gz

清空文件

运维过程经常有巨大的 log 文件需要清空,直接删除是很不妥的操作,需要使用指令来清空。

> test.log :重定向一个 null 到该文件。
cat /dev/null > test.log :将 /dev/null(上文提到过)重定向到该文件。
cp /dev/null test.log :将 /dev/null 复制到该文件。

以上三种就够用了,随便选一个喜欢的用就行。

输入输出重定向

Linux 中标准的输入设备默认指的是键盘,标准的输出设备默认指的是显示器。通过输入输出重定向,我们可以改变输入输出设备,最常用的是将文件作为输入输出。

输入重定向

[程序] < [文件]:将指定文件作为程序的 stdin .

[程序] << [结束标志]:从键盘中读入,直到读到结束标志结束。这些读入的数据作为程序的 stdin .

输出重定向

[程序] > [文件]:将程序的 stdout 覆盖写入到指定文件。

[程序] 2> [文件]:将程序的 stderr 覆盖写入到指定文件。

[程序] &> [文件]:将程序的 stdout 和 stderr 覆盖写入到指定文件。

[程序] >> [文件]:将程序的 stdout 尾接写入到指定文件。

[程序] 2>> [文件]:将程序的 stderr 尾接写入到指定文件。

[程序] &>> [文件]:将程序的 stdout 和 stderr 尾接写入到指定文件。

结合使用

以上两种重定向可以同时使用,如 [程序] < [文件A] > [文件B],即文件 A 作为 stdin,文件 B 作为 stdout.

管道操作

Linux 使用 | 代表管道操作,它将前者的标准输出传输到后者的标准输入,就像一个管道一样。

例如 ls -al | grep chriskim,管道操作符将 ls -al 的输出传输到 grep chriskim 的输入中,最后显示 grep chriskim 的输出。

常用组合方法

more

该命令用于多页显示内容,例如可以用 more test.txt 分页显示 test.txt 的内容。这在内容非常长的时候很实用。我们可以组合为:ps -ef | more,将非常长的进程用 more 查看。

grep

该命令用于匹配字符,具体有很多用法细节。我们可以组合为:ls -al | grep chriskim,这个命令可以查找该目录下带有 chriskim 字符的文件。

查看指令位置

指令肯定对应了一个程序,如果要查看程序存放在哪,可以使用 whereiswhich 指令。

whereis <command>: 定位可执行文件、源代码文件、帮助文件在文件系统中的位置。

which <command>: 在 PATH 环境变量的目录中查找文件。

两个指令略有区别,不过都能达到查询命令位置的效果。

Linux 主流发行版

严格来说 Linux 是一种开源的操作系统内核,基于这种内核,Linux 有非常多的发行版,让人眼花缭乱。我们简单安装派系分类一下主流发行版。

Red Hat 派系

  • RHEL: https://www.redhat.com/en/technologies/linux-platforms/enterprise-linux
  • CentOS: https://www.centos.org/
  • Fedora: https://getfedora.org/

Debian 派系

  • Debian: https://www.debian.org/
  • Ubuntu: https://ubuntu.com/
  • Kali Linux: https://www.kali.org/

Arch Linux 派系

  • Arch Linux: https://archlinux.org/
  • Manjaro: https://manjaro.org/

Slackware Linux 派系

  • Slackware Linux: http://www.slackware.com/
  • SUSE: https://www.suse.com/

这些只是比较常见的,详细的发行版整理可以见这篇文章:

https://blog.csdn.net/liaowenxiong/article/details/116082478

软件管理

使用软件包管理器

Linux 不同发行版派系有不同的软件包管理器,软件包管理器的分发方式一般都是编译好的二进制文件,下载后无需编译可以直接运行,因此是非常省时方便的软件安装方式。

软件包管理器的种类

不同 Linux 发行版派系有着不同的软件包管理器,大概如下:

软件包管理器常用指令系统
RPMrpm, yumRHEL, CentOS
DPKGdpkg, aptDebain, Ubuntu
PacmanpacmanArch Linux, Manjaro
DNFdnfFedora
ZypperzypperSUSE

yum 常用指令

我目前接触的最多的是 yum,因此记录下它的常用指令。

# 来源: https://www.jianshu.com/p/64598784d7a3

# 1. 安装
yum install             # 全部安装
yum install package1    # 仅安装指定的安装包 package1
yum groupinsall group1  # 仅安装程序组 group1

# 2. 更新和升级
yum update              # 全部更新
yum update package1     # 仅更新指定程序包 package1
yum check-update        # 检查可更新的程序
yum upgrade package1    # 仅升级指定程序包 package1
yum groupupdate group1  # 升级程序组 group1

# 3. 查找和显示
yum info package1       # 显示安装包信息 package1
yum list                # 显示所有已经安装和可以安装的程序包
yum list installed      # 显示已经安装的程序包
yum list package1       # 显示指定程序包安装情况 package1
yum groupinfo group1    # 显示程序组 group1 信息
yum search string       # 根据关键字 string 查找安装包

# 4. 删除程序
yum remove package1     # 删除软件包 page1
yum groupremove group1  # 删除程序组 group1
yum deplist package1    # 查看程序 package1 依赖情况

# 5. 清除缓存
yum clean packages        # 清除缓存目录下的软件包
yum clean headers         # 清除缓存目录下的 headers
yum clean oldheaders      # 清除缓存目录下旧的 headers
yum clean, yum clean all  # 清除缓存目录下的软件包及旧的 headers

从源码编译安装

这可能是 Linux 的特别之处,由于开源性,你可以将软件源码下载到你的电脑,然后在你的电脑上编译该软件,得到可运行的二进制程序。由于有些程序比较庞大,编译安装所需时间可能需要很久,也比较考验计算机的性能,在有些小内存云服务器上还有可能编译失败。

但是,编译安装的存在一定是有原因的。在编译过程中,有些程序会根据你的 CPU 进行优化,使在自己电脑上编译的程序效率比别人编译的更快。当然如果对程序效率的要求不是太高,直接下载二进制还是最方便的选择。

本文提到的编译安装都是 C 语言编译(至于为什么开源软件大都是 C 语言而不是 C++ 编写的,我也有同样的疑问,可参见 https://www.zhihu.com/question/20080552 的解答)

编译流程

C 语言编译安装有三个步骤:

  • 配置 (configure)
  • 编译 (make)
  • 安装 (make install)

当然在开始编译之前最好确保自己有 C 语言编译环境,根据具体程序需求可能不同。

配置

下载源码后解压,文件夹内应该有一个 configure 文件。configure 文件是一个可执行的脚本文件,它有很多参数选项可以指定。

通过指定这些参数,可以控制程序的编译和安装细节,这就像 Windows 上的程序安装器,选择要安装那些组件,要安装在哪个目录一样。

每个程序拥有的参数不同,可以使用命令 ./configure --help 查看详细的参数选项列表。

最常用的参数便是 --prefix,它指定了程序安装的位置。不配置该选项,程序默认安装在 /usr/local 内,即:

  • 可执行文件 -> /usr/local/bin
  • 库文件 -> /usr/local/lib
  • 配置文件 -> /usr/local/etc
  • 其他文件 -> /usr /local/share

若指定了该参数,程序就会安装到指定的目录,例如使用 --prefix=/root/python,那么:

  • 可执行文件 -> /root/python/bin
  • 库文件 -> /root/python/lib
  • 配置文件 -> /root/python/etc
  • 其他文件 -> /root/python/share

这么做的好处就是,该程序所有文件都会存在你指定的目录,不会与其他程序混在一起,如果想要卸载,直接删掉该目录即可不会有残余,当然如果你想安装在默认目录其实也没有任何问题。

当然不建议把目录改的太离谱导致你忘记程序安哪去了,建议还是遵守上文提到的目录结构,在 /usr/local 下创建个文件夹,把程序装在里面。

编译

在执行完 ./configure 后,脚本会自动生成 Makefile 文件,该文件描述了整个软件的编译、连接等规则,使整个软件可以全自动编译。

输入 make 指令,就可以根据生成的 Makefile 文件开始程序的编译。该过程最耗时,需要耐心等待编译完成。

安装

在编译完成后,二进制文件生成在源代码目录下,还没有复制到前面指定的安装目录。

输入 make install,将会自动把相关文件复制到对应目录下。

后续操作

后续可能需要配置从 /usr/bin 到程序的软链接,或者使用 PATH 环境变量。环境变量跟 Windows 上的原理一样,可以让命令行找到该程序。

若要卸载程序,可在源代码目录执行 make uninstall(若提供了卸载选项的话)

交换分区

创建文件型 SWAP

创建文件

指令结尾数字即 SWAP 大小,2048 即 2GB,一般 2~4G 就够用了。

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

设置权限

sudo chmod 600 /swapfile

设置文件为交换分区

sudo mkswap /swapfile

启用交换分区

sudo swapon /swapfile

启用自动挂载

echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

检查状态

swapon -s
本文链接:https://www.zouht.com/2899.html
本文使用:CC BY-NC-SA 4.0 许可
#
首页      随笔      Linux 使用常识备忘录 [UPD: 24.02.07]

回复 ChrisKim 取消回复

textsms
account_circle
email

  • eriktse

    每次用linux都要百度查命令 [大哭]

    2年前 回复
    • ChrisKim博主

      @eriktse: 确实,不过常用命令查的多了就记下来了,不常用的命令要用的时候再查,哈哈哈 [doge]

      2年前 回复
  • Abyss

    很棒,不过对于一个不怎么用linux(最多就管理服务器)的人来说,我最熟的也就ls,cd,sudo su,vim,touch,rm,之类的了,真正遇到不懂的还得上网查,平时也就掌握一些基本的操作命令和vim的一些基础就好了

    2年前 回复

颢天

Linux 使用常识备忘录 [UPD: 24.02.07]
虽说没有系统学习 Linux 的使用,但在运维博客和各种折腾的过程中也算是零零散散学习了许多 Linux 的知识。有些东西很常用但是总是忘记,在这里写篇博客,就当作自己的备忘录吧,可能会持…
扫描二维码继续阅读
2022-06-04