Skip to content

Instantly share code, notes, and snippets.

@hoangdh
Last active December 20, 2023 10:00
Show Gist options
  • Save hoangdh/0f2bb0449a2544fa93834a852ef25c61 to your computer and use it in GitHub Desktop.
Save hoangdh/0f2bb0449a2544fa93834a852ef25c61 to your computer and use it in GitHub Desktop.
Một vài note nhanh khi thao tác với Linux - Tips and Tricks Linux/Unix

Đếm số file trong các thư mục Hệ thống Linux

ls / | grep -v proc | while read l; do count=$(find /$l/ -type f -print | wc -l); echo "/$l: $count"; done

Bonus: Đếm số file trong một thư mục bất kỳ

Ví dụ: /data/docker

dirparent="/data/docker"; 
find ${dirparent} -maxdepth 1 -type d | tail -n +2 | while read l; do count=$(find ${l}/ -type f -print | wc -l); echo "${l}: ${count}"; done | sort -k2 -n

Đọc một dòng số N của file

tail -n 10 file.txt | head -n1

## Or using sed

sed -n 10p file.txt

Đọc file từ dòng số N tới cuối file

tail -n +10 file.txt

Hiển thị các dòng tiếp theo sau từ khóa tìm được với GREP

grep -A 5 'pattern' /tmp/file.txt

Thay đổi tên file từ chữ HOA thành chữ thường

ls | while read l; do new=$(echo $l | tr '[:upper:]' '[:lower:]'); mv $l $new; done

Thay đổi không gian phân vùng

  • Unmount phân vùng
  • Kiểm tra phân vùng đang tồn tai
# parted /dev/vdb
> print
  • Chọn phân vùng và thay đổi không gian (VD: Phân vùng 1 và tăng tối đa kích thước không gian)
> resizepart 1 -1
> quit
  • Thay đổi kích thước phân vùng:
    • Với các định dạng ext3, ext4
e2fsck -f /dev/vdb1
resize2fs /dev/vdb1
  • Với xfs
xfs_growfs -d /dev/vdb1

Cài đặt PIP theo phiên bản python hiện tại

curl -sL https://bootstrap.pypa.io/get-pip.py | python

Sử dụng atop

atop -r /var/log/atop/atop_XXXXXXXXX -b 15:00 -e 15:30

Xem lại lịch sử sử dụng tài nguyên trong khoảng thời gian từ 15:00 -> 15:30 Với tùy chọn:

  • -r: Đọc file. Mặc định lưu trữ tại /var/log/atop
  • -b: Thời gian bắt đầu
  • -e: Thời gian kết thúc

Lọc thông tin với phím tắt sau:

  • m: Xem thông tin về Memory
  • d: Xem thông tin về Disk;
  • n: Xem thông tin về Network;
  • v: Lọc các tiến trình; loại bỏ các tiến trình giống nhau
  • c: Xem câu lệnh của tiến trình.

Tham khảo:

Support về DB

Tạo user MySQL

Xem thông tin user đã có trên hệ thống:

Tìm kiếm chính xác:

select user,host,authentication_string from mysql.user where user='user1';

-> Chú ý: Với bản MySQL < 5.6, thay thế authentication_string thành password.

Tìm kiếm tương đối:

select user,host,authentication_string from mysql.user where user like '%user1%';
  • Xác định chính xác thông tin quyền hạn, host, CSDL, bảng biểu,...
  • Chỉ cho phép 20 kết nối/user

Ví dụ:

GRANT USAGE ON 	database.table 	TO 'user1'@'host1' IDENTIFIED BY 'passw0rD' WITH MAX_USER_CONNECTIONS 20;
  • Với password hash có sẵn:
GRANT USAGE ON 	database.table 	TO 'user1'@'192.168.2.251' IDENTIFIED BY PASSWORD '*XXX' WITH MAX_USER_CONNECTIONS 20;

Tạo csdl MySQL với charset UTF-8

CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Đổi thông tin user

RENAME USER old@1.2.3.4 TO new@1.2.3.5

Khai báo thông tin kết nối DB cho client trong ~/.my.cnf

Mẫu như sau:

[client]
user = root
password = 'YBKw4'
host = 10.5.0.81
port = 33066

Xem quyền của User sử dụng pt-show-grants

pt-show-grants --only user1@192.168.2.111

Mydumper lỗi thư viện ssl1.0.0

Lỗi:

mydumper: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory

Khắc phục:

wget http://security.ubuntu.com/ubuntu/pool/main/o/openssl1.0/libssl1.0.0_1.0.2n-1ubuntu5.6_amd64.deb
apt install ./libssl1.0.0_1.0.2n-1ubuntu5.6_amd64.deb

https://askubuntu.com/a/1344529

UFW

Xem trạng thái

ufw status

Cho phép truy cập

  • Mở một port cố định

ufw allow 3306/tcp

  • Mở range port

ufw allow 6000:6007/tcp

  • Cho phép dải IP được phép sử dụng port 3306

ufw allow from 15.15.15.0/24 to any port 3306

  • Cho phép truy cập từ địa chỉ xác đinh đến 3306

ufw allow from 15.15.15.15 to any port 3306 proto tcp

Chặn truy cập

  • Chặn truy cập từ địa chỉ IP xác định

ufw deny from 15.15.15.14

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