跳转至

1-Linux-最常用的命令


镜像

https://mirrors.pku.edu.cn/Help
https://mirrors.cqu.edu.cn/

https://help.mirrors.cernet.edu.cn/


🟢开机自启

#!/bin/sh -e
echo '[Unit]
Description=/etc/autoup Compatibility
# 即在文件系统中存在/etc/autoup目录时,才启动该服务
ConditionPathExists=/etc/autoup

[Service]
Type=simple
ExecStart=/bin/sh /etc/autoup
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
Restart=always
RestartSec=3
StartLimitInterval=5

[Install]
WantedBy=multi-user.target'>/etc/systemd/system/autoup.service

echo '''#!/bin/sh -e
# this is Boot on auto-start
''' >/etc/autoup

启动服务,并加入自启
chmod +x /etc/autoup
systemctl start autoup.service
systemctl enable autoup.service

🟢代理

详细SSH

1
2
3
4
-L 正向代理 把远端的端口搬运到本地   
    ssh -L 本地IP:本地端口:远端(被搬运的IP):远端被搬运的端口  root@远端IP
-R 反向代理 将本地的端口搬运到远端   
    ssh -R 远端IP(搬运的目的地):远端端口: 本地IP:本地端口(被搬运的端口) root@远端IP   

代理的使用

export http_proxy=http://192.168.1.100:8080
export https_proxy=http://192.168.1.100:8080
export ALL_PROXY="socks5://10.66.30.100:60120"

export http_proxy=http://username:password@proxy_host:proxy_port
export https_proxy=https://username:password@proxy_host:proxy_port

proxy 账户密码的 编码替换清单
~ : %7E,         ! : %21    
@ : %40,         # : %23  
 $ : %24,         % : %25  
^ : %5E,         & : %26  
* : %2A,         ? : %3F   

🟢GIT 最常用

清理全部的历史提交记录

1
2
3
4
5
6
7
8
git checkout --orphan=bo
git add .
git commit -m 'commit'
git branch -D master
git branch -m master
git push -f origin master
git branch --set-upstream-to=origin/master
git pull
1
2
3
4
5
6
7
8
git checkout --orphan=bo
git add .
git commit -m 'commit'
git branch -D main
git branch -m main
git push -f origin main
git branch --set-upstream-to=origin/main
git pull
1
2
3
4
5
6
7
git checkout . && git clean -df 退回到上次的修改(会删除所有上次提交后至今的变更)
git reset <commit>          # 本地仓库覆盖到工作区(保存回退文件内容修改)
git reset --mixed <commit>  # 本地仓库覆盖到工作区(保存回退文件内容修改)
git reset --soft <commit>   # 本地仓库覆盖到工作区(保留修改并加到暂存区)
git reset --hard <commit>   # 本地仓库覆盖到工作区(不保留修改直接删除掉)
git commit --amend # 改变当前最新一次提交记录
git push origin :dev # 删除远端dev分支
1
2
3
4
5
github
git clone https://kaozb:私人令牌@github.com/kaozb/abcde.git

gitlab
https://username:私人令牌@gitlab.mibo.com/tp/OS/dpdk/dpdk-19.11.git
  • 使用到该命令,一般都是在当前分支正在开发,突然需要切到其他分支修改 bug 或者处理问题时使用。
  • 可以使用 git stash 命令把你当前的工作分支里的修改暂存起来,然后在切换到其他分支处理问题,
  • 等处理完成之后再切换到之前的分支,使用 git stash pop 把你之前的修改弹出,继续你之前未完成的工作。

🟢py 打包 | pip源 | req

pyinstaller -F --upx-dir="D:\01-Soft\upx" xxx.py

pip install pip -U -i https://mirrors.pku.edu.cn/pypi/web/simple
pip config set global.index-url https://mirrors.pku.edu.cn/pypi/web/simple
或者直接创建
1
2
3
4
5
6
7
mkdir -p ~/.config/pip/
echo '[global]
index-url = https://mirrors.pku.edu.cn/pypi/web/simple

[install]
trusted-host = pypi.douban.com
' > ~/.config/pip/pip.conf

运行:pipreqs ./ –encoding=utf-8

然后你就会看到这个项目所有以来环境的requirements.txt了

🟢文件复制

scp

1
2
3
ssh -p 22 root@10.2.3.4 tar -cf - -C /root log2.txt |tar -xf -
本地文件上传到远程设备
tar -cf - log.txt | ssh -p 22 root@10.2.3.4 tar -xf - -C /tmp/000/

nc

1
2
3
4
5
6
7
8
9
文件输入一方,需要添加 -N 参数  判断文件是否传输完毕
接收端监听
nc -l -p 9999 | tar xf -
发送端发送
tar cf - * | nc 10.2.3.4 9999

## 单个文件
nc -l [port] > file_name                # 服务端,指定文件名作为输出
nc -N [ip_addr] [port] < file_name          # 客户端,指定文件名作为输入

rsync

rsync -av -e 'ssh -p PORT_NUMBER' /path/to/source user@remote_host:/path/to/destination

🟢输出后台进程的打印行为

strace -p 3596 -e write -s 9999 -tt
-p 指定要跟踪的进程pid, 要同时跟踪多个pid, 重复多次-p选项即可
-s 当系统调用的某个参数是字符串时,最多输出指定长度的内容,默认是32个字节
-e 控制要跟踪的事件和跟踪行为,比如指定要跟踪的系统调用名称(-e trace=file 文件、network、process、signal、read、write )
-tt 在每行输出的前面,显示毫秒级别的

时间
-T 显示每次系统调用所花费的时间
-v 对于某些相关调用,把完整的环境变量,文件stat结构等打出来。
-f 跟踪目标进程,以及目标进程创建的所有子进程
-o 把strace的输出单独写到指定的文件

🟢ssh执行(登录后默认执行的文件)

(赋予可执行权限) ~/.ssh/rc


🟢禁用IPv6

临时禁用

sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1

永久禁用

echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf 
sysctl -p /etc/sysctl.conf

编辑 /etc/sysctl.conf 
#禁用全部    
net.ipv6.conf.all.disable_ipv6 = 1 
#禁用指定口子    
net.ipv6.conf.lo.disable_ipv6 = 1
#使之生效   
sysctl -p /etc/sysctl.conf

🟢bash-history 别名

# 将历史命令的时间戳、用户名和 IP 地址格式化
USER_IP=$(who -u am i 2>/dev/null | awk '{print $NF}')
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S - ${USER_IP}: "

# 增加历史命令文件的大小上限到 100 万条
export HISTFILESIZE=1000000

# 每次执行命令时将历史命令追加到文件,并重新加载历史命令到内存中
export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"

# 在历史命令文件中追加新命令,而不是覆盖旧命令;防止历史被清空
shopt -s histappend

/etc/bashrc 追加下面的命令

alias ll='ls -l -h --time-style="+%Y-%m-%d %H:%M:%S"'
alias now='date "+%Y-%m-%d %H:%M:%S"'
alias vpn='export https_proxy=http://xl.hoao.fun:7890 && export http_proxy=http://xl.hoao.fun:7890'
alias ll='ls -l -h --time-style="+%Y-%m-%d %H:%M:%S"'
alias dockerps='docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Command}}"'
alias push='git add . && git commit -m "`date`" && git push'

# 克隆仓库 写函数来完成git clone 的别名不能处理的空格问题
gc() {
  git clone https://账户:令牌@github.com/账户/$1.git
}

🟢挂载临时文件系统

mount -t tmpfs 12345 memory -o size=1G

mount: 这是一个用于挂载文件系统的命令。

-t tmpfs: 这是一个选项,指定要挂载的文件系统类型为 tmpfs。

12345: 这是一个标识符或者称为超级块标记(superblock specifier),它可以是任意非空字符串。
    该标识符用于唯一标识该挂载的 tmpfs 文件系统。在这个命令中,它被设置为 12345,你可以根据需要将其更改为其他值。

memory: 这是要创建的挂载点的名称或路径。它是一个用户定义的名称,可以根据需要进行更改。

-o size=1G: 这是一个选项,用于指定挂载的 tmpfs 文件系统的大小。在这个命令中,大小被设置为 1GB。
执行该命令后,系统将会在 memory 路径下创建一个大小为 1GB 的 tmpfs 文件系统,并使用标识符 12345 进行唯一标识
tmpfs 是一个临时文件系统,会在系统重新启动后被清空

🟢磁盘加密

apt-get install cryptsetup

步骤

1、创建分区
使用工具如 fdisk  parted 创建一个分区

2、设置加密
cryptsetup luksFormat /dev/sda
大写YES 确定并设置密码

3、打开加密分区
cryptsetup open /dev/sdaX mibo  # 输入密码
cryptsetup luksOpen /dev/sda1 mibo -d /root/idrs # 使用密钥文件
cryptsetup open UUID=a9d4c1d4-4600-4612-a85c-0609cf0cc89f mibo

4、格式化分区
mkfs.ext4 /dev/mapper/mibo

5、挂载使用
mount /dev/mapper/mibo /mnt/mibo

6、卸载关闭
umount /mnt/mibo
cryptsetup close mibo

7、修改密码
luksAddKey <device> [<new key file>] - add key to LUKS device
luksRemoveKey <device> [<key file>] - removes supplied key or key file from LUKS device
luksChangeKey <device> [<key file>] - changes supplied key or key file of LUKS device

🟢监控文件夹变化

watch.py
1
2
3
from watchfiles import watch
for changes in watch('C://'):
    print(changes)
watch.sh
1
2
3
4
5
6
7
8
9
#!/bin/bash

CONFIG_FILE="/var/www/html/nginx/default.conf"

while true; do
    if inotifywait -e modify,move,create,delete "$CONFIG_FILE"; then
       docker restart webserver
    fi
done

SVN 证书变化忽略

1
2
3
mv /usr/bin/svn /usr/bin/svn3
echo -e "/usr/bin/svn3 --non-interactive --trust-server-cert --trust-server-cert-failures=unknown-ca,cn-mismatch,expired,not-yet-valid,other $@" >> /usr/bin/svn
chmod +x /usr/bin/svn

sshd_config配置

###  root 只能从10.66.30.100登录

Match User root
       AllowUsers root@10.66.30.100


###    只允许从10.66.30.140登录这台设备     

AllowUsers *      # 默认情况下,允许所有用户登录
Match Address *,!10.66.30.140   # 针对所有其他地址,禁止所有用户登录
    DenyUsers *