服务器 Ubuntu 系统安装

制作启动 U 盘 下载镜像,这里用的是 20.04.2 的 server 版本 https://ftp.sjtu.edu.cn/ubuntu-cd/20.04.2/ubuntu-20.04.2-live-server-amd64.iso 安装烧录工具——balenaetcher 烧录镜像,烧录完会自动弹出 安装系统 将 u 盘插入后开机,会自动进入引导程序,跟着指引安装即可 硬盘分区 主硬盘 1 个 T,分了下面几个区,其中/boot/efi是自动分的,其他都需要选择自定义后自己配置出来。格式均为xfs。 /boot/efi:500M /boot:1G /:100G /home:100G /data:400G [SWAP]:8G 还剩下约 300G 的空间备用。 配置 SSH 如果有联网的话,可以自动从 github 拉取。装系统时如果没有联网,可以等联网后配置authorized_keys,或者通过scp拉取云服务器上的authorized_keys。 配置系统 修改 root 密码 sudo passwd root 卸载 snap sudo apt autoremove --purge snapd 硬盘管理 # 磁盘信息查看 lsblk -f # 显示硬盘、分区及类型 sudo parted /dev/sda print # 显示硬盘格式与分区 # 查看lvm管理的磁盘信息 sudo pvs # phicical volumes sudo vgs # volume groups sudo lvs # logical volumes # lvm 拓展容量 vgextend # 挂载硬盘 sudo mkdir /data1 sudo mount /dev/sda1 /data1 Setup Flexible Disk Storage with Logical Volume Management (LVM) in Linux – PART 1 ...

June 7, 2021

Bash Script

References 手册 - Bash Reference Manual 检查工具 - shellcheck Shebang #!/bin/bash # 在 shebang 行中使用 env 命令 #!/usr/bin/env python Variables # 定义变量 var="a string" #等号两边不能有空格 #调用变量 echo $var echo ${var} # `""` suppress expansions except parameter expansion, arithmetic expansion, and command substitution echo "$var" # 双引号会渲染变量,输出`a string` # `''` suppress all expansions echo '$var' # 单引号不会渲染变量,输出`$var` # Arithmetic Expansion echo $((1+1)) # 输出`2` echo $((1.0/2)) # 输出`0.5` # Command Substitution # 执行命令并将输出替换原来内容 `cat some.txt` $( cat some.txt ) # Process Substitution # 执行命令并将输出写到一个临时文件中,并用临时文件名替换原来内容 # 在我们希望返回值通过文件而不是STDIN传递时很有用 diff <(ls foo) <(ls bar) #显示文件夹`foo`和`bar`中文件的区别 # Globbing 通配符 # `?`匹配一个字符,`*`匹配任意个字符 rm foo* # Brace Expansion # 花括号展开 echo Number_{1..5} echo Front-{A,B,C}-Back Positional Paramaters $0:路径 dirname $0:路径名 basename $0:文件名 $1~$9:输入参数 $@: 所有参数,$*:区别是会展开双引号中的参数 $#: 参数个数 $?:上一次退出状态,0为正常,其他为错误 $$: 当前脚本的进程识别码 Logit Expression Logit [[ exp1 && exp2 ]] and [[ exp1 || exp2 ]] or [[ ! exp1 ]] not # do command2 if command1 is sucessful command1 && command2 # do command2 if command1 is unsucessful command1 || command2 Function function func { commands return } # or func () { commands return } # local variable function func { local var var=2 return } # function arguments func () { echo "Parameter #1 is $1" # arguments are refered by position } func "$arg1" "$arg2" # call function with arguments Branching and Looping If x=5 if [[ $x -eq 5 ]]; then echo "x equals 5." elif [[ $x -eq 4 ]]; then echo "x equals 4." else echo "x does not equal 5 or 4." fi Case read -p case $REPLY in 0) echo "zero" ;; 1) echo "one" ;; 2) echo "two" ;; *) echo "other" ;; esac While count=1 while [[ $count -le 5 ]]; do echo $count count=$((count + 1)) done while true; do echo 1 sleep 1 done For for (( i=0; i<5; i=i+1 )); do echo $i done for i in {1..100}; do echo $i done # iterate arguments from the 3rd for i in "${@:3}"; do echo $i done Test test 手册 ...

January 1, 2000

Commands

Commands 文件与文件夹操作 文件夹操作 ls # 查看文件夹 ls your_dir ls -F # 显示文件类型,/:文件夹,*:可执行文件,@:链接 ls -F your_dir | grep / # 只显示文件夹 ls -F your_dir | grep -v / # 只显示文件 tree # 以树形显示文件夹 tree tree -L 2 # 参看目录层数2以内的文件 tree -f # 显示完整路径 tree -d # 只显示文件夹 pwd # 显示当前目录 pwd mkdir # 创建文件夹 mkdir dir mkdir -p your_dir/subdir # 创建多级文件夹 tree -fid --noreport your_dir >> your_dir.txt && mkdir tmp && cd tmp/ && mkdir -p `cat ../your_dir.txt` # 复制目录结构 touch # 创建文件 touch your_file # 创建文件或更新文件时间戳 touch -a|-m your_file # 只更新访问|修改时间 touch -t 202102071200.00|YYYYmmddHHMM.SS # 更新为指定时间戳 cd # 切换文件夹 cd your_dir cd ..|~|- # 切换到父文件夹|用户文件夹|上一次文件夹 cp # 复制文件夹 cp source target cp -p # 保留源文件所有属性 cp -d # 保留符号链接指向 cp -r # 递归复制文件夹 cp -a # == cp -pdr mv # 移动文件夹 mv source target mv source_1 source_2 source_3 target # 移动多个文件到目标文件夹 rm # 删除 rm file rm -rf dir # 你懂的 rename # 重命名文件 rename .png .jpg ./* # 将所有文件的后缀由 .png 改成 .jpg ln # 链接 ln your_target your_link # hard link,多个文件名指向同一个索引节点(inode),只有当文件所有的硬链接都被删除时,文件才真正删除。可以通过创建硬链接防止文件误删 ln -s your_target your_link # soft/symbolic link,相当于创建快捷方式 # 查看链接具体的命令 type ll # 输出:ll is aliased to `ls -l --color=auto' 文件夹路径 basename # 显示文件名 basename /data/dir1/file1.txt basename /data/dir1/file1.txt .txt # 不显示后缀 .txt dirname # 显示路径 dirname /data/dir1/file1.txt dirname ./file1.txt # 传入相对路径也会返回相对路径 查看文件信息 ls -l 输出的含义 1|2 |3 |4 |5| 6 | 7 | 8 | 9 | 10 -rw-rw-r-- 1 yzchen yzchen 6 Feb 7 15:23 newfile drwxrwxr-x 2 yzchen yzchen 4096 Feb 7 15:25 subdir 文件(夹)类型 文件所属用户权限 文件所属用户组权限 其他用户权限 硬链接个数 所属用户 所属用户组 文件(夹)大小 修改时间 名称 file 查看文件类型 ...

January 1, 2000

Network

Link Layer 查看网络接口 ip # 查看网络接口详细信息 ip address ip address show ${interface} ip -brief address # 查看链路层接口 ip -brief link x # 查看路由表 ip route # 查看arp表 ip neighbour ifconfig interface configuration command ifconfig # 查看所有接口 ifconfig -a # 查看接口eth0信息 ifconfig eth0 # Disable eth0 interface ifconfig eth0 down # Enable eth0 interface ifconfig eth0 up # Assign IP address to eth0 interface ifconfig eth0 ${ip_address} /sys/class/net ll /sys/class/net 查看arp表 arp # 查看arp表 arp # 清空arp缓存 sudo arp -a -d Network Layer icmp ping # echo ping # ping with fixed size data to check MTU ping -c 4 -s 1600 ${target_ip} traceroute # 发现IP接力路径(route)上的各个路由器 traceroute 路由转发 route # 查看路由表 route -n # 添加路由规则 sudo route add -net ${ip_address} netmask ${netmask_address} gw ${gw_address} # 删除路由规则 sudo route del -net ${ip_address} netmask ${netmask_address} gw ${gw_address} routel # list routes with pretty output format routel routef # flush routes routef /proc/sys/net/ipv4/ip_forward # 启用路由转发 echo "1" > /proc/sys/net/ipv4/ip_forward # 禁用路由转发 echo "0" > /proc/sys/net/ipv4/ip_forward Public IP curl ifconfig.me curl myip.ipip.net dig +short myip.opendns.com @resolver1.opendns.com Transport Layer TCP telnet # tcp连接 telnet ${ip} ${port} nc # Listen on a specified port and print any data received nc -l ${port} # send UDP data echo "Hello, UDP!" | nc -u ${receiver_ip} ${port} # Connect to a certain port nc ${ip_address} ${port} # 扫描指定ip指定端口 nc -z -v ${ip_address} ${port} 过滤 iptables IPTables ...

January 1, 2000

Server

修改hostname vi /etc/hostname vi /etc/hosts 修改时区 # 查看时区 timedatectl timedatectl list-timezones | grep -i shanghai # 修改时区 sudo timedatectl set-timezone Asia/Shanghai 创建用户 # 创建用户 sudo adduser yzchen # 添加sudoer sudo su chmod +w /etc/sudoers visudo # 相当于vi /etc/sudoers ## 添加以下行 username ALL=(ALL) ALL chmod -w /etc/sudoers ssh免密登录 创建密钥 ssh-keygen 复制公钥到要登录的服务器 ssh-copy-id username@host bashrc配置 配置语言 export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8 常用alias alias quick-push="git add --all && git commit -m 'update' && git push -u origin master" alias json_pp="json_pp -json_opt pretty,utf8" Self-hosted frp Github - fatedier/frp ...

January 1, 2000

ShortCuts

ShortCuts Ctrl + A: 跳到行首 Ctrl + E: 跳到行尾 Ctrl + W: 清除到上一个单词 # 回到上次所在的目录 cd - # 输入上一个命令 !! clear/Ctrl+L clear the screen history Display the contents of the history list Ctrl+A Move cursor to the beginning of the line Ctrl+E Move cursor to the end of the line Alt+F Move cursor forward one word Alt+B Move cursor backward one word Ctrl+K Kill text from the cursor location to the end of line Ctrl+U Kill text from the cursor location to the beginning of the line Ctrl+Y Yank text from the kill-ring and insert it at the cursor location <tab> <tab> Display list of possible completions Alt+* Insert all possible completions Signals Ctrl-C: 发送 SIGINT,停止进程,可以被进程捕获 Ctrl-Z: 发送 SIGTSTP,暂停进程 Ctrl-\: 发送 SIGQUIT

January 1, 2000

Vim

References vimtutor Github - spf13-vim openvim - Tutorial Modes Normal Mode: ESC Insert Mode: i Replace Mode: R Visual Mode: v Command Mode: : Commands Quit: :q Save: :w Save and quit: :wq Quit without save: :q! Open file for editing: :e {filename} Show open buffers: :ls 切换不同buffer: :b {filename} :b1: 切换至第1个buffer 关闭当前buffer: :bw Help: :help {topic} opens help for the :w command: :help :w opens help for the w movement: :help w jump from one window to another: Ctrl-W+Ctrl-w Shell command: :! :!ls: list files Retrieve from file: :r {filename} Retrieve from command: :r !ls Set option: :set {option} Ignore case: :set ic 关掉选项,前面加’no’: :set noic Highlight search: :set hls/:nohlsearch 清除上次搜索结果: :noh Command completion: Ctrl-D, or TAB Normal mode Movement Basic movement: hjkl (left, down, up, right) Words: w (next word) b (beginning of word) e (end of word) Lines: 0 (beginning of line) ^ (first non-blank character) $ (end of line) Screen: H (top of screen) M (middle of screen) L (bottom of screen) Scroll: Ctrl-U (up)/Ctrl-D (down):Scroll half a page Ctrl-B (up)/Ctrl-F (down):Scroll a page Percentage jump 30% File: gg (beginning of file) G (end of file) Line numbers: :{number} or {number}G Find: f{character}, t{character}, F{character}, T{character} find/to forward/backward {character} on the current line , / ; for navigating matches Search: `/{regex}`` n / N for navigating matches ? : search in backward direction /ignore\c: ignore case 跳到对应括号: % (corresponding item) Edits insert: i: insert a: append A: append at the end of line insert line below / above: o/O delete: d{motion} dw: delete word d$/D: delete to end of line d0: delete to beginning of line dd: delete line change: c{motion} delete character: x substitude character: s :s/old/new/: substitute ’new’ for the first ‘old’ :s/old/new/g: substitute ’new’ for ‘old’ globally in the line :s/old/new/gc: with prompt :5,10s/old/new/g replace character: r R: 进入replace mode,连续替换字符 undo: u undo line: U redo: Ctrl-R copy (yank): y paste: p filp the case: ~ code complete Ctrl-N: Forward Ctrl-P: Backward Counts 3w move 3 words forward 5j move 5 lines down 7dw/d7w delete 7 words Misc 查看所在位置:Ctrl-G Visual Mode Visual: v Visual Line: V Visual Block: Ctrl-V Insert in Visual Mode: Shift-I Windows Split window: :sp/:vsp :sp {filename}: split window with file Ctrl-W+s/ Ctrl-W+v Move left/down/up/right: Ctrl-W+h/j/k/l, Move to next window: Ctrl-W+w Plugins Vim Awesome ctrlp.vim ctrlp.vim NERDTree NERDTree

January 1, 2000