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.
- shell
- Linux SysOps Handbook: A study notes book for the common knowledge and tasks of a Linux system admin.
- Linux Hardening Guide: Linux is not a secure operating system. However, there are steps you can take to improve it. This guide aims to explain how to harden Linux as much as possible for security and privacy. This guide attempts to be distribution-agnostic and is not tied to any specific one.
- The early days of Linux
- ⭐The Linux Command Line - zh-CN
- Linux Guide for Complete Beginners: 本书摘取了 The Linux Commandline 中最常用的部分,结合一些开发中比较实用的操作而整理出来的。
- Linux工具快速教程:本书专注于Linux工具的最常用用法,以便读者能以最快时间掌握,并在工作中应用
- ctrl + w 往回删除一个单词,光标放在最末尾
- ctrl + u 删除光标以前的字符
- ctrl + k 删除光标以后的字符
- ctrl + a 移动光标至的字符头
- ctrl + e 移动光标至的字符尾
- ctrl + l 清屏
-
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
- CentOS:
- 进程信息树:
systemctl status PID
- xargs
-
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
-
hostnamectl set-hostname XXXX
-
hostname XXXX
临时修改主机名为 XXXX -
vi /etc/hostname
永久修改主机名
-
-
vi /etc/hosts
添加新主机名回环地址映射 - 其他云主机相关
-
vi /etc/cloud/cloud.cfg
将preserve_hostname
为true
-
- 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.
-
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 -had1
以 human readable 格式,浏览当前目录,所有文件及文件夹大小
-
cp -r xxx/. yyy/
递归拷贝xxx至yyy,含隐藏文件
-
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
- 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'
- 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 的硬链接与软链接
- 修改用户密码:
- 修改当前用户密码:passwd
- 用root修改某个用户密码:passwd username
- 添加用户:adduser username
- 添加root权限:usermod -g root username
- 踢人
who
/w
thenpkill -kill -t XXX_TTY
-
udp
connection test:nc -vuz SOME_IP SOME_PORT
-
如何在 Ubuntu 服务器 22.04 上设置静态 IP 地址
vi /etc/netplan/00-installer-config.yaml
netplan apply
- 详见: ssh
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
- 推荐使用 rsync
- ssh copy 文件至远端
scp -P12345 xx.txt [email protected]:~/
, 注意大写-P
-
vsftp
注:Step2中备份不要用mv,用cp - vsftpd允许root用户登录
- 修改 vsftpd 的默认根目录
- 创建用户
useradd -d /var/www/html -M ftpuser
passwd ftpuser
chown -R ftpuser /var/www/html
- 命令行连接
ftp [OPTION...] [HOST [PORT]]
-
查看当前所有tcp端口使用情况:
netstat -ntlp
- Daemon to ban hosts that cause multiple authentication errors
- Protect SSH with Fail2Ban
- VPS 防止 SSH 暴力登录尝试攻击
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
目录镜像至DEST
:rsync -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 使用
-
仅复制指定文件rsync -av --include="*.lua" --exclude='*' SRC/ DEST
rsync -av --include='*/' --include="*.lua" --exclude='*' SRC/ DEST
- 详见:rsync copy over only certain types of files using include option
-
rsync -av --include="*.txt" --exclude='f1.txt' SRC/ DEST
指定复制规则,同时排除特定
-
- 设置 ssh config 后,操作基本同 scp
- 同步 xxx 至 $REMOTE:/var/www/html/xxx
-
rsync -avzP xxx $REMOTE:/var/www/html
- 压缩传输(-z), 显示进度(-P)
-
- 同步 xxx 至 $REMOTE:/var/www/html/xxx
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
是与 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
systemd
集成了自己的日志系统 journald
,通过 journalctl
命令查看。
journalctl -u nginx
# 实时跟踪服务的日志 (类似 tail -f)
journalctl -u nginx -f
- 定时任务crontab
-
如何在重启或启动时执行命令或脚本
- 添加脚本至
/etc/rc.d/rc.local
,确保该文件有运行权限chmod a+x /etc/rc.d/rc.local
- 使用
crontab -e
添加@reboot (sleep 30; bash ~/xx.sh)
- 执行
select-editor
可重新选择编辑器 - 也可以添加在
/etc/crontab
,但是注意需要指定 user,如:@reboot root (sleep 30; bash ~/xx.sh)
- 添加脚本至
- How To Secure A Linux Server
- 服务器操作系统应该选择 Debian/Ubuntu 还是 CentOS?
- CentOS: 永远有多远就离它多远
-
在linux上做机器学习,请问centos和ubuntu有区别吗?
- TensorFlow 用 Ubuntu
-
Linux 配置 history 命令显示操作时间、用户和登录 IP
export HISTSIZE=4096 export HISTFILESIZE=$HISTSIZE USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` if [ -z $USER_IP ]; then USER_IP=`hostname` fi export HISTTIMEFORMAT="%F %T $USER_IP:`whoami` "
-
Disable "Permanently added ..." warning on local LAN
- add
LogLevel ERROR
to~/.ssh/config
- add
- Awesome Linux, A list of awesome projects and resources that make Linux even more awesome
- ArchWiki