Linux Basic Notes
Linux Boot System
Grub
Grub Configuration
/etc/default/grub
配置文件, 用于一些基本的修改项, 如默认启动项, Grub 界面等待时长, Grub 主题 etc. More details ininfo -f grub -n 'Simple configuration'
# Default Startup OS
GRUB_DEFAULT=0
# Default Timeout
GRUB_TIMEOUT=5
# https://github.com/vinceliuice/grub2-themes
GRUB_THEME="/boot/grub/themes/Tela/theme.txt"
/etc/grub.d/*
生成/boot/grub/grub.cfg
的执行脚本(update-grub
命令), 可以更细致地修改启动项, 如各个启动项的名称、顺序等.
Grub Repair
Windows Repair
- easyBCD for non-efi loader
- with efi loader, run command:
# root commander
bcdedit /set "{bootmgr}" path \EFI\ubuntu\grubx64.efi
Ubuntu Live Repair
sudo add-apt add-apt-repository ppa:yannubuntu/boot-repair
sudo apt update
sudo apt install boot-repair
boot-repair
重装 Linux
- 自动挂载项 /etc/fstab etc/rc.local
- 自定义脚本-新建目录(加入环境变量)
- 自定义别名 ~/.bashrc
SSH Commands
Key
ssh-keygen -t rsa
ssh-add ~/.ssh/id_rsa
SSHD
- config file in
/etc/ssh/sshd_config
sudo systemctl reload sshd
sudo service restart sshd
SSH Config File
~/.ssh/config
:
- Host 别名
- HostName 主机名(ip)
ssh user@ip
- Port 可忽略
- User 登录用户名
ssh user@ip
- PreferredAuthentications publicKey
- IdentityFile 密钥文件完整路径
ssh -i file
- HostName 主机名(ip)
Host github.com
HostName github.com
PreferredAuthentications publicKey
IdentityFile ~/.ssh/id_rsa
Host cs.github.com
HostName github.com
PreferredAuthentications publicKey
IdentityFile ~/.ssh/cs
Host cloud
HostName xx.org
User root
IdentityFile ~/.ssh/dsl_private_key
Host bwg
HostName 23.106.150.152
User root
Port 29692
git clone git@github.com:user/repo
git clone git@cs.github.com:user/repo
ssh -qTfnN -D 7070 bwg
google-chrome socks5 127.0.0.1 7070
密钥文件
- 登录 远程主机:ssh -i sabertaz root@119.29.140.60
- 文件传输:sftp -i sabertaz root@119.29.140.60
- 登录数据库:mysql -h 10.66.135.125 -P 3306 -u root -p
远程传输文件
rsync -ax -e 'ssh -c blowfish' /root/start_dir root@x.x.x.x:/root/dest_dir
sshpass -p "$DEPLOY_PASSWORD" \
scp -o StrictHostKeyChecking=no \
-P $DEPLOY_PORT \
-r ./build $DEPLOY_USER@$DEPLOY_ADDR:/var/www/html
命令优先级
- 包含路径命令
./vmtools.pl
. - 别名命令.
- Bash 内部命令.
$PATH
包含目录内的命令 (/bin
//sbin
).
Linux Folder Structure
man hier
通过源码包安装的软件,可以通过 ./configure --prefix=/opt/
.
/usr/src
: Kernel source code./usr/share/applications
: Desktop shortcuts./usr/share/fonts/opentype
: Open Type Fonts (OTF)./usr/share/fonts/truetype
: True Type Fonts (TTF)./etc/nginx
: Nginx.
Ubuntu
Ubuntu Locale Settings
export LANG=en_US
xdg-user-dirs-gtk-update
export LANG=zh_CN
- /var/lib/locales/supported.d/local
sudo locale-gen zh_CN.GBK
sudo locale-gen zh_CN.GB18030
sudo dpkg-reconfigure locales
Ubuntu Themes
Icon Themes
Nightly build for Numix Circle icon:
sudo add-apt-repository ppa:numix/ppa
sudo apt update
sudo apt install numix-icon-theme-circle
GTK Themes
GTK/GNOME themes located in /usr/share/themes/
or ~/.themes/
:
# Vimix Cursors Installation
git clone https://github.com/vinceliuice/Vimix-cursors
sudo ./Vimix-cursors/install.sh
# WhiteSur GNOME theme Installation
git clone https://github.com/vinceliuice/WhiteSur-gtk-theme
sudo ./WhiteSur-gtk-theme/install.sh -t all -i ubuntu
# Tweak for Firefox
sudo ./WhiteSur-gtk-theme/tweaks.sh -f
# Tweak for Snap Apps
sudo ./WhiteSur-gtk-theme/tweaks.sh -s
# Tweak for GDM
sudo ./WhiteSur-gtk-theme/tweaks.sh -g -i ubuntu
# Tweak Help Docs
sudo ./WhiteSur-gtk-theme/tweaks.sh -h
Repair for not detected HDMI problem:
sudo dpkg-reconfigure gdm3
sudo apt install --reinstall gdm3 lightdm ubuntu-desktop
GNOME Shell Extension
- Install GNOME shell extension for browser.
- Install local binding app:
sudo apt install chrome-gnome-shell
. - Visit
extensions.gnome.org
to install extensions.
Ubuntu Fonts
mkdir -p ~/.local/share/fonts/
cp -fr code-fonts ~/.local/share/fonts/
fc-cache -f -v
fc-list
fc-list : family | sort | uniq
Arch Linux
Setup Arch Linux Configuration
less /usr/share/aif/docs/official_installation_guide_en
pacman -S lynx arch-wiki-docs arch-wiki-lite
lynx /usr/share/doc/arch-wiki/html/index.html
systemctl enable dhcpcd
reboot
pacman -S --needed base-devel git wget jshon expac yajl zsh vim
- makepkg
curl -L -O https://aur.archlinux.org/cgit/aur.git/snapshot/package_name.tar.gz
cd package_name
less PKGBUILD
less package_name.install
makepkg -si
# -s sync deps
# -i install
# -r rm deps
# -c clean up
Pacman
- /etc/pacman.conf
- /etc/pacman.d/mirrorlist
AUR
# packages' list
wget https://aur.archlinux.org/packages.gz
WSL
WSL Installation
# <!-- markdownlint-disable-next-line MD013 -->
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
# Restart computer.
wget https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
wsl --set-default-version 2
wsl --set-version Ubuntu-22.04 2
wsl -l -v
WSL Proxy Connection
HostIP=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
WslIP=$(hostname -I | awk '{print $1}')
Port=1080
PROXY_SOCKS="socks5://${HostIP}:${Port}"
# For Git CLI.
git config --global http.proxy "${PROXY_SOCKS}"
git config --global https.proxy "${PROXY_SOCKS}"
# For GitHub CLI.
export HTTP_PROXY="${PROXY_SOCKS}"
export HTTPS_PROXY="${PROXY_SOCKS}"
主机代理客户端需要允许 VLAN (或其他网络) 设备访问本地代理连接.
WSL Remote Connection
echo -en '\x10' | sudo dd of=/usr/bin/gzip count=1 bs=1 conv=notrunc seek=$((0x189))
netsh winsock reset
WSL Wi-Fi Connection
Win 11 MediaTek Wi-Fi 6 delay start:
set Network Connections
and WLAN AutoConfig
to auto-start in services.
Basic Commands
ls
-lh
: long human.-ld
: long directory.-i inode --color==auto
权限 (user/group/other) 引用计数 user group 文件大小 文件修改时间 文件名.
cd
-
: 上次目录...
: 上级目录.
pwd
print working directory
rm
- –r delete directory
- –f delete forcedly
- -i 显示确认信息
cp
- -r copy directory
- -p copy property
- -d copy link
- -a --all(-pdr)
- -i 显示确认信息
mv
无需参数(改名+移动)
- -i 显示确认信息
ln
link: create .bak/.hard
(硬链接) and .soft
(软链接:创建链接时填写绝对路径).
- A hard link always points a filename to data on a storage device.
- A soft link always points a filename to another filename, which then points to information on a storage device.
ln [源文件] [New Hard Link File]
ln -s [源文件] [New Soft Link File]
history
- -c 清除历史命令
- -w (~/.bash_history) 保存历史命令
/etc/profile 中修改 HISTSIZE !n/!!/!字符串 重复执行第 n 条/上一条/指定开头的历史命令
# repeat history command
!number
Ctrl R History
Press ctrl-r
提示符改变, 显示我们正在执行反向增量搜索.
搜索过程是反向的
,因为我们按照从现在
到过去
某个时间段的顺序来搜寻.
下一步, 我们开始输入要查找的文本搜索返回我们需要的结果.
(enter
to execute, ctrl-j
to copy)
History Shortcuts
command | function |
---|---|
Ctrl-p | 移动到上一个历史条目 |
Ctrl-n | 移动到下一个历史条目 |
Alt-< | 移动到历史列表开头 |
Alt-> | 移动到历史列表结尾 |
Ctrl-r | 反向增量搜索 |
Alt-p | 反向搜索,非增量搜索 |
Alt-n | 向前搜索,非增量 |
Ctrl-o | 执行历史列表中的当前项,并移到下一个 |
Time
date
Change ntp (Network Time Protocol) time:
sudo apt-get install ntpdate
sudo iptables -A OUTPUT -p udp --dport 123 -j ACCEPT
sudo iptables -A INPUT -p udp --sport 123 -j ACCEPT
sudo ntpdate time.windows.com
sudo hwclock --localtime --systohc
Use local time (not UTC time):
sudo timedatectl set-local-rtc 1
Find and Search Commands
Locate
结合 updatedb
命令 (该命令一般自动 1 天/次).
Type
Indicate how a command name is interpreted.
Apropos
Display a list of appropriate commands.
Whereis and Whatis
Which
Find
find [搜索路径] [可选参数] [文件名](可加"")
:
-name
.-iname
: 不区分大小写.-user
:user_name
按照所有者搜索.-nouser
: 搜索没有所有者的文件.-atime
(文件访问时间)/-ctime
(改变文件属性)/-mtime
(改变文件内容):-10
(十天内)/10
(十天当天)/+10
(十天前).-size
: 文件大小,-25k
(小于 25k)/25M
(25M)/+25G
(大于 25G).-inum
:inode_number
.-a
/-o
: 逻辑与/逻辑或 (左右两端搜索条件).-exec system_command_list {} ;
: 对搜索结果执行操作.-ok system_command_list {} ;
: 对搜索结果执行操作.
find . -name "*.bak" -type f
find . -name "*.bak" -type f -delete
Grep
grep
[可选参数] '字符串' 文件名
:
-I
: 不区分大小写-v
: 排除指定字符串-r
: recursive on directory-l
: only print matched filename--exclude
Find FunctionalComponent
in files and open them all:
grep -lr FunctionalComponent src --exclude=*.md | xargs code
CPU and Process Commands
uptime
Average load information
ps
Report a snapshot of current processes
ps aux
top
top/htop:
- Display tasks
- Average load
- Process status
- CPU usage
atop:
- Memory usage
- Disk I/O usage
- Network usage
vmstat
Outputs a snapshot of system resource usage:
- CPU usage
- Context switch times
- Interrupt times (/proc/interrupts)
- Running and exclusive process status
- Memory usage
- Swap space
- Disk I/O usage
pidstat
Process and Thread:
- CPU usage
- Context switch times
- Interrupt times (/proc/interrupts)
pidstat 1
mpstat
- CPU usage
- Software interrupt times (/proc/interrupts)
mpstat -P ALL 1
lscpu
Show /proc/cpuinfo
.
jobs
list active jobs
bg
place a job in the background
fg
place a job in the foreground
kill
send a signal to a process
killall
kill processes by name
shutdown
shutdown or reboot the system
pstree
outputs a process list arranged in a tree-like pattern
xload and tload
draws a graph showing system load over time
screen
screen -S screenName
screen -ls
screen -r
- Ctrl+d // detach window
- Ctrl+k // kill window
CLI Input Output Commands
cat
concatenate files
sort
sort lines of text
uniq
report or omit repeated lines
wc
print newline, word, and byte counts for each file
Head and Tail
output the first/last part of a file
head -n 5 filename
tail -f filename
Tee
read from standard input and write to standard output and files
[me@linuxBox ~]$ ls /usr/bin | tee ls.txt | grep zip
bunzip2
bzip2
....
nl
number lines
fold
wrap each line to a specified length
fmt
a simple text formatter
pr
prepare text for printing
printf
format and print data
Helper and Documentation Commands
Man
- -f 显示操作等级
- -k 包含匹配
- -1/2/.../9 显示命令不同操作等级的帮助
- Commands (Programs) Those commands that can be executed by the user from within a shell.
- System calls Those functions which must be performed by the kernel.
- Library calls Most of the libc functions.
- Special files (devices) Files found in /dev.
- File formats and conventions The format for /etc/passwd and other human-readable files.
- Games
- Conventions and miscellaneous Overviews of various topics, conventions and protocols, character set standards, and miscellaneous other things.
- System management commands Commands like mount(8), many of which only root can execute.
Help Shell
显示 shell 内部命令帮助,如 cd 命令(shell 内部命令)
Info
显示大型帮助文档 - enter 进入 u 返回 p 上一节 n 下一节 q 退出
System Info
sudo add-apt-repository ppa:dawidd0811/neofetch
sudo apt-get update
sudo apt-get install neofetch
sudo apt-get install screenfetch
Linux Documentation Reference
Compress and Extract Commands
Zip
zip -r(目录) 压缩文件 源文件/源目录
.unzip 源文件 -d 指定路径
.
zip -r archive_name.zip folder_to_compress
unzip archive_name.zip
zipinfo archive_name.zip
unzip -l archive_name.zip
Gz
gzip 源文件
.gzip –c 源文件 > 压缩文件
.gzip -r 源目录 将源目录下所有子文件分别单独压缩
.gzip –d(解压缩) 文件
.gunzip 压缩文件
.
Bz2
不可压缩目录:
bzip2 –k(保留源文件) 源文件
.bzip2 –d(解压缩) –k(保留压缩文件) 压缩文件
.bunzip2 –k(保留压缩文件) 压缩文件
.
Tar
.tar.gz
/.tar.bz2
:
tar [可选参数] 压缩文件(可指定压缩路径) [-c 解压缩路径]源文件/源目录
:
-z
: 将.tar
压缩为.tar.gz
.-j
: 将.tar
压缩为.tar.bz2
.-c
: 打包-x
解打包.-t
: 查看压缩文件.-v
: 显示过程.-f
: 指定压缩文件名.-C
: 指定解压缩路径.-zcvf
/-zxvf
/-ztcf
.-jcvf
/-jxvf
/-jtvf
.
7z
a
: add.x
: extract.-r
: recursive.-o
: specific path.-t
: type.
7z x manager.7z -r -o /home/xx
7z a -t7z -r manager.7z /home/manager/*
UnAr
Decompress files of any format:
unar archive_name.zip
unar archive_name.7z
unar archive_name.rar
unar archive_name.ISO
unar archive_name.tar.gz
LsAr
Peek files in a compress file of any format:
lsar -l archive_name.zip
lsar -l archive_name.7z
lsar -l archive_name.ISO
lsar -l archive_name.rar
lsar -l archive_name.tar.gz
User and Group Commands
- w/who 查看用户详细信息
- last 显示所有用户登陆信息(/var/log/wtmp)
- lastlog 显示所有用户最后一次登陆时间(/var/log/lastlog)
组操作
创建组
groupadd test
修改组
groupmod -n test2 test -g
删除组
groupdel test2
查看组
- groups someUser
- cat /etc/group
cat /etc/passwd | awk -F [:] ‘{print $4}’
\ |sort|uniq | getent group |awk -F [:] ‘{print $1}’
/etc/passwd
is a configuration file which stores user account information.
It is a plain text-based file containing information like
username, user ID and group ID.
用户操作
增加用户
useradd [options] LOGIN
Options:
- -b, --base-dir BASE_DIR 设置基本路径作为用户的登录目录
- -c, --comment COMMENT 对用户的注释
- -d, --home-dir HOME_DIR 设置用户的登录目录
- -D, --defaults 改变设置
- -e, --expiredate EXPIRE_DATE 设置用户的有效期
- -f, --inactive INACTIVE 用户过期后,让密码无效
- -g, --gid GROUP 使用户只属于某个组
- -G, --groups GROUPS 使用户加入某个组(附设组)
- -h, --help 帮助
- -k, --skel SKEL_DIR 指定其他的 skel 目录
- -K, --key KEY=VALUE 覆盖 /etc/login.defs 配置文件
- -m, --create-home 自动创建登录目录
- -l, 不把用户加入到 lastlog 文件中
- -M, 不自动创建登录目录
- -r, 建立系统账号
- -o, --non-unique 允许用户拥有相同的 UID
- -p, --password PASSWORD 为新用户使用加密密码
- -s, --shell SHELL 登录时候的 shell
- -u, --uid UID 为新用户指定一个 UID
- -Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
useradd -s bash -m testUser
passwd testUser # modify `/etc/passwd`, then add to `/etc/sudoers`
adduser
is a perl script which uses useradd
binary in back-end,
adduser
is more user friendly and interactive.
修改用户
usermod -d /home/test -G test2 test
- -l 新用户名 旧用户名 修改用户名
- -g 新用户组 目标用户 改变用户所属组
gpasswd -a test test2 将用户 test 加入到 test2 组(附设组)
gpasswd -d test test2 将用户 test 从 test2 组中移出
usermod -aG sudo <username>
删除用户
userdel test -r 同时删除用户登录目录(/home/xxx)
查看用户
w/who 查看当前登录的所有用户
whoami 查看当前登录用户名
finger apacheUser 查看单个用户信息
限制用户
- passwd -l 用户名 锁定用户
- passwd -u 用户名 解锁用户
- passwd -d 用户名 清除用户密码
Privilege Management Commands
Common Privilege Management
- chown 用户名:组名 文件名
- chgrp 组名 文件名
- umask 存储位置 ——/etc/profile
- 文件默认权限 = 文件默认最大权限 rw-(666) 减去 umask 值(如----w--w-)(022)
- 目录默认权限 = 目录默认最大权限 rwx(777) 减去 umask 值
id <username>
ACL Privilege Management
-
查看分区 ACL 权限是否开启 dumpe2fs -h 设备分区名
-
临时开启分区 ACL 权限 mount -o remount,acl 设备分区名
-
永久开启分区 ACL 权限 /etc/fstab
-
setfacl -m (d:默认权限) u/g:用户名/组名:权限(rwx) 文件名
-
getfacl 文件名——查看文件 ACL 权限
Sudo Privilege Management
/etc/sudoers.tmp
SetUID and SetGID
可执行程序/目录+普通用户临时获得 root 权限 (rws):
- chmod 0xxx 取消双权限
- chmod 2xxx 设置 SetGID 权限
- chmod 4xxx 设置 SetUID 权限
- chmod 6xxx 设置双权限
Disk IO Commands
主分区 (primary) 与延伸分区 (extended) 延伸分区可以继续划分成逻辑分区 (logical).
挂载命令
mount [-t 文件系统][-o 特殊选项] 设备文件名 挂载点(挂载目录/media /misc /mnt)
- 无参数: 显示当前挂载设备.
-a
: 依据/etc/fstab
文件配置, 自动挂载.
umount 设备文件名/挂载点
fdisk –l