【好文翻译】最重要的但是没人教你的 linux 命令

88 阅读3分钟

原文: medium.com/@halil_tek/…

无论你是系统管理员、开发工程师、运维、安全工程师等等,高效地使用 Linux 以及工具是最基础的必备技能。Linux 系统是世界上大多数服务器和应用的基石。

47%的专业工程师都在使用基于 Linux 的操作提供
--Statista

linux-c-002.png

图片来自于truelist.co数据来自于Statista

在过去几个月里,我看过很多关于“20 个你必须知道的 Linux 命令”、“Linux 入门教程”之类的文章。问题在于,这些文章的单篇内容针对的是初学者,讲述的内容比较初级,类似ls或者echo之类的命令使用。我相信我的大部分粉丝对这些基础的 Linux 命令已经非常熟悉了。因此,这篇文章将会讲述一些不一样的,高级实用的东西。

我将会列出我在日常工作中常用的命令清单。这份清单可能会超出初学者的知识范畴,它将会更关注于那些帮助你推进工作、更加有效重要的命令,并以此帮助你管理你的 Linux 系统。

这篇文章将分为两个部分:

  • Linux 工具。基础的 Linux 工具及其最佳实践。
  • 高级命令。高级命令在一些特殊场景中将会非常有用。

Linux 工具

实用工具

rsync

用于复制文件或目录到目标地址,类似cp命令,不同的是,他允许复制到远程地址并提供实时进度条,因此它常用于数据备份。

# Example Usage
$ rsync -vap --ignore-existing <source_file> <destination_file>#  Key flags:
v = verbrose, r = recursive, p = preserve permissions, g = group, o = owner, a = archive, --progress = progresss bar

mkpasswd

mkpasswd是一个简单又实用的命令,它可以生成一个指定长度的复杂随机密码。

$ mkpasswd -l 8
> iwF1g2Lo

screen

screen 是一个全屏窗口管理器,它会创建一个可运行 shell 解释器的独立窗口,并且允许在一个会话中运行多个窗口。当你运行一个非常长的远程任务,担心你的 SSH 会话会断掉而功亏一篑时,screen 将会非常有用。即使 SSH 断开连接,窗口对你不可见,screen 也会继续运行,同样也会继续运行你之前的命令。

# Example Usage
$ screen # Start a screen session
$ screen -ls # List running services
$ screen -r # Attach to session

ldapsearch

如果你经常和 LDAP 数据库打交道,那么ldapsearch命令是必须要会的。它可以链接到 LDAP 服务器,你可以在数据库中搜索、查找和调试。

# Example Usage
$ ldapsearch -x -W -D <username | less# Key Flags
-x = simple authentication, -W = prompt for password, -D = Use distinguished binddn name to bind to LDAP directory

监听工具

uptime

uptime 返回一些系统指标,包括服务器运行时间、当前时间、用户数量以及内存使用平均值。如果你的服务器出现了问题,这个工具通常是排查问题的第一入口。
‘w’ ——就一个字母,非常简单。这个命令非常巧妙,它是uptime命令和who命令的组合,并可以使他们串行使用。

wall

对于任何系统管理员来说,wall 是一个非常实用的命令。他允许你想登录到同一个系统的所有用户的终端发送消息。这对于做系统公告非常有用。

$ wall "Maintenance scheduled for 13:30"Broadcast message from Joel@localhost: Maintenance scheduled for 13:30

top

展示一个自动刷新的 CPU 使用指标和关键内存使用的进程列表。

$ top

ncdu

ncdu 命令可以快速方便地查看硬盘使用情况。你可以使用它快速查看那些目录的硬盘使用率最高。

$ ncdu

lsof

lsof 命名只有一个作用:列出当前进程中打开的所有文件。当遇到一些问题,提示一些文件被占用,这个命令可以快速的识别是哪些文件被什么进程占用。

lsof

网络工具

netcat

nc命令主要用于端口扫描,实际上它能提供的功能不止于此,是系统管理员必备的一个命令。netcat 支持端口扫描、文件复制、端口转发、服务器代理以及服务器托管。可以说它的用途极其广泛。

# Example Usage:
$ nc -vz <host> <port> # Checks the connection between two hosts on a given port
$ nc -l 8080 | nc <host> 80 # Creating a proxy server

netcat 命令可以高度定制,我无法在一篇文章中列出所有功能,如果你想知道更多内容,我会贴出我最喜欢的关于 netcat 的 Medium 文章, 这些文章由@sahityamaruvada 撰写。

7 个 Netcat 的基础使用 > Netcat 指南—TCP/IP 中的瑞士军刀

netstat

netstat 会返回各种网络细节,如路由表、网络连接、成员信息、统计状态和标识等等。

# Example Usage
$ netstat -a # List all network ports
$ netstat -tlpn # List all listening ports# Key Flags
-s = Show statistics, -v = verbrose, -r = show routing tables, -i display interface table, -g = show group memeberships

nslookup

常用于获取网络服务器信息或本地网络信息。它通过查询 DNS 来找到服务器信息。网络调试中会很实用。

# Example Usage
$ nslookup medium.com/tags/devops# Key Flags
-port = Change port number for connection, -type = Change type of query. -domain = Sets search list to name

tcpdump

用于捕获和分析你的系统流量。它是一个强大的多功能工具,专攻于调试和排查网络问题。同样它也可以用做安全工具。

# Example Usage
$ tcpdump
$ tcpdump -i <interface> <ipaddress or hostname> <port>

高级命令

美化 API 响应

在终端中阅读 API 返回的 JSON 数据是一件非常烦的事,就像下面展示的一样,即使一小块数据,在命令行中都是一团无序的数据,让人难以阅读。

$ cat test.json
{"title":"Person","type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"age":{"description":"Age in years","type":"integer","minimum":0}},"required":["firstName","lastName"]}

幸运的是,python 有一个好的解决方案。把内容输入到 python 中就可以调用 python 的 JSON 模块来格式化输出。

$ cat test.json | python -m json.tool
{
    "properties": {
        "age": {
            "description": "Age in years",
            "minimum": 0,
            "type": "integer"
        },
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        }
    },
    "required": [
        "firstName",
        "lastName"
    ],
    "title": "Person",
    "type": "object"
}

当然,你也可以使用其他工具,比如JQ,来美化你的JSON数据。

使用apt来搜索可用包

$ apt-cache seach <keyword>

列出任意两个命令的区别

# Example usage of comparing output of two ls commands
$ diff -u <(ls -l /directory/) <(ls -l /directory/) | colordiff

将一个unix时间戳转换为可阅读的格式

# Convert Unix timestamp to human readable
$ date -d 1656685875
Fri, 01 Jul 2022 14:31:15 +0000# Current time as UNIX timestamp

压缩git提交信息

$ git log # See how many commits you've made
$ git rebase -i HEAD~x 
# x = number of commits you've made
# Make changes on the text editor, keeping the last commit as pick and changing the rest to sqash
# Edit the commit messages as you'd like, preferbly removing ones from previous commits
$ git push --force-with-lease

列出所有系统服务

$ systemctl -l -t service | less