linux - doubility-sky/daydayup GitHub Wiki

Linux (/ˈlinʊks/ (:sound:listen) LEEN-uuks or /ˈlɪnʊks/ LIN-uuks) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged in a Linux distribution.

Learn

books

others

Shortcuts

  • ctrl + w 往回删除一个单词,光标放在最末尾
  • ctrl + u 删除光标以前的字符
  • ctrl + k 删除光标以后的字符
  • ctrl + a 移动光标至的字符头
  • ctrl + e 移动光标至的字符尾
  • ctrl + l 清屏

Common

  • whatis XXX
    whatis whatis
      whatis (1)           - display one-line manual page descriptions
    whatis df
      df (1)               - report file system disk space usage
    whatis du
      du (1)               - estimate file space usage
    whatis free
      free (1)             - Display amount of free and used memory in the system
    
  • Date time
    • date "+%Y-%m-%d %H:%M:%S"
  • Package Manager
    • CentOS: yum update
  • 进程信息树: systemctl status PID
  • xargs

System

  • neofetch: Fast, highly customisable system info script
    • apt install neofetch
  • 6 Methods to check the Linux Kernel version running on your system
    • uname -a
  • 查看 Linux 发行版名称和版本号的 8 种方法
    • cat /etc/os-release
  • 查询 CPU 信息
    • 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数
    • 查看物理CPU个数
      • cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
    • 查看每个物理CPU中core的个数(即核数)
      • cat /proc/cpuinfo| grep "cpu cores"| uniq
    • 查看逻辑CPU的个数
      • cat /proc/cpuinfo| grep "processor"| wc -l
    • 查看是否为超线程,如 cpu cores 数量和 siblings 一致,则未启用超线程
      • cat /proc/cpuinfo | grep -e "cpu cores" -e "siblings" | sort | uniq
  • setup swapfile
    • swapon --show
    • fallocate -l 16G /swapfile
    • chmod 600 /swapfile
    • mkswap /swapfile
    • swapon /swapfile
    • vi /etc/fstab add line /swapfile none swap sw 0 0
  • time zone switch
    • cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  • uptime

hostname

  • hostnamectl set-hostname XXXX
    • hostname XXXX 临时修改主机名为 XXXX
    • vi /etc/hostname 永久修改主机名
  • vi /etc/hosts 添加新主机名回环地址映射
  • 其他云主机相关
    • vi /etc/cloud/cloud.cfgpreserve_hostnametrue
  • 1 切换显示各逻辑CPU状况
  • c 切换显示各进程完整命令行
  • x 高亮排序属性列
  • SHIFT + '<' / '>' 左右切换排序列
  • M 按内存占用率排序
  • P 按CPU占用率排序

htop is an interactive system-monitor process-viewer and process-manager. It is designed as an alternative to the Unix program top

iftop does for network usage what top(1) does for CPU usage. It listens to network traffic on a named interface and displays a table of current bandwidth usage by pairs of hosts. Handy for answering the question "why is our ADSL link so slow?".

Glances is a cross-platform monitoring tool which aims to present a large amount of monitoring information through a curses or Web based interface. The information dynamically adapts depending on the size of the user interface.

File

  • Linux shell 之 提取文件名和目录名的一些方法
    echo dirname is $(dirname $file)
    echo filename is $(basename $file)
    echo extension is "${file##*.}"
    echo filename without ext "${file%.*}"
  • lsof -i:80 lsof is a command meaning "list open files", which is used in many Unix-like systems to report a list of all open files and the processes that opened them.

du

  • du -had1 以 human readable 格式,浏览当前目录,所有文件及文件夹大小

cp

  • cp -r xxx/. yyy/ 递归拷贝xxx至yyy,含隐藏文件

tar

  • tar -zcvf xxx.tar.gz [FILE]...
    • -z :压缩类型为 .tar.gz
    • -c :打包 (建立压缩档案)
    • -v :显示过程
    • -f :指定打包后的文件名
  • tar -zxvf xxx.tar.gz
    • -z :解压缩类型为 .tar.gz
    • -x :解压
    • -v :显示过程
    • -f :指定打包后的文件名
  • tar -zxvf xxx.tar.gz -C ./dst/ 解压到指定已存在目录 dst

find

  • https://man.linuxde.net/find
  • 删除指定时间之前的文件[夹] (并约束递归深度)
    • find . -maxdepth 1 -mtime +30 -exec rm -rf {} \;
  • 配合 xargs 批量处理
    • find . -name '*.txt' | xargs -I file bash -c 'echo "Hello" > file'

links

  • Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。默认情况下,ln命令产生硬链接。
    • 硬连接指通过索引节点来进行连接。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在Linux中,多个文件名指向同一索引节点是存在的。一般这种连接就是硬连接。硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。
    • 软连接,也叫符号连接(Symbolic Link)。软链接文件有类似于Windows的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。
    touch f1          # 创建一个测试文件f1
    ln f1 f2          # 创建f1的一个硬连接文件f2
    ln -s f1 f3       # 创建f1的一个符号连接文件f3
    ls -li            # -i参数显示文件的inode节点信息
    # 1). 删除符号连接f3,对f1,f2无影响;
    # 2). 删除硬连接f2,对f1,f3也无影响;
    # 3). 删除原文件f1,对硬连接f2没有影响,导致符号连接f3失效;
    # 4). 同时删除原文件f1,硬连接f2,整个文件会真正的被删除。
  • 理解 Linux 的硬链接与软链接

User

  • 修改用户密码:
    • 修改当前用户密码:passwd
    • 用root修改某个用户密码:passwd username
  • 添加用户:adduser username
  • 添加root权限:usermod -g root username
  • 踢人 who/w then pkill -kill -t XXX_TTY

According to OpenSSH developers in April 2019, SCP is outdated, inflexible and not readily fixed; they recommend the use of more modern protocols like sftp and rsync for file transfer. https://www.openssh.com/txt/release-8.0

ftp

netstat

iptables

Similar to cp, rcp and scp, rsync requires the specification of a source and of a destination, of which at least one must be local.

  • rsync 用法教程
  • SRC 目录同步至 DEST/SRC(不存在则创建):rsync -av SRC DEST
  • SRC 目录同步至 DEST (不存在则创建):rsync -av SRC/ DEST
    • -a/--archive 存档模式,保存所有的元数据
    • -v 输出细节。-vv 输出更详细的信息,-vvv 输出最详细的信息。
  • SRC 目录镜像至 DESTrsync -av --delete SRC/ DEST
  • SRC1 ... SRCn 同步至 DEST/SRC1 ... DEST/SRCn(不存在则创建)
    • rsync -av SRC1 SRC2 ... SRCn DEST
  • exclude
    • SRC 目录同步至 DEST,且排除隐藏文件 .*
      • rsync -av --exclude '.*' SRC/ DEST
    • SRC 目录同步至 DEST,且有多个排除选项
      • rsync -av --exclude={'f1.txt','f2.txt'} SRC/ DEST
  • include 一般需配合 exclude 使用
  • 设置 ssh config 后,操作基本同 scp
    • 同步 xxx 至 $REMOTE:/var/www/html/xxx
      • rsync -avzP xxx $REMOTE:/var/www/html
        • 压缩传输(-z), 显示进度(-P)

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.

  • linux screen 命令详解
  • Frequently Command:
    screen -ls             列出当前所有的session
           -r <作业名称>    恢复离线的screen作业。
    
  • In Session Command:
    C-a n -> Next,切换到下一个 window 
    C-a p -> Previous,切换到前一个 window 
    C-a d -> detach,暂时离开当前session,
            将目前的 screen session (可能含有多个 windows) 丢到后台执行,
            并会回到还没进 screen 时的状态,此时在 screen session 里,
            每个 window 内运行的 process (无论是前台/后台)都在继续执行,即使 logout 也不影响。 
    
  • man screen

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

Zellij is a workspace aimed at developers, ops-oriented people and anyone who loves the terminal. At its core, it is a terminal multiplexer (similar to tmux and screen), but this is merely its infrastructure layer.

Systemctl

systemctl 是与 systemd 交互的主要工具,用于管理服务和其他系统单元。

管理服务状态

这些命令用于控制服务的实时状态,[服务名] 通常可以省略 .service 后缀,例如 nginx

systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl status nginx

管理开机自启

这些命令决定服务是否在系统启动时自动运行。

systemctl enable nginx
systemctl disable nginx
systemctl is-enabled nginx
# 启用并立即启动服务
systemctl enable --now nginx

查看服务单元信息

用于列出和检查系统中的所有 systemd 单元。

# 列出所有正在运行的服务单元
systemctl list-units --type=service
# 列出所有已安装的服务单元 (无论是否启动)
systemctl list-unit-files --type=service
# 显示服务单元的配置文件内容
systemctl cat nginx
# 显示服务单元的依赖关系
systemctl list-dependencies nginx

查看日志 (通过 journalctl)

systemd 集成了自己的日志系统 journald,通过 journalctl 命令查看。

journalctl -u nginx
# 实时跟踪服务的日志 (类似 tail -f)
journalctl -u nginx -f

Schedule

FAQs

Resources

⚠️ **GitHub.com Fallback** ⚠️