# Monitor traffic and http request - cái này rất hay dùng theo dõi dạng realtime trên terminal các request tới server nhé
# Cài đặt
yum install tcpdump -y
# tcpdump filter for HTTP GET
tcpdump -s 0 -A 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420'
# Theo dõi card mạng
yum install nethogs -y
nethogs
yum -y install nload
nload venet0 -u M
nload venet0 -u M devices eth2
# Xem tổng số connection tới server - realtime
watch -n 1 'netstat -an | grep :80 | wc -l'
# Xem số connection ứng với từng IP
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head -n 30
# Xem số connection ứng với từng port
netstat -tuna | awk -F':+| +' 'NR>2{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
# Change hostname <= https://support.rackspace.com/how-to/centos-hostname-change/
# Đổi trong file sau và reboot lại
vi /etc/sysconfig/network
# Nếu không muốn reboot thì set hostname lại bằng lệnh sau và mở terminal khác lên để thấy sự thay đổi
hostname new_name
# Lệnh đồng bộ lại hostname của DigitalOcean khi restore từ mộ snapshot
# Vì khi restore thì vps mới sẽ vẫn bị set hostname theo vps đã tạo ra snapshot đó
HNAME="$(cat /etc/hosts | grep "127.0.0.1 www" | awk '{print $2}')" && sed -i -E "s/HOSTNAME=[^\
]+/HOSTNAME=$HNAME/g" /etc/sysconfig/network && hostname $HNAME && hostname
# Test syntax of bash file before run
sh -n script-name
# Fix error: /bin/bash^M: bad interpreter
# Mở file bằng vi hoặc vim để ở chế độ lệnh và gõ một trong 2 lênh sau rồi lưu lại
# https://stackoverflow.com/a/5514351
:set fileformat=unix
:set ff=unix
# wget save với chỉ định tên file
wget -O example.html https://www.electrictoolbox.com/wget-save-different-filename/
# wget with ipv4 - ipv6
wget -4 -O example.html https://www.electrictoolbox.com/wget-save-different-filename/
wget -6 -O example.html https://www.electrictoolbox.com/wget-save-different-filename/
# Lấy danh sách filename
ls -lA | awk -F':[0-9]* ' '/:/{print $2}'
# Xóa hết danh sách file
cat 1.txt | xargs echo rm | sh
# Lọc hết những file rỗng (empty)
find /tmp -type f -empty
# Lọc hết file có chứa keyword, từ khóa, nội dung
find / -type f -exec grep -H 'keyword' {} \\;
# Xem 1 file liên tục từ dòng 30 đến dòng 60
watch -n 1 "sed -n '30,60p' nginx.conf"
# Xem 1 file liên tục 30 dòng đầu tiên
watch -n 1 "head -30 nginx.conf"
# Xem 1 file liên tục 30 dòng cuối cùng
watch -n 1 "tail -n 30 /home/nginx/logs/nginx-error.log"
watch -n 1 "tail -n 35 1.txt"
watch -n 1 "tail -n 35 2.txt"
watch -n 1 "tail -n 35 3.txt"
watch -n 1 "tail -n 35 4.txt"
# Limit open file
# Các ứng dụng hay bị lỗi: Too many open files
# nginx với traffic cao hoặc các ứng dụng ghi nhiều file
vi /etc/security/limits.conf
# Thêm cái này vào cuối file - logout và login lại
* hard nofile 500000
* soft nofile 500000
root hard nofile 500000
root soft nofile 500000
# Kiểm tra việc limit
ulimit -Hn
ulimit -Sn
# Kiểm tra port được sử dụng bởi thằng nào
netstat -nlp | grep :80
# Kiểm tra toàn bộ
netstat -tulpn
# Đếm tổng số file trong thư mục hiện tại
find . -type f | wc -l
cp -a /source/. /dest/
# List all user
cat /etc/passwd
# mysql import
mysql -u username -p database_name < file.sql
mysql -u root -p < database.sql
# Mysql export
mysqldump -u root -p database-name > backup.sql
mysqldump -u root -p --all-databases > database.sql
# Mysql repair all table
mysqlcheck -A --auto-repair -u root -p
# Hardlink và Softlink trong hệ thống file của linux
# 2 cái đều trỏ về một vùng nhớ chứa dữ liệu
# Softlink là 1 dạng shortcut nên khi xoá source thì sẽ mất dữ liệu
# Hardlink khi xoá 1 trong 2 (Hardlink hoặc source) thì dữ liệu vẫn còn nguyên và thằng còn lại vẫn còn dữ liệu
# Ứng dụng khi cần di chuyển dữ liệu nhưng vẫn muốn giữ lại cấu trúc đường dẫn thư mục (ví dụ thư mục: uploads, images.....)
# Source SoftLink
ln -s /home/jake/doc/test/2000/something /home/jake/xxx
# Source HardLink
ln /home/jake/doc/test/2000/something /home/jake/xxx
# Test tốc độ ổ cứng
dd if=/dev/zero of=/1GB bs=1024 count=1024k
dd if=/dev/zero of=/512MB bs=512 count=1024k
# Check KMV or Openvz => nếu file sau có thì là openvz
cat /proc/user_beancounters
# Kiểm tra phiên bản của Centos version
rpm --query centos-release
# Kill process
kill -SIGTERM PID
for pid in $(ps -ef | grep "ffmpeg" | awk '{print $2}'); do echo $pid & kill -9 $pid; done
for pid in $(ps -ef | grep "abc.com" | awk '{print $2}'); do echo $pid && kill -9 $pid; done
# Zip toàn bộ thư mục hiện tại
zip -r sincos.zip ./*
# Zip toàn bộ thư mục hiện tại bỏ qua 1 số thư mục con
zip -r sincos.zip ./* -x ./bo-qua-1/**\\* ./bo-qua-2/**\\*
# Khắc phục lỗi unknown host trong terminal khi ssh tới 1 server lần đầu tiên
# Các 1:
ssh -oStrictHostKeyChecking=no root@23.227.178.99
# Cách 2: Vào ~/.ssh/config thêm dòng nội dung sau:
StrictHostKeyChecking no
# Hiện ngày giờ theo timezone
echo `TZ=Asia/Saigon date`
# Update date and time => Nhiều khi server sai giờ
yum install ntp
ntpdate pool.ntp.org
# Nếu muốn tự động sync thì thêm phần sau
chkconfig ntpd on
ntpdate pool.ntp.org
service ntpd start
# Fix Centos: BAD PASSWORD: it is based on a dictionary word
# https://www.linuxquestions.org/questions/linux-software-2/bad-password-it-is-based-on-a-dictionary-word-449149/
vi /etc/pam.d/system-auth
The original file looks like this
------------------------------------------------------------------------------------
password requisite pam_cracklib.so try_first_pass retry=3
password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok
password required pam_deny.so
------------------------------------------------------------------------------------
Comment all the three lines
------------------------------------------------------------------------------------
# password requisite pam_cracklib.so try_first_pass retry=3
# password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok
# password required pam_deny.so
------------------------------------------------------------------------------------
Add this line
------------------------------------------------------------------------------------
password sufficient /lib/security/$ISA/pam_unix.so nullok md5 shadow
------------------------------------------------------------------------------------
It will look like this now
------------------------------------------------------------------------------------
# password requisite pam_cracklib.so try_first_pass retry=3
# password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok
# password required pam_deny.so
password sufficient /lib/security/$ISA/pam_unix.so nullok md5 shadow
------------------------------------------------------------------------------------
Note:
------------------------------------------------------------------------------------
If you run authconfig, the file /etc/pam.d/system-auth will be overwritten
------------------------------------------------------------------------------------Tuyển tập lệnh Linux by sincos
sincos17/04/20191421 lượt xem12 bình luận
Nghĩ đi nghĩ lại chả có gì chia sẻ vì trong này toàn cao thủ. Thôi thì có mỗi cái này do mình sưu tập và note lại thành file dùng dần phòng khi quên đỡ mất công search lại. Nay chia sẻ với anh em.
Bình luận
Đang tải...
Đang kiểm tra đăng nhập...
