我将开始使用的 5 个 Linux 命令
Linux 系统管理最好被描述为自动化 Linux 系统管理。有时这些工具是您自己的。其他时候,实用程序会与您团队中的其他人共享、发货或以服务形式提供。这源于对 UNIX 哲学最随意的理解:使用小型、专门构建的工具,并以新颖、强大和意想不到的方式将它们组合在一起。这些可能是复杂的命令,它们会变成一行脚本,也可能变成多行脚本。有些脚本您会保留并分享,有些您会在下次喝咖啡时忘记。
[ 你可能还喜欢: 使用 Bash 实现自动化]
每当我抛出诸如UNIX 哲学之类的短语时,我都会想起那个时代,那时并非所有东西都是 Linux,甚至所有 Linux 系统都可能是同一个发行版。我对未来唯一的信心是,未来会有所不同。因此,一些历史性的可移植性技巧(向前和向后)是应该的,尤其是当它们不花费你任何钱的时候。
这就是说,我了解的标准工具越多,我的一次性命令就越强大,我编写它们的速度就越快,我的客户的实际问题得到解决的速度也就越快。
我几乎每个月都会在学习别人的命令行风格时学到一些新技术或技巧。疫情时期和远程工作让这变得更加困难,所以我要感谢Ken Hess向我介绍或提醒我五个我不太了解的命令。
拱门
在构建脚本中,生成描述平台、构建环境、yum repo 路径等的路径是很常见的。通常,我会使用它uname -p
来获取处理器类型,但arch
输入速度更快,而且可以自行记录。谢谢!
GitHub 上有许多以下形式的代码:
ARCH=$(uname -m)
所有这些都可以替换为:
ARCH=$(arch)
2. 阿帕网
就在上周,我正在编写一个 API 来创建 PTR 记录。它扮演着一个相当知名的 IPAM 的 Ansible 角色,但如果我必须使用通用 DNS 工具进行批量导入,我会使用nsupdate
。这个命令非常方便。快速代码示例:
[jwarnica@lappy ~]$ nsupdate <<EOF
update add `arpaname $ipaddr` 86400 IN PTR $(hostname -f).
send
EOF
当我可能有一堆新的 IoT 设备、虚拟机或一组笔记本电脑需要安装,并且已经在一个简单的 CSV 文件中拥有它们的主机名/IP 或以其他方式生成它们时,这可以很容易地在循环内使用。
3. bc
Bash can't do floating point math, so additional tooling is needed for scripting. With that said, bc --expression
got me thinking about how to script dc
, which is, of course, is possible.
I'll forget the details before I use it, but I'll remember forever this "ah-ha!" moment that dc
can be scripted.
While I don't want to sound like a high school math teacher, there are environments where one doesn't have their pocket supercomputer handy or even access to gnome-calculator. Or you simply want to save the five seconds and use the interactive dc
without taking your hands off the keyboard to use either the mouse or phone.
4. dumpkeys
One of my annual volunteer days is doing the results for a running relay race. After years of optimizing the dumbest thing that works, this is now mostly data entry of times. With a laptop on my lap sitting in a car, this is the opposite of ergonomic, but I could at least make it one-handed by mapping the numeric keypad . to a :. Within X11, xmodmap
was the tool for this mapping. While hardly a bowling ball, Canada continues to make advancements in human-computer interaction.
Saving 10 minutes a year is hardly a reason to learn a new tool (or is it? -- ed). Mapping keys could be the difference between being able to type and not. Maybe in most places in the world, the US-104 keyboard isn't standard, and/or the local language isn't unaccented English.
dumpkeys
has a close friend called loadkeys
. Together these make unusable consoles usable, and every Linux distro installer for decades has relied on them.
5. uname
Allegedly /proc
is considered by the Linux kernel developers to be stable, and I'll grant /proc/version
is likely very stable, but I don't quite trust them. I sometimes use systems that don't have the Linux-only /proc
, anyway. The uname
command, being part of POSIX, will work anywhere.
I might find this command useless now because I now know about arch
, but it's still a goto command just to verify where I am. And scripting examples abound. A random example from Fedora's /etc/profile.d/qt.sh
:
case `uname -m' in
x86_64 | ia64 | s390x | ppc64 | ppc64le)
QT_PREFIXES="/usr/lib64/qt-3.3 /usr/lib/qt-3.3" ;;
* )
QT_PREFIXES="/usr/lib/qt-3.3 /usr/lib64/qt-3.3" ;;
esac
[ Improve your skills managing and using SELinux with this helpful guide. ]
Wrap up
Are these tools revolutionary? Not at all. What they are is solid, ubiquitous foundations for the daily work of millions, and will be for decades more. That makes them utilities worth keeping in mind the next time you sit down at a Linux system.