Linux thì có rất nhiều loại như Ubuntu, Redhat, Debian,… Nhưng hôm nay mình sẽ làm một vài câu lệnh cần thiết và thường gặp trên CentOS 7. Những câu lệnh này sẽ thường gặp cho các bạn làm IT quản trị hệ thống trong việc fix lỗi.
1. Check Version của Linux
cat /etc/os-release
Hoặc nếu bạn đang không biết nó đang là hệ điều hành nào thì có thể sử dụng cách này
cat /etc/*-release
2. Hiển thị các phiên bản kernel của Linux
#hostnamectl Kết quả: Static hostname: centos2 Icon name: computer-vm Chassis: vm Machine ID: 804f60cc25a848acaeca1da37f7dbecd Boot ID: 5afe786c1fdc44ffacd9b54b0130735f Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.49.1.el7.x86_64 Architecture: x86-64
3. Check thông tin máy chủ
cat /proc/cpuinfo Kết quả: processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 42 model name : Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz stepping : 7 microcode : 0x2f cpu MHz : 1396.829 cache size : 3072 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave avx hypervisor lahf_lm ssbd ibrs ibpb stibp tsc_adjust arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities bogomips : 2793.65 clflush size : 64 cache_alignment : 64 address sizes : 45 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 42 model name : Intel(R) Core(TM) i3-2367M CPU @ 1.40GHz stepping : 7 microcode : 0x2f cpu MHz : 1396.829 cache size : 3072 KB physical id : 2 siblings : 1 core id : 0 cpu cores : 1 apicid : 2 initial apicid : 2 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave avx hypervisor lahf_lm ssbd ibrs ibpb stibp tsc_adjust arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities bogomips : 2793.65 clflush size : 64 cache_alignment : 64 address sizes : 45 bits physical, 48 bits virtual power management:
4. Check RAM
Với lệnh check RAM thì nếu bạn check đơn giản thì dùng lệnh
free -m
Còn với những bạn yêu cầu check phức tạp hơn thì dùng lệnh
cat /proc/meminfo
5. Các lệnh restart, shutdown
# lệnh shutdown init 0 #lệnh restart init 6
6. Lệnh check ổ cứng
# df -h Có thể tìm hiểu thêm bằng lệnh # df --help
7. Lệnh tìm kiếm
Nếu bạn muốn tìm kiếm tất cả các file có phần mở rộng là .txt
find /home -name *.txt Trong đó: find là lệnh tìm kiếm /home là thư mục ta muốn tìm kiếm -name gọi tên file cần tìm *.txt là phần đuôi mở rộng có thể thay đổi
Còn nếu bạn muốn tìm kiếm các file rỗng
find /tmp -type f -empty Trong đó: Find là lệnh tìm kiếm /tmp là thư mục tìm kiếm trong nó -type là thể loại -empty là rỗng
Ngoài ra còn có các lệnh tìm kiếm khác khá hay (nâng cao)
Tìm kiếm file được chỉnh sửa trong vòng 50 ngày # find / -mtime 50 Tìm kiếm file được chỉnh sửa trong vòng 50 - 100 ngày # find / -mtime +50 –mtime -100 Tìm kiếm file vừa được tạo ra trong vòng 1 giờ # find / -cmin -60
Lệnh này dùng đễ check nhanh tình trạng chiếm dung lượng trong thư mục
Tìm kiếm 1 file dung lượng lớn nhất trong thư mục # du -h /etc | sort -n -r | head -n 10 Với trường hợp file ít thì có thể show tất cả file lớn nhỏ ra đễ xem # du -h Tìm kiếm dung lượng từng thư mục # du -h --max-depth=1 /etc
8. Lệnh Copy
Với copy thì ta dùng lệnh cp
Copy từ 1 file A sang file B nhưng vẫn giữ được file A # cp A.txt B.txt Copy nhiều file vào 1 thư mục # cp A.txt B.txt C.txt D.exe E.exe ./lab Copy file từ 1 thư mục khác sang thư mục mới # cp /root/report.xlsx /home/ Trong đó: report.xlsx là file cần copy /home là thư mục copy file vào
Ngoài ra các bạn có thể tự học và phân tích câu lệnh với trang web: https://explainshell.com/