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

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/cloud/cloud.cfgpreserve_hostnametrue
    • vi /etc/hosts 添加新主机名回环地址映射
  • 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,含隐藏文件

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 用法教程
    • rsync -avP SRC DEST 将 SRC 内容递归复制到 DEST/SRC(不存在则创建)
    • rsync -avP SRC/ DEST 将 SRC 内容递归复制到 DEST(不存在则创建)
    • rsync -avP SRC1 SRC2 ... SRCn DEST 将 SRC1 ... SRCn 内容递归复制到 DEST/SRC1 ... DEST/SRCn(不存在则创建)
    • rsync -avP --exclude '.*' SRC/ DEST 排除隐藏文件 .*
    • rsync -avP --exclude={'f1.txt','f2.txt'} SRC/ DEST 多个排除选项
    • rsync -avP --include="*.lua" --exclude='*' SRC/ DEST 仅复制指定文件,需配合 exclude
    • rsync -avP --include="*.txt" --exclude='f1.txt' SRC/ DEST 指定复制规则,同时排除特定
    • rsync -avP --delete SRC/ DEST 复制并删除 DEST 中,不存在于 SRC 中的文件(即:镜像同步)
  • 用 ssh key 同步远程主机文件
    • rsync -avzP -e "ssh -i ~/sshkey.pem" [email protected]:Projects/sample.csv ~/sample.csv
  • 设置 ssh config 后,操作基本同 SCP,详见 SSH
    • 例如:rsync -avP SRC REMOTE:/var/www/html

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
  • SSH 教程
  • 远程连接:ssh -p12345 [email protected]
    • 其中 12345 为端口,无 -p 选项则为默认 22 端口。
  • ssh copy 文件至远端 scp -P12345 xx.txt [email protected]:~/
    • 注意此处指定端口与 ssh 不同,需要大写 -P
  • ssh key 免密连接配置
    • 生成密钥对 ssh-keygen -t rsa -P ''
    • 将公钥 id_rsa.pub 内容,写入远端文件 ~/.ssh/authorized_keys
  • ssh 设置远端别名
    • 修改本地文件 ~/.ssh/config (没有则用 touch 创建), 按如下格式添加内容,其中 Port 默认为 22
      Host Xsvr
      HostName 172.217.31.238
      Port 12345
      User root
      IdentityFile ~/.ssh/id_rsa
      
    • 即可 ssh Xsvr
  • 禁止密码登录(仅 RSA key 登录更安全)
    • vi /etc/ssh/sshd_config
    • PasswordAuthentication no
    • service sshd restart
  • ssh免密码登录
  • SSH设置别名访问远程服务器
  • ssh 鲜为人知的三种用法
    • vi /etc/ssh/sshd_config 打开 GatewayPorts yes
    • 远程端口转发至本地端口 ssh -N -R [REMOTE_BIND:]REMOTE_PORT:LOCAL_IP:LOCAL_PORT user@remote_host
  • 使用 SSH TUNNEL 打通公司和家里的网络
  • linux管理多个ssh公钥密钥
  • VPS 防止 SSH 暴力登录尝试攻击
  • Fail2Ban: ban hosts that cause multiple authentication errors
  • SSH Broken pipe
    # vi /etc/ssh/sshd_config (server side)
    ClientAliveInterval 10
    ClientAliveCountMax 6
  • HowTo: Disable SSH Host Key Checking
    Host *
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null
  • How to Keep SSH Session Alive
  • Mosh is a remote terminal application that supports intermittent connectivity, allows roaming, and provides speculative local echo and line editing of user keystrokes. It aims to support the typical interactive uses of SSH, plus ... https://mosh.org/
    • Mosh will log the user in via SSH, then start a connection on a UDP port between 60000 and 61000.
    • install mosh on both client and server side. let server ufw allow 60000:61000/udp
  • GlobalSSH 是一款致力于提高跨国远程管理服务器效率的产品,旨在解决由于跨国网络不稳定导致的远程管理出现的卡顿、连接失败、传输速度较慢等现象。本产品可极大程度的减少卡顿、连接失败的情况发生,提高运维工作的效率。

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

  • CentOS 上的 FirewallD 简明指南
  • 启动/开机启动
    • systemctl start firewalld / systemctl enable firewalld
  • 停止/禁用
    • systemctl stop firewalld / systemctl disable firewalld
  • 查看状态
    • firewall-cmd --state
  • 开放指定端口
    • firewall-cmd --zone=public --add-port=12345/tcp --permanent
  • 关闭指定端口
    • firewall-cmd --zone=public --remove-port=12345/tcp --permanent
  • 查看配置
    • firewall-cmd --zone=public --list-all
    • firewall-cmd --list-all-zones 所有区域的配置
  • 端口转发 local:80 => local:12345
    • firewall-cmd --zone="public" --add-forward-port=port=80:proto=tcp:toport=12345
  • 转发远程主机 local:80 => 123.456.78.9:8080
    • firewall-cmd --zone="public" --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=123.456.78.9
    • 在需要的区域中激活 masquerade firewall-cmd --zone=public --add-masquerade
    • 删除规则 firewall-cmd --zone=public --remove-masquerade
  • 应急模式,阻断或放开所有网络
    • firewall-cmd --panic-on / firewall-cmd --panic-off
  • ⭐令修改生效(重新加载 FirewallD 配置) firewall-cmd --reload
  • Rich Rules
    # 允许来自指定远程主机的所有 IPv4 流量
    firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address=123.456.78.9 accept'
    # 拒绝来自指定主机到 22 端口的 IPv4 的 TCP 流量
    firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address=123.456.78.9 port port=22 protocol=tcp reject'
    # 允许来自指定主机到 80 端口的 IPv4 的 TCP 流量,并将流量转发到 6532 端口上
    firewall-cmd --zone=public --add-rich-rule 'rule family=ipv4 source address=123.456.78.9 forward-port port=80 protocol=tcp to-port=6532'
    # 移除相应 rich rule 
    firewall-cmd --zone=public --remove-rich-rule '... XXXX ...'

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.

Schedule

FAQs

Resources

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