Linux工具-技巧篇
参考文献
示例
1 | # 在远程机器上运行一段脚本。这条命令最大的好处就是不用把脚本拷到远程机器上 |
1 | # 显示ascii码表 |
1 | # 这行脚本能输出你最常用的十条命令 |
1 | # Marix的屏幕效果 |
1 | # 列出已知的编码 |
查看系统信息
显示硬件系统部件 - (SMBIOS / DMI)
1 | dmidecode -q |
显示机器的处理器架构
1 | root@xxxx:/home/xxx# arch |
Linux 查看内核版本
-
cat /proc/version
1
2[root@holelin ~]# cat /proc/version
Linux version 3.10.0-1127.19.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Aug 25 17:23:54 UTC 2020 -
uname -a
1
2[root@holelin ~]# uname -a
Linux holelin 3.10.0-1127.19.1.el7.x86_64 #1 SMP Tue Aug 25 17:23:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux -
dpkg --print-architecture
1
2[root@holelin ~]# dpkg --print-architecture
amd64
Linux查看系统版本命令
-
lsb_release -a
列出所有版本信息1
2
3
4
5
6
7[root@holelin ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.9.2009 (Core)
Release: 7.9.2009
Codename: Core -
cat /etc/redhat-release
1
2[root@holelin ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core) -
cat /etc/issue
只在centos6 看到系统版本号
查看Linux是64还是32位
1 | $ getconf LONG_BIT |
显示各种信息
1 | # 显示CPU info的信息 |
显示文件或目录其他信息
1 | $ stat -x readme.md # macOS 系统 |
控制台使用技巧
-
操作快捷键
-
Ctrl + r: 可以快速查找历史命令;
-
Ctrl + l: 可以清理控制台屏幕;
-
Ctrl + a \ Ctrl + e: 移动光标到命令行首\行尾;
-
Ctrl + w \ Ctrl + k: 删除光标之前\之后的内容。
-
-
VIM文件编辑快捷键
- 快捷键ZZ: 文件保存并退出。
-
进程操作快捷键
-
Ctrl + c: 强制终止程序的执行;
-
Ctrl + z: 挂起一个进程;
-
Ctrl + d: 终端中输入 exit 后回车。
-
-
Linux命令中快捷键(top)
-
Shift + p: 根据 CPU 使用率排序;
-
Shift + m: 根据内存占用排序。
-
1 | Ctrl + A 跳转到你当前编辑的命令行行首 |
Mac cli
技巧
1 | $ !! # 再一次执行上一条指令 |
修改Ubuntu20.04 IP
为静态IP
-
使用
ifconfig
查看网卡信息- 若提示没当前命令则执行
sudo apt install net-tools
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18holelin@minio-1:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.247.100 netmask 255.255.255.0 broadcast 192.168.247.255
inet6 fe80::20c:29ff:fee3:8221 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e3:82:21 txqueuelen 1000 (Ethernet)
RX packets 1557 bytes 569184 (569.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1565 bytes 186192 (186.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 252 bytes 25184 (25.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 252 bytes 25184 (25.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 - 若提示没当前命令则执行
-
进入
/etc/netplan
目录查看目录下文件,注:该目录下文件名称可能不太相同,但内容是一致的1
2
3
4
5holelin@minio-1:/etc/netplan$ ll
total 16
drwxr-xr-x 2 root root 4096 Jun 1 02:49 ./
drwxr-xr-x 94 root root 4096 Jun 1 02:24 ../
-rw-r--r-- 1 root root 239 Jun 1 02:49 00-installer-config.yaml -
修改
00-installer-config.yaml
1
2
3
4
5
6
7
8
9
10# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
dhcp4: false
addresses: [192.168.247.100/24]
gateway4: 192.168.247.2
nameservers:
addresses: [114.114.114.114, 114.114.115.115, 8.8.8.8, 8.8.4.4]
version: 2ens33
为网络设备接口名称,addresses
为需要配置的ip地址信息,后面的24
为子网掩码,代表255.255.255.0
,需要用命令ifconfig -a
查看自己的网络信息进行配置gateway4
为ip4
网关,不清楚网关可执行命令route -n
进行查看nameservers
下addresses
为DNS
地址,国内一般选择114.114.114.114
或者114.114.115.115
-
使修改的文件生效
sudo netplan apply
-
修改
/etc/systemd/resolved.conf
文件1
2
3
4
5# 先备份
cp /etc/systemd/resolved.conf /etc/systemd/resolved.conf.bak
# 编辑文件
vim /etc/systemd/resolved.conf1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details
[Resolve]
DNS=114.114.114.114 114.114.115.115 8.8.8.8 8.8.4.4
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes- 若之前DNS查询失败,可能是缓存问题,可以清除DNS缓存:
sudo systemd-resolve --flush-caches
查看Linux系统服务默认的端口列表信息
1
cat /etc/services
- 若之前DNS查询失败,可能是缓存问题,可以清除DNS缓存:
批量重命名文件
去除文件某些字符
1 | $ find . -name '*test.*' -exec bash -c 'mv "$0" "${0/test./}"' {} \; |
历史记录优化
1 | cat >> /etc/profile.d/history.sh <<EOF |
- 键入
history
查看命令行历史记录,再用!n
(n
是命令编号)就可以再次执行。其中有许多缩写,最有用的大概就是!$
, 它用于指代上次键入的参数,而!!
可以指代上次键入的命令了(参考 man 页面中的“HISTORY EXPANSION”)。不过这些功能,你也可以通过快捷键 ctrl-r 和 alt-. 来实现。
关闭SELinux
1 | cat >> /etc/selinux/config <<EOF |
sysctl
优化
1 | cat >> /etc/sysctl.conf <<EOF |
分析Linux
服务器启动耗时
1 | systemd-analyze plot > boot.svg |
查看所有用户的定时任务
1 | cat /etc/passwd | cut -f 1 -d : |xargs -I {} crontab -l -u {} |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HoleLin's Blog!