Thursday, September 16, 2021

常用Linux Commands筆記

 Use rsync on my Windows 10 D:\SourceCode to sync folder from Linux's ~/SourceCode

rsync -r liudanny@<your IP address>:/home/liudanny/SourceCode /cygdrive/d/

List all users with their UID
awk -F: '{printf "%s:%s\n",$1,$3}' /etc/passwd

kubectl command completion in ~/.bashrc
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi

source <(kubectl completion bash)

使用update-alternatives管理多個版本的Python
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 3

另一種方式執行pip2 or pip3
python2 -m pip install --user --upgrade pip
python3 -m pip install --user --upgrade pip

將執行程式的output同步輸出到螢幕與檔案
$ my_program 2>&1 | tee my_log.txt

通訊埠掃瞄(port scan)
nc -z -v -n ip_address port1-port2

-z 只進行掃描, 不進行任何的資料傳輸
-v 顯示掃瞄訊息
-n 不進行 DNS 查詢

exp: nc -z -v -n 140.96.27.24 21-100

掃描多台主機
nmap www.hinet.net tw.yahoo.com www.google.com.tw
nmap -sP 140.96.27.0/24
nmap -sP 140.96.27.0.123-140

列出 Listening 狀態的連接埠
sudo netstat -tulp -n

-l listening 狀態
-t TCP
-u UDP
-p 連接埠
-n 不要讓 netstat 自動解析 DNS、連接埠名稱與使用者名稱

sudo netstat -r

-r 可以查看目前核心的路由表

多重條件過濾grep用法
./run_stap.sh | grep -e '80' -e '8080' > no_cluster_ip.txt

取代檔案內的line當含有特定Strings
sed -i "/- ip/c\ - ip: $CLUSTER_IP" apm_agent.yaml

取代檔案內的Strings
sed -i 's/<old_string>/<new_string>/g' *.yaml

遞迴搜尋文字
grep --include="*.*" -rnw './' -e "my_string"
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:

#This will only search through those files which have .c or .h extensions:
grep --include=\*.{cc,h,pbtxt} -rnw '/danny/tensorflow/' -e "RewriterConfig"

#This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

#This will exclude searching all the files ending with .o extension:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

#For directories its possible to exclude a particular directory(ies) through --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

grep --include=\BUILD -rnw './tensorflow' -e "framework"

grep -rnw './tensorflow' --include=\* -e "TextLineDataset" --exclude=\*.md --exclude=\*test* --exclude=\*go
grep -rnw './' --include=\*.pb* -e "Example"

Mount NFS
sudo mount -t nfs <nfs IP>:<data path> /mnt/D/nfs

Simple infinite loop command ( sleep 1 sec)
while [ 1 = 1 ]; do docker start da6; sleep 1; done
while true; do curl http://10.56.217.101/python8080; sleep 1; done

Simple infinite loop in shell script ( sleep 1 sec)
#!/bin/bash
echo "type Ctrl + C to break…"
while :
do
echo "Render fake traffics:"
echo "[1]"
curl http://10.10.101.12:30823/python8080
sleep 1
echo "Done"
done

查看iptables
# NAT table
sudo iptables -L -n -t nat

壓縮/解壓縮
解壓縮 tar.gz
tar -zxvf filename.tar.gz

壓縮 tar.gz
tar -zcvf filename.tar.gz /folder

解壓縮 tar.bz2
tar -jxvf filename.tar.bz2

壓縮 tar.bz2
tar -jcvf filename.tar.bz2 /folder

解開 .tar
tar -xvf filename.tar

打包 .tar
tar -cvf filename.tar /folder

Downgrade Linux Kernel
Install Linux kernel image, headers and modules
sudo apt install linux-image-4.15.0-117-generic
sudo apt install linux-headers-4.15.0-117-generic
sudo apt install linux-modules-extra-4.15.0-117-generic

Edit string in ‘/etc/default/grub’, then save and set default kernel
GRUB_DEFAULT=0
change to
GRUB_DEFAULT=saved

Display the grub menu
awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg
0 : Ubuntu
1 : Advanced options for Ubuntu
1>0 : Ubuntu, with Linux 5.3.0-46-generic
1>1 : Ubuntu, with Linux 5.3.0-46-generic (recovery mode)
1>2 : Ubuntu, with Linux 5.3.0-40-generic
1>3 : Ubuntu, with Linux 5.3.0-40-generic (recovery mode)
2 : Memory test (memtest86+)
3 : Memory test (memtest86+, serial console 115200)
4 : Windows 10 (on /dev/sdb1)

Setup boot option for Ubuntu
sudo grub-set-default '1>0'
sudo update-grub

No comments: