参考文献

示例

1
2
# 在远程机器上运行一段脚本。这条命令最大的好处就是不用把脚本拷到远程机器上
ssh user@server bash < /path/to/local/script.sh
1
2
# 显示ascii码表
man ascii
1
2
# 这行脚本能输出你最常用的十条命令
history | awk '{CMD[$2]++;count++;} END { for (a in CMD )print CMD[a] " " CMD[a]/count*100 "% " a }' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
1
2
# Marix的屏幕效果
tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]"
1
2
3
4
5
# 列出已知的编码
iconv -l
437, 500, 500V1, 850, 851, 852, 855, 856, 857, 858, 860, 861, 862, 863, 864,
....

查看系统信息

显示硬件系统部件 - (SMBIOS / DMI)

1
dmidecode -q

显示机器的处理器架构

1
2
3
4
root@xxxx:/home/xxx# arch
x86_64
root@xxxx:/home/xxx# uname -m
x86_64

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
2
3
4
$ getconf LONG_BIT
64
$ file /bin/ls
/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=9567f9a28e66f4d7ec4baf31cfbf68d0410f0ae6, stripped

显示各种信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 显示CPU info的信息
cat /proc/cpuinfo
# 显示中断
cat /proc/interrupts
# 校验内存使用
cat /proc/meminfo
# 显示哪些swap被使用
cat /proc/swaps
# 显示内核的版本
cat /proc/version
# 显示网络适配器及统计
cat /proc/net/dev
# 显示已加载的文件系统
cat /proc/mounts
# 罗列 PCI 设备
lspci -tv
# 显示 USB 设备
lsusb -tv

显示文件或目录其他信息

1
2
$ stat -x readme.md   # macOS 系统
$ stat readme.md # Linux 系统

控制台使用技巧

  • 操作快捷键

    • 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Ctrl + A  跳转到你当前编辑的命令行行首
Ctrl + E 跳转到你当前编辑的命令行行尾
Ctrl + L 清屏,和 clear 指令类似
Ctrl + U 清除行中光标之前的内容(在行尾时即清除整行)
Ctrl + H 和退格一样
Ctrl + R 能让你搜索之前使用过的命令行记录
Ctrl + C 强制停止当前的程序
Ctrl + D 退出当前 shell (壳层/命令行界面)
Ctrl + Z 将当下运行的程序挂起,使用 fg 来恢复运行
Ctrl + W 删除光标前的一个词
Ctrl + K 清除行中光标之后的内容
Ctrl + T 交换光标前两个字符
Esc + T 交换光标前两个词
Alt + F 将光标移至行内下一个词处
Alt + B 将光标移至行内上一个词处
Tab 自动补全文件/目录的名称

Mac cli技巧

1
2
3
4
5
6
7
8
9
10
$ !!                            # 再一次执行上一条指令
$ sudo !! # 以管理员身份执行上一条指令
$ !<word> # 加上特定命令行前缀再执行上一条指令
$ !<word>:p # 显示上一条指令加上前缀,但不要执行
$ <space>command # 执行指令,但不要存到历史记录中
$ echo "ls -l" | at midnight # 在特定时间执行指令
$ caffeinate -u -t 3600 # 接下来一小时内阻止你的mac休眠
$ ls -lhs # 将目录中文件按大小排序显示
$ qlmanage -p <file> # 从命令行调用"速览"
$ top -o vsize # 查看是什么拖慢了你的mac

修改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
    18
    holelin@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
    5
    holelin@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: 2
    • ens33为网络设备接口名称,addresses为需要配置的ip地址信息,后面的24为子网掩码,代表255.255.255.0,需要用命令ifconfig -a查看自己的网络信息进行配置
    • gateway4ip4网关,不清楚网关可执行命令route -n进行查看
    • nameserversaddressesDNS地址,国内一般选择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.conf
    1
    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 

批量重命名文件

去除文件某些字符

1
$ find . -name '*test.*' -exec bash -c 'mv "$0" "${0/test./}"' {} \;

历史记录优化

1
2
3
4
5
6
7
8
9
10
cat >> /etc/profile.d/history.sh <<EOF
# Administrator specific aliases and functions for system security
# Add by netkiller
export HISTSIZE=10000
export HISTFILESIZE=10000
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export TIME_STYLE=long-iso
EOF

source /etc/profile.d/history.sh

关闭SELinux

1
2
3
4
5
6
cat >> /etc/selinux/config <<EOF
# Add by netkiller
SELINUX=disabled
EOF

setenforce Permissive

sysctl优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat >> /etc/sysctl.conf <<EOF
# Netkiller
net.ipv4.ip_local_port_range = 1025 65500
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_keepalive_time = 1800
net.core.netdev_max_backlog=3000
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_max_tw_buckets = 4096
net.core.somaxconn = 1024
# TCP BBR
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
EOF
#net.ipv4.tcp_syncookies = 1
#net.ipv4.tcp_fin_timeout = 60

sysctl -p

分析Linux服务器启动耗时

1
systemd-analyze plot > boot.svg

查看所有用户的定时任务

1
cat /etc/passwd | cut -f 1 -d : |xargs -I {} crontab -l -u {}