Skip to content

Instantly share code, notes, and snippets.

@kaichao
Last active August 13, 2023 08:37
Show Gist options
  • Save kaichao/096fe04e4c2a1a7ec13a1dae1e1a372e to your computer and use it in GitHub Desktop.
Save kaichao/096fe04e4c2a1a7ec13a1dae1e1a372e to your computer and use it in GitHub Desktop.
常用shell命令

CentOS7上的git2

yum -y remove git git-*
yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
yum -y install git

双远端rsync设置

  • 源端、本地、目标端使用相同用户名
  • 将源端、本地的pub文件拷贝到目标端(ssh-copy-id或直接文本编辑authrized_keys)
  • 用以下命令执行拷贝
ssh -R localhost:50000:DEST-HOST:22 SRC-HOST 'cd /SOURCE-BASE-DIR;rsync -e "ssh -p 50000 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" -RPut RELATIVE-PATH localhost:/DEST-BASE-DIR'

命令行git clone设置代理

git config --global http.proxy 'socks5://127.0.0.1:1080'

git config --global --unset http.proxy

小文件打包传输

# 远端向本地传输
ssh myhost 'tar cf - /tmp/kubernetes | gzip' | pv |gzip -d | tar xf -
# 本地向远端传输
tar cf - ./devel | gzip | pv | ssh root@host-do 'gzip -d | tar xf -'
# 两个远端节点间传输
ssh azure-hk 'tar cf - /home/nemo/kaichao/devel/apt-repo/APT-DATA/dists | gzip' | pv | ssh fast 'gzip -d | tar xf -'

基于netcat的数据传输

# server-side:
sudo nc -l -p port | tar -zxf - #l 参数用于监听
sudo nc -l -p port > ufile
# client-side:
tar -zcf - ufile | sudo nc host port
sudo nc host port < ufile

docker镜像跨节点迁移

# 本地向远端迁移
docker save debian:latest | pxz | pv | ssh root@myhost 'xz -d | docker load'
#远端向本地迁移
ssh root@myhost 'docker save debian:latest | gzip' | pv |gzip -d | docker load
# 两个远端节点间迁移
ssh root@do-sgp 'docker save secuone/jibri:latest | gzip' | pv | ssh fast 'gzip -d | docker load'

获取本机IP地址

ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}'
/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d "addr:"

自动确认shell中的交互

sh -c '/bin/echo -e "meet.kaichao.me\n2\n/etc/ssl/meet.kaichao.me.key\n/etc/ssl/meet.kaichao.me.crt\n"| apt-get install -y --allow-unauthenticated jitsi-meet'

wget下载文件并通过管道运行

wget -qO -  https://download.jitsi.org/jitsi-key.gpg.key | apt-key add - 

aria2c并行下载

aria2c -s16 -x16 -k1M http://openslr.magicdatatech.com/resources/18/data_thchs30.tgz

ssh/scp/rsync跳板(OpenSSh >= 7.3)

A -- ssh --> B -- ssh --> C -- ssh --> D

ssh -J user@B,user@C user@D
scp -o 'ProxyCommand ssh username@B -W %h:%p' someFile username@C:/some/path
rsync -azv -e 'ssh -A -J user@B,user@C' someFile user@D:/some/path

dpkg vs rpm

1. 列出所有的包:rpm -qa         ; dpkg -l
2. 查询包中文件:rpm -ql package ; dpkg-query -L package
3. 查询文件归属:rpm -qf file    ; dpkg-query -S  file

查找重复目录/文件

find . -name d | while read line; do echo ${line##*/} ${line}; done | sort > /tmp/sorted-dir

测试端口连通性

echo > /dev/tcp/10.255.11.1/22 && echo "SSH port is open"
@kaichao
Copy link
Author

kaichao commented Aug 13, 2023

rename docker volume:

docker volume create --name new_volume && docker run --rm -it -v old_volume:/from -v new_volume:/to alpine ash -c 'cd /from ; cp -av . /to' && docker volume rm old_volume

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment