Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Giới thiệu

Mở khóa toàn bộ tiềm năng của hệ thống Linux của bạn với hướng dẫn toàn diện này về các lệnh Linux thiết yếu. Cho dù bạn là quản trị viên dày dạn kinh nghiệm hay mới bắt đầu, việc thành thạo các lệnh này là rất quan trọng để quản lý máy chủ, viết tập lệnh và khắc phục sự cố hiệu quả. Trong hướng dẫn này, bạn sẽ tìm hiểu các lệnh mạnh mẽ và được sử dụng thường xuyên nhất để quản lý tệp, kiểm soát quy trình, quyền truy cập của người dùng, cấu hình mạng và gỡ lỗi hệ thống.

Bạn sẽ tìm hiểu hơn 50 lệnh Linux phải biết điều đó sẽ biến bạn thành một người sử dụng Linux thành thạo. Từ cơ bản đến nâng cao, những lệnh này sẽ trở thành công cụ hỗ trợ bạn giải quyết bất kỳ nhiệm vụ nào xảy ra.

Bài học chính

  • Các lệnh Linux được sắp xếp theo mục đích:quản lý tệp (ls , cp , mv , rm ), kiểm soát quá trình (ps , kill , top ), quyền của người dùng (chmod , chown , sudo ) và kết nối mạng (ssh , ping , wget ).
  • Số man lệnh cung cấp tài liệu tích hợp cho mọi lệnh Linux và là tài liệu tham khảo chính khi bạn cần chi tiết về cú pháp hoặc tùy chọn.
  • Sử dụng grep để tìm kiếm đầu ra văn bản hoặc tập tin, head /tail để xem trước nội dung tập tin và diff /cmp để so sánh các tập tin.
  • Quyền truy cập tệp được quản lý bằng chmod (thay đổi bit chế độ) và chown (thay đổi chủ sở hữu), điều này rất quan trọng đối với việc kiểm soát quyền truy cập và bảo mật máy chủ.
  • Các lệnh như tar , zip , và unzip xử lý việc lưu trữ và nén; sshscp xử lý truy cập từ xa và truyền tệp một cách an toàn.
  • Lệnh quản lý quy trình (ps , kill , killall , top ) rất cần thiết để theo dõi tình trạng hệ thống và chấm dứt các quy trình không phản hồi.

Điều kiện tiên quyết

Chúng tôi sẽ chạy các lệnh này trên máy chủ Ubuntu, nhưng bạn có thể làm theo trên bất kỳ bản phân phối Linux hiện đại nào. Bạn có thể thiết lập máy chủ Ubuntu cho hướng dẫn này bằng cách làm theo hướng dẫn của chúng tôi về Thiết lập máy chủ ban đầu trên Ubuntu.

Triển khai các ứng dụng giao diện người dùng của bạn từ GitHub bằng Nền tảng ứng dụng DigitalOcean. Hãy để DigitalOcean tập trung vào việc mở rộng ứng dụng của bạn.

Hãy bắt tay ngay vào vấn đề nào!

50 lệnh Linux hàng đầu bạn phải biết khi là người dùng thông thường

  1. l - Lệnh được sử dụng thường xuyên nhất trong Linux để liệt kê các thư mục
  2. pwd - In lệnh thư mục làm việc trên Linux
  3. cd - Lệnh Linux để điều hướng qua các thư mục
  4. mkdir - Lệnh dùng để tạo thư mục trong Linux
  5. mv - Di chuyển hoặc đổi tên tệp trong Linux
  6. cp - Cách sử dụng tương tự như mv nhưng để sao chép tệp trong Linux
  7. rm - Xóa tập tin hoặc thư mục
  8. chạm - Tạo tập tin trống/trống
  9. ln - Tạo liên kết tượng trưng (phím tắt) tới các file khác
  10. rõ ràng - Xóa màn hình thiết bị đầu cuối
  11. mèo - Hiển thị nội dung file trên terminal
  12. tiếng vang - In bất kỳ văn bản nào theo lệnh
  13. ít hơn - Lệnh Linux để hiển thị kết quả đầu ra được phân trang trong thiết bị đầu cuối
  14. người đàn ông - Truy cập các trang hướng dẫn cho tất cả các lệnh Linux
  15. không tên - Lệnh Linux để lấy thông tin cơ bản về hệ điều hành
  16. whoami - Lấy tên người dùng đang hoạt động
  17. tar - Lệnh giải nén, nén file trong linux
  18. grep - Tìm kiếm một chuỗi trong một đầu ra
  19. đầu - Trả về số dòng xác định từ đầu
  20. đuôi - Trả về số dòng đã chỉ định từ dưới lên
  21. khác biệt - Tìm sự khác biệt giữa hai tập tin
  22. cmp - Cho phép bạn kiểm tra xem 2 file có giống nhau không
  23. liên lạc - Kết hợp chức năng của diff và cmp
  24. sắp xếp - Lệnh Linux để sắp xếp nội dung của file khi xuất ra
  25. xuất - Xuất các biến môi trường trong Linux
  26. mã zip - Tệp zip trong Linux
  27. giải nén - Giải nén file trong Linux
  28. ssh - Lệnh Shell an toàn trong Linux
  29. dịch vụ - Lệnh Linux để khởi động và dừng dịch vụ
  30. ps - Hiển thị các tiến trình đang hoạt động
  31. giết và giết tất cả - Tiêu diệt các tiến trình đang hoạt động theo ID hoặc tên tiến trình
  32. df - Hiển thị thông tin hệ thống tập tin đĩa
  33. gắn kết - Gắn hệ thống tập tin vào Linux
  34. chmod - Lệnh thay đổi quyền truy cập tập tin
  35. chown - Lệnh cấp quyền sở hữu file hoặc thư mục
  36. ifconfig - Hiển thị giao diện mạng và địa chỉ IP
  37. theo dõi - Theo dõi tất cả các bước nhảy mạng để đến đích
  38. bỏ đi - Tải file trực tiếp từ internet
  39. ufw - Lệnh tường lửa
  40. iptable - Tường lửa cơ sở cho tất cả các tiện ích tường lửa khác có thể giao tiếp với
  41. apt, pacman, ngon, vòng/phút - Trình quản lý gói tùy theo nhà phân phối
  42. sudo - Lệnh leo thang đặc quyền trong Linux
  43. cal - Xem lịch dòng lệnh
  44. bí danh - Tạo lối tắt tùy chỉnh cho các lệnh bạn thường xuyên sử dụng
  45. đ - Được sử dụng chủ yếu để tạo các thanh USB có khả năng khởi động
  46. ở đâu - Xác định vị trí các trang nhị phân, nguồn và thủ công cho lệnh
  47. là gì - Tìm xem lệnh dùng để làm gì
  48. trên cùng - Xem trực tiếp các quy trình đang hoạt động cùng với việc sử dụng hệ thống của chúng
  49. useradd và usermod - Thêm người dùng mới hoặc thay đổi dữ liệu người dùng hiện có
  50. mật khẩu - Tạo hoặc cập nhật mật khẩu cho người dùng hiện tại

Lệnh tệp và thư mục

Lệnh Mô tả Ví dụ Nội dung thư mục lsList.ls thư mục cdChange.cd /path/to/directory pwdHiển thị thư mục hiện tại.pwd mkdirTạo một thư mục mới.mkdir new_directory rmdirXóa thư mục trống.rmdir empty_directory rmDelete tập tin hoặc thư mục.rm file.txt touchTạo một tệp trống.touch new_file.txt cpSao chép tập tin hoặc thư mục.cp file.txt /path/to/destination mvDi chuyển hoặc đổi tên tập tin.mv file.txt /path/to/new_location catHiển thị nội dung tệp.cat file.txt tập tin nano / vimEdit trong terminal.nano file.txt findTìm kiếm tệp trong hệ thống phân cấp thư mục.find . -name "file.txt" grepTìm kiếm văn bản bằng cách sử dụng các mẫu.grep "pattern" file.txt tarArchive và nén tập tin.tar -cvf archive.tar file1.txt file2.txt dfHiển thị mức sử dụng đĩa của hệ thống tệp.df duShow kích thước thư mục/tập tin.du -sh /path/to/directory chmodThay đổi quyền truy cập tệp.chmod 755 file.txt chownThay đổi chủ sở hữu tệp.chown user:group file.txt mountGắn hệ thống tập tin.mount /dev/sdb1 /mnt umountUnmount một hệ thống tập tin.umount /mnt

Các lệnh kết nối mạng

Lệnh Mô tả Cách sử dụng mẫu pingTest kết nối với máy chủ.ping google.com ifconfig / ip aHiển thị giao diện mạng.ifconfig hoặc ip a netstat / ssHiển thị kết nối mạng.netstat -tuln hoặc ss -tuln wgetTải xuống tệp qua HTTP/FTP.wget http://example.com/file.zip CurlTruyền dữ liệu bằng cú pháp URL.curl -O http://example.com/file.zip nc (Netcat)Gỡ lỗi mạng và truyền dữ liệu.nc -zv 192.168.1.1 80 tcpdumpChụp và phân tích các gói mạng.tcpdump -i eth0 iptablesĐịnh cấu hình quy tắc tường lửa.iptables -A INPUT -p tcp --dport 22 -j ACCEPT tracerouteTrace đường dẫn các gói tin đến máy chủ mạng.traceroute example.com nslookupQuery DNS để lấy tên miền hoặc ánh xạ địa chỉ IP.nslookup example.com sshKết nối an toàn với máy chủ từ xa.ssh user@example.com

Lệnh giám sát hệ thống và quy trình

Lệnh Mô tả Lệnh mẫu psHiển thị các tiến trình đang chạy.ps aux topDynamic trình xem quy trình.top htopPhiên bản nâng cao của top.htop killGửi tín hiệu đến một tiến trình.kill <PID> killallKill xử lý theo tên.killall <process_name> thời gian hoạt độngThời gian hoạt động và tải của hệ thống.uptime whoamiNgười dùng đã đăng nhập hiện tại.whoami envDisplay các biến môi trường.env lệnh gọi hệ thống straceTrace của một quy trình.strace -p <PID> systemctlQuản lý dịch vụ systemd.systemctl status <service_name> tạp chíXem nhật ký hệ thống.journalctl -xe freeDisplay mức sử dụng bộ nhớ.free -h vmstatBáo cáo số liệu thống kê bộ nhớ ảo.vmstat 1 iostatBáo cáo số liệu thống kê CPU và I/O.iostat lsofList mở tệp theo quy trình.lsof Thông báo bộ đệm vòng hạt nhân dmesgPrint.dmesg

Lệnh quản lý quyền và người dùng

Lệnh Mô tả Lệnh mẫu passwdThay đổi mật khẩu người dùng.passwd <username> adduser / useraddThêm người dùng mới.adduser <username> hoặc useradd <username> deluser / userdelXóa người dùng.deluser <username> hoặc userdel <username> usermodSửa đổi tài khoản người dùng.usermod -aG <group> <username> nhómHiển thị tư cách thành viên nhóm.groups <username> Các lệnh sudoExecute dưới quyền root.sudo <command> chageChange thông tin hết hạn mật khẩu người dùng.chage -l <username> idHiển thị thông tin nhận dạng người dùng.id <username> newgrpĐăng nhập vào nhóm mới.newgrp <group>

Lệnh truyền tệp và đồng bộ hóa

Lệnh Mô tả Lệnh mẫu scpSao chép an toàn các tệp qua SSH.scp user@remote:/path/to/file /local/destination rsyncĐồng bộ hóa hiệu quả các tập tin và thư mục.rsync -avz /local/directory/ user@remote:/path/to/destination ftpTruyền tệp bằng Giao thức truyền tệp.ftp ftp.example.com sftpTruyền tệp an toàn bằng Giao thức truyền tệp SSH.sftp user@remote:/path/to/file wgetTải xuống tệp từ web.wget http://example.com/file.zip CurlTruyền dữ liệu từ hoặc tới máy chủ.curl -O http://example.com/file.zip

Lệnh xử lý văn bản

Lệnh Mô tả Lệnh mẫu awkPattern quét và xử lý.awk '{print $1}' file.txt trình soạn thảo sedStream để lọc/sửa đổi văn bản.sed 's/old/new/g' file.txt cutXóa các phần khỏi dòng văn bản.cut -d':' -f1 /etc/passwd SortSắp xếp dòng văn bản.sort file.txt grepTìm kiếm mẫu trong văn bản.grep 'pattern' file.txt wcĐếm từ, dòng và ký tự.wc -l file.txt dánHợp nhất các dòng tệp.paste file1.txt file2.txt joinNối các dòng của hai tệp trên một trường chung.join file1.txt file2.txt headOutxuất phần đầu tiên của tập tin.head -n 10 file.txt tailOutxuất phần cuối cùng của tệp.tail -n 10 file.txt

Các lệnh tắt và tiện ích Shell

Lệnh Mô tả Lệnh mẫu bí danhTạo lối tắt cho lệnh.alias ll='ls -la' unaliasXóa bí danh.unalias ll historyHiển thị các lệnh đã nhập trước đó.history clearXóa màn hình terminal.clear khởi động lạiKhởi động lại hệ thống.reboot tắt máyTắt nguồn hệ thống.shutdown now dateHiển thị hoặc đặt ngày giờ hệ thống.date echoHiển thị một dòng văn bản.echo "Hello, World!" sleepDelay trong một khoảng thời gian nhất định.sleep 5 timeĐo thời gian thực hiện lệnh.time ls watchThực hiện chương trình theo định kỳ, hiển thị toàn màn hình đầu ra.watch -n 5 df -h

Bây giờ chúng ta hãy đi sâu hơn một chút vào từng lệnh này và hiểu chúng chi tiết hơn. Chúng tôi đã có rất nhiều bài viết hiện có cho từng lệnh riêng lẻ đó. Để thuận tiện cho bạn, chúng tôi sẽ thêm liên kết đến tất cả các bài viết hiện có và tiếp tục cập nhật bài viết khi có chủ đề mới.

Số ls lệnh trong Linux

ls lệnh được sử dụng để liệt kê các tập tin và thư mục trong thư mục làm việc hiện tại. Đây sẽ là một trong những lệnh Linux được sử dụng thường xuyên nhất mà bạn phải biết.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Như bạn có thể thấy trong hình trên, việc sử dụng lệnh mà không có bất kỳ đối số nào sẽ cho chúng ta một kết quả có tất cả các tệp và thư mục trong thư mục. Lệnh này mang lại rất nhiều sự linh hoạt trong việc hiển thị dữ liệu ở đầu ra.

Tìm hiểu thêm về cách sử dụng ls lệnh

Quay lại danh sách lệnh ↑

Số pwd lệnh trong Linux

pwd lệnh cho phép bạn in thư mục làm việc hiện tại trên thiết bị đầu cuối của bạn. Đó là một lệnh rất cơ bản và giải quyết rất tốt mục đích của nó.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Bây giờ, dấu nhắc terminal của bạn thường phải có thư mục đầy đủ. Nhưng trong trường hợp không, đây có thể là một lệnh nhanh để xem thư mục bạn đang ở. Một ứng dụng khác của lệnh này là khi tạo tập lệnh, trong đó lệnh này có thể cho phép chúng ta tìm thư mục nơi tập lệnh đã được lưu.

Quay lại danh sách lệnh ↑

Số cd lệnh trong Linux

Trong khi làm việc trong terminal, việc di chuyển trong các thư mục là điều cần thiết. Lệnh cd là một trong những lệnh Linux quan trọng bạn phải biết và nó sẽ giúp bạn điều hướng qua các thư mục. Chỉ cần gõ cd theo sau là thư mục, như hiển thị bên dưới.

root@ubuntu:~# cd <directory path>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Như bạn có thể thấy trong lệnh trên, tôi chỉ cần gõ cd /etc/ để vào thư mục /etc. Chúng tôi đã sử dụng pwd lệnh in thư mục làm việc hiện tại.

Quay lại danh sách lệnh ↑

Số mkdir lệnh trong Linux

mkdir lệnh cho phép bạn tạo các thư mục từ bên trong terminal.

root@ubuntu:~# mkdir <folder name>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Như bạn có thể thấy trong ảnh chụp màn hình ở trên, chúng tôi đã tạo một thư mục mới chỉ bằng lệnh đơn giản này.

Quay lại danh sách lệnh ↑

Số cpmv lệnh

cpmv các lệnh tương đương với lệnh sao chép-dán và cắt-dán trong Windows. Nhưng vì Linux thực sự không có lệnh đổi tên tệp nên chúng tôi cũng sử dụng mv lệnh đổi tên tập tin và thư mục.

root@ubuntu:~# cp <source> <destination>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Trong lệnh trên, chúng ta đã tạo một bản sao của tệp có tên Sample. Hãy xem điều gì sẽ xảy ra nếu chúng ta sử dụng mv ra lệnh theo cách tương tự.

root@ubuntu:~# mv <source> <destination>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Trong trường hợp trên, vì chúng tôi đang di chuyển tệp trong cùng một thư mục nên nó hoạt động như một sự đổi tên. Tên tệp hiện đã được thay đổi.

Quay lại danh sách lệnh ↑

Số rm lệnh trong Linux

Trong phần trước, chúng tôi đã xóa tệp Sample-Copy. Lệnh rm dùng để xóa các tập tin, thư mục và là một trong những lệnh Linux quan trọng mà bạn phải biết.

root@ubuntu:~# rm <file name>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Để xóa một thư mục, bạn phải thêm -r tranh luận với nó. Không có -r đối số, rm lệnh sẽ không xóa thư mục.

root@ubuntu:~# rm -r <folder/directory name>

-r cờ trong rm lệnh trong Linux là viết tắt của “đệ quy ”. Khi được sử dụng với rm lệnh, nó sẽ loại bỏ không chỉ tệp được chỉ định mà còn tất cả các thư mục con của nó và các tệp trong các thư mục con đó theo cách đệ quy.

Lưu ý: Điều quan trọng là phải cẩn thận khi sử dụng rm lệnh bằng -r flag, vì nó có thể xóa nhanh chóng và vĩnh viễn một số lượng lớn tệp và thư mục. Bạn nên sử dụng -i cờ kết hợp với -r cờ, sẽ nhắc bạn xác nhận trước khi xóa từng tệp và thư mục.

Ví dụ:để xóa mydir thư mục và nội dung của nó có xác nhận, bạn có thể sử dụng lệnh này:

root@ubuntu:~# rm -ri mydir

Điều này sẽ nhắc bạn xác nhận trước khi xóa từng tệp và thư mục trong mydir thư mục.

Quay lại danh sách lệnh ↑

Số touch lệnh trong Linux

touch lệnh trong Linux tạo một tệp trống hoặc cập nhật dấu thời gian của tệp hiện có.

root@ubuntu:~# touch <file name>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Quay lại danh sách lệnh ↑

Số ln lệnh trong Linux

Để tạo liên kết tới file khác, chúng ta sử dụng ln lệnh. Đây là một trong những lệnh Linux quan trọng nhất mà bạn nên biết nếu định làm quản trị viên Linux.

root@ubuntu:~# ln -s <source path> <link name>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

-s cờ tạo một liên kết tượng trưng (còn được gọi là liên kết tượng trưng hoặc liên kết mềm) tới một tệp hoặc thư mục. Liên kết tượng trưng là một loại tệp đặc biệt hoạt động như một lối tắt hoặc con trỏ tới tệp hoặc thư mục khác.

Theo mặc định, ln lệnh sẽ tạo liên kết cứng thay vì các liên kết tượng trưng hoặc mềm.

Lưu ý: Giả sử bạn có một tập tin văn bản. Nếu bạn tạo một liên kết tượng trưng tới tệp đó, liên kết chỉ là một con trỏ tới tệp gốc. Nếu bạn xóa file gốc thì liên kết sẽ bị hỏng vì nó không còn nội dung nào để trỏ tới nữa.

Một liên kết cứng là bản sao của một tệp gốc có cùng nội dung. Giống như các liên kết tượng trưng, ​​​​nếu bạn chỉnh sửa nội dung của tệp gốc, những thay đổi đó sẽ được phản ánh trong liên kết cứng. Tuy nhiên, nếu bạn xóa tệp gốc, liên kết cứng sẽ vẫn hoạt động và bạn có thể xem và chỉnh sửa nó như một bản sao bình thường của tệp gốc.

Tìm hiểu thêm về Liên kết mềm và Liên kết cứng.

Quay lại danh sách lệnh ↑

Số clear lệnh trong Linux

clear lệnh trong Linux sẽ xóa màn hình terminal. Nó loại bỏ tất cả văn bản và đầu ra hiện được hiển thị trên thiết bị đầu cuối và cung cấp cho bạn một phương tiện rõ ràng để làm việc.

Dưới đây là ví dụ về cách sử dụng clear lệnh:

root@ubuntu:~# clear

Thao tác này sẽ xóa màn hình terminal và di chuyển con trỏ đến góc trên bên trái của màn hình.

Bạn cũng có thể sử dụng clear lệnh kết hợp với các lệnh khác, như thế này:

root@ubuntu:~# ls -l; clear

Thao tác này sẽ liệt kê các tập tin và thư mục trong thư mục hiện tại, sau đó xóa màn hình terminal.

Lưu ý: clear lệnh không xóa bất kỳ tập tin hoặc dữ liệu nào khỏi hệ thống của bạn. Nó chỉ ảnh hưởng đến việc hiển thị của thiết bị đầu cuối.

Số cat , echo , và less lệnh

Khi bạn muốn xuất nội dung của một tập tin hoặc in bất cứ thứ gì ra thiết bị đầu cuối, chúng tôi sử dụng cat hoặc echo lệnh. Hãy xem cách sử dụng cơ bản của chúng.

root@ubuntu:~# cat <file name>
root@ubuntu:~# echo <Text to print on terminal>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Như bạn có thể thấy trong ví dụ trên, cat lệnh khi được sử dụng trên New-File của chúng tôi , in nội dung của tập tin. Đồng thời, khi chúng ta sử dụng echo lệnh, nó chỉ đơn giản in bất cứ điều gì theo sau lệnh.

less lệnh được sử dụng khi đầu ra được in bởi bất kỳ lệnh nào lớn hơn không gian màn hình và cần cuộn. less lệnh cho phép người dùng chia nhỏ đầu ra và cuộn qua nó bằng cách sử dụng phím enter hoặc phím cách.

Cách đơn giản để thực hiện việc này là sử dụng toán tử pipe (| ).

root@ubuntu:~# cat /boot/grub/grub.cfg | less

Lưu ý: Sử dụng -S gắn cờ với less để kích hoạt tính năng ngắt dòng. Điều này sẽ cho phép bạn xem các dòng văn bản dài mà không cần cuộn theo chiều ngang.

Sử dụng -N gắn cờ với less để hiển thị số dòng. Điều này có thể hữu ích khi bạn cần biết số dòng của một đoạn văn bản cụ thể.

Bạn có thể sử dụng những lá cờ hữu ích này theo cách sau:

root@ubuntu:~# cat /boot/grub/grub.cfg | less -SN

Sử dụng less với người vận hành đường ống có thể hữu ích trong nhiều tình huống khác nhau. Dưới đây là một vài ví dụ:

  • Xem đầu ra của lệnh chạy dài, chẳng hạn như top hoặc htop .
  • Tìm kiếm văn bản cụ thể trong đầu ra của lệnh, chẳng hạn như grep hoặc cat .

Quay lại danh sách lệnh ↑

Số man lệnh trong Linux

man lệnh là một lệnh Linux rất hữu ích mà người ta phải biết. Khi làm việc với Linux, các gói mà chúng tôi tải xuống có thể có nhiều chức năng. Biết hết là điều không thể.

man lệnh trong Linux được sử dụng để hiển thị trang hướng dẫn cho một lệnh cụ thể. Nó cung cấp thông tin chi tiết về lệnh, bao gồm cú pháp, tùy chọn và ví dụ.

Sau đây là ví dụ về cách sử dụng man lệnh:

  1. Mở terminal và gõ man ls để hiển thị trang hướng dẫn sử dụng cho ls lệnh.
root@ubuntu:~# man ls

Điều này sẽ hiển thị một trang trông giống như thế này:

LS(1) User Commands LS(1)
NAME
 ls - list directory contents
SYNOPSIS
 ls [OPTION]... [FILE]...
DESCRIPTION
 List information about the FILEs (the current directory by default).
 Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
 Mandatory arguments to long options are mandatory for short options too.
 -a, --all
 do not ignore entries starting with .
 -A, --almost-all
 do not list implied . and ..
 -c with -lt: sort by, and show, ctime (time of last modification
 of file status information) with -l: show ctime and sort
 by name; 
...

Quay lại danh sách lệnh ↑

Số unamewhoami lệnh

unamewhoami các lệnh cho phép bạn truy cập một số thông tin cơ bản hữu ích khi bạn làm việc trên nhiều hệ thống.

uname lệnh trong Linux hiển thị thông tin về hạt nhân của hệ thống, bao gồm tên hạt nhân, tên máy chủ, bản phát hành hạt nhân, phiên bản hạt nhân và tên phần cứng máy.

whoami lệnh trong Linux trả về tên người dùng của người dùng hiện tại. Nó là viết tắt của "tôi là ai?" và nó thường được sử dụng để xác định danh tính của người dùng hiện tại trong tập lệnh shell hoặc thiết bị đầu cuối.

Hãy xem đầu ra của cả hai lệnh và cách chúng ta có thể sử dụng chúng.

root@ubuntu:~# uname -a

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Tham số -a với uname lệnh là viết tắt của “tất cả”. Điều này in ra các thông tin đầy đủ. Nếu tham số không được thêm vào, tất cả những gì bạn sẽ nhận được khi đầu ra là “Linux”.

Lưu ý: Một số cờ quan trọng bạn có thể sử dụng với uname lệnh.

  1. Sử dụng uname -s để hiển thị tên hạt nhân.
  2. Sử dụng uname -n để hiển thị tên máy chủ.
  3. Sử dụng uname -r để hiển thị bản phát hành kernel.
  4. Sử dụng uname -v để hiển thị phiên bản kernel.
  5. Sử dụng uname -m để hiển thị tên phần cứng của máy.

Quay lại danh sách lệnh ↑

Năm tar , zip , và unzip lệnh

tar lệnh trong Linux được sử dụng để tạo và giải nén các tệp lưu trữ. Chúng tôi có thể trích xuất nhiều tệp lưu trữ khác nhau bằng tar lệnh.

Để tạo kho lưu trữ, chúng tôi sử dụng -c tham số và để trích xuất một kho lưu trữ, chúng tôi sử dụng -x tham số. Hãy xem nó hoạt động như thế nào.

#Compress
root@ubuntu:~# tar -cvf <archive name> <files separated by space>
#Extract
root@ubuntu:~# tar -xvf <archive name>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Ở dòng đầu tiên, chúng tôi đã tạo một kho lưu trữ có tên Compress.tar với Tệp mới và Liên kết tệp mới. Trong lệnh tiếp theo, chúng tôi đã trích xuất các tệp đó khỏi kho lưu trữ.

Hãy cùng thảo luận về zipunzip lệnh. Cả hai đều rất đơn giản. Bạn có thể sử dụng chúng mà không cần bất kỳ tham số nào và chúng sẽ hoạt động như dự kiến. Hãy xem một ví dụ bên dưới.

root@ubuntu:~# zip <archive name> <file names separated by space>
root@ubuntu:~# unzip <archive name>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Vì chúng ta đã có các tệp đó trong cùng một thư mục nên unzip lệnh sẽ nhắc chúng tôi trước khi ghi đè các tệp đó.

Quay lại danh sách lệnh ↑

Số grep lệnh trong Linux

grep command là một công cụ tìm kiếm văn bản mạnh mẽ và linh hoạt trong các hệ điều hành dựa trên Linux và Unix. Nó có thể tìm kiếm các mẫu hoặc chuỗi cụ thể trong một hoặc nhiều tệp và lọc đầu ra của các lệnh khác.

grep lệnh là viết tắt của “in biểu thức chính quy toàn cầu”, phản ánh khả năng tìm kiếm biểu thức chính quy trên nhiều dòng và tệp.

root@ubuntu:~# <Any command with output> | grep "<string to find>"

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Đây là một cuộc biểu tình đơn giản của lệnh. Tìm hiểu thêm về lệnh grep.

Quay lại danh sách lệnh ↑

Số headtail lệnh

Khi xuất các tệp lớn, headtail các lệnh có ích. Các lệnh này lần lượt hiển thị phần đầu hoặc phần cuối của tệp. Chúng thường được sử dụng để xem nhanh nội dung của tệp mà không cần phải mở tệp đó bằng trình soạn thảo văn bản.

headtail các lệnh hiển thị 10 dòng đầu tiên của tệp theo mặc định. Để hiển thị số dòng khác nhau, bạn có thể sử dụng -n tùy chọn, theo sau là số dòng bạn muốn hiển thị.

Đây là một ví dụ về việc sử dụng headtail lệnh:

root@ubuntu:~# head <file name>
root@ubuntu:~# tail <file name>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Như bạn có thể thấy, lệnh head hiển thị 10 dòng tính từ đầu tệp.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Lệnh tail xuất ra 10 dòng cuối cùng của tệp.

Bạn có thể sử dụng các lệnh này để xem nhanh nội dung của tệp, theo dõi các bản cập nhật theo thời gian thực để khắc phục sự cố, lọc đầu ra khỏi các lệnh khác và thực hiện phân tích nhật ký.

Quay lại danh sách lệnh ↑

Số diff , comm , và cmp lệnh

diff , comm , và cmp các lệnh đều được sử dụng để so sánh các tệp trong hệ điều hành dựa trên Linux và Unix. Các lệnh này có thể được sử dụng để xác định sự khác biệt giữa hai tệp, hợp nhất các thay đổi và thực hiện các tác vụ so sánh tệp khác.

root@ubuntu:~# diff <file 1> <file 2>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Như bạn có thể thấy ở trên, chúng tôi đã thêm một đoạn văn bản nhỏ có nội dung “Dòng này đã được chỉnh sửa” vào tệp Đã chỉnh sửa tệp mới.

cmp lệnh được sử dụng để so sánh hai tệp và hiển thị byte đầu tiên khác nhau giữa chúng. Nó có thể được sử dụng để xác định sự khác biệt giữa các tệp nhị phân hoặc để kiểm tra lỗi trong tệp.

root@ubuntu:~# cmp <file 1> <file 2>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

cmp lệnh chỉ cho chúng ta biết số dòng, số này khác. Không phải văn bản thực tế.

comm lệnh được sử dụng để so sánh hai tệp đã được sắp xếp và hiển thị các dòng duy nhất cho mỗi tệp cũng như các dòng chung cho cả hai tệp.

root@ubuntu:~# comm <file 1> <file2>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Văn bản được căn trái chỉ có trong file 1 . Văn bản căn giữa chỉ xuất hiện trong file 2 . Và văn bản căn phải có trong cả hai tệp.

Nhìn bề ngoài thì có comm lệnh này có ý nghĩa nhất khi chúng ta đang cố gắng so sánh các tệp lớn hơn và muốn thấy mọi thứ được sắp xếp lại với nhau.

Cả ba lệnh này đều là những công cụ cần thiết để làm việc với các tệp trong hệ điều hành dựa trên Linux và Unix. Bằng cách hiểu cách sử dụng diff , comm , và cmp lệnh một cách hiệu quả, bạn có thể xác định sự khác biệt giữa các tệp, hợp nhất các thay đổi và thực hiện các tác vụ so sánh tệp khác.

Các lệnh này có thể giúp bạn xác định và giải quyết sự cố với tệp cũng như theo dõi các thay đổi và duy trì kiểm soát phiên bản. Cho dù bạn là nhà phát triển hay quản trị viên hệ thống thì những lệnh này đều là một phần thiết yếu trong bộ công cụ của bạn.

Quay lại danh sách lệnh ↑

Số sort lệnh trong Linux

sort lệnh được sử dụng để sắp xếp các dòng trong tệp văn bản hoặc đầu vào tiêu chuẩn trong hệ điều hành dựa trên Linux và Unix. Nó có thể được sử dụng để sắp xếp các dòng theo thứ tự tăng dần hoặc giảm dần và thực hiện các thao tác sắp xếp khác, chẳng hạn như sắp xếp theo trường hoặc sử dụng thứ tự sắp xếp tùy chỉnh.

Cú pháp cơ bản của sort lệnh là:

root@ubuntu:~# sort <filename>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Theo mặc định, sort lệnh sắp xếp các dòng theo trình tự đối chiếu ASCII , có thể dẫn đến kết quả không mong muốn khi sắp xếp số hoặc ký tự đặc biệt. Để sắp xếp các số theo thứ tự số, bạn có thể sử dụng -n tùy chọn.

Đây là một ví dụ về việc sử dụng -n tùy chọn:

root@ubuntu:~# sort -n file.txt

Lệnh trên sẽ sắp xếp các dòng trong file.txt theo số thứ tự.

sort lệnh cũng có thể được sử dụng để sắp xếp các dòng dựa trên các trường cụ thể bằng cách sử dụng -k tùy chọn.

Đây là một ví dụ về việc sử dụng -k tùy chọn:

root@ubuntu:~# sort -k 2 file.txt

Lệnh này sẽ sắp xếp các dòng trong file.txt dựa trên trường thứ hai.

sort command là một công cụ mạnh mẽ và linh hoạt để làm việc với các tệp văn bản trong hệ điều hành dựa trên Linux và Unix. Bằng cách hiểu cách sử dụng sort lệnh một cách hiệu quả, bạn có thể sắp xếp các dòng trong tệp văn bản, sắp xếp các dòng dựa trên các trường cụ thể và thực hiện các thao tác sắp xếp khác.

Các lệnh này có thể giúp bạn sắp xếp và phân tích dữ liệu cũng như thực hiện các tác vụ thao tác tệp khác. Cho dù bạn là nhà phát triển hay quản trị viên hệ thống, sort lệnh là một phần thiết yếu trong bộ công cụ của bạn.

Quay lại danh sách lệnh ↑

Số export lệnh trong Linux

export lệnh trong hệ điều hành dựa trên Linux và Unix được sử dụng để đặt các biến môi trường. Biến môi trường được sử dụng để lưu trữ thông tin có thể được sử dụng bởi các quy trình hoặc lệnh.

Khi một biến môi trường được đặt, nó có thể được truy cập bởi bất kỳ tiến trình hoặc lệnh nào đang chạy trong cùng một shell.

Biến môi trường có thể được sử dụng để lưu trữ nhiều loại thông tin, chẳng hạn như cài đặt cấu hình, tùy chọn người dùng hoặc thông tin hệ thống.

Đây là một ví dụ về việc sử dụng export lệnh:

root@ubuntu:~# export <variable name>=<value>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Tìm hiểu thêm về export lệnh

Quay lại danh sách lệnh ↑

Số ssh lệnh trong Linux

ssh lệnh trong hệ điều hành dựa trên Linux và Unix thiết lập kết nối shell an toàn đến máy chủ từ xa. Lệnh này cung cấp kết nối được mã hóa an toàn giữa máy chủ cục bộ và máy chủ từ xa, cho phép người dùng chạy lệnh và truyền tệp một cách an toàn.

Cú pháp cơ bản của ssh lệnh là:

root@ubuntu:~ ssh username@remote-server

Lệnh này thiết lập một ssh kết nối với remote-server sử dụng username tài khoản.

ssh command supports a wide range of options and configurations, including:

  • Configuring authentication methods (password, public key, etc.)
  • Configuring encryption algorithms
  • Configuring compression
  • Configuring port forwarding
  • Configuring X11 forwarding
  • Configuring SSH keys

The service command in Linux

The service command in Linux is used to manage system services, which are long-running processes that are started at boot time and run in the background. These services are responsible for providing various system functionalities, such as networking, database management, and user authentication.

The service command is used to start, stop, restart, and check the status of these services. It is a front-end to the systemctl command, which is used to manage the systemd service manager.

The basic syntax of the command is as below.

root@ubuntu:~ service ssh status
root@ubuntu:~ service ssh stop
root@ubuntu:~ service ssh start 

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

As you can see in the image, the ssh server is running on our system.

Jump back to commands list ↑

The ps , kill , and killall commands

The ps , kill , and killall commands are all used to manage processes in Linux.

The ps command is used to display information about the current running processes on the system. Here are some examples of using the ps command:

Display a list of all running processes:

root@ubuntu:~ ps -ef

Display a list of all processes for a specific process ID (PID):

root@ubuntu:~ ps -p PID

Let’s see all of this in action:

root@ubuntu:~ ps 
root@ubuntu:~ kill <process ID>
root@ubuntu:~ killall <process name>

For demonstration purposes, we will create a shell script with an infinite loop and will run it in the background.

With the use of the & symbol, we can pass a process into the background. As you can see, a new bash process with PID 14490 is created.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Now, to kill a process with the kill command, you can type kill followed by the PID(Process Id) of the process.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

But if you do not know the process ID and just want to kill the process with the name, you can make use of the killall command.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

You will notice that PID 14490 stayed active. That is because, both times, we killed the sleep process.

Learn more about the ps command and the kill command.

Jump back to commands list ↑

The df and mount commands

When working with Linux, the df and mount commands are very efficient utilities to mount filesystems and get details of the file system.

The df command is used to display the amount of disk space used and available on the file systems, and the mount command is used to mount a file system or device to a specific directory.

When we say mount , it means that we’ll connect the device to a folder so we can access the files from our filesystem. The default syntax to mount a filesystem is below:

root@ubuntu:~ mount /dev/cdrom /mnt
root@ubuntu:~ df -h

In the above case, /dev/cdrom is the device that needs to be mounted. Usually, a mountable device is found inside the /dev folder. mnt is the destination folder to which to mount the device. You can change it to any folder you want, but we have used /mnt as it’s the system’s default folder for mounting devices.

To see the mounted devices and get more information about them, we use the df command. Just typing df will give us the data in bytes, which is not readable. So, we’ll use the -h parameter to make the data human-readable.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Jump back to commands list ↑

The chmod and chown commands

The chmod and chown commands are used to modify file permissions and ownership in Linux.

The chmod command is used to change the permissions of a file or directory, and the chown command is used to change the ownership of a file or directory

The default syntax for both the commands is chmod <parameter> filename and chown <user:group> filename

root@ubuntu:~ chmod +x loop.sh
root@ubuntu:~ chmod root:root loop.sh

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

In the above example, we’re adding executable permissions to the loop.sh file with the chmod lệnh. In addition, with the chown command, we’ve made it accessible only to the root user and users within the root group.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

As you will notice, the root root part is now changed to www-data which is the new user who has full file ownership.

Learn more about the Linux file Permissions and using the chmod command.

Jump back to commands list ↑

The ifconfig and traceroute commands

The ifconfig and traceroute commands manage network interfaces and trace the route of network packets in Linux.

The ifconfig command will give you the list of all the network interfaces along with the IP addresses, MAC addresses and other information about the interface.

root@ubuntu:~ ifconfig

There are multiple parameters that can be used, but we’ll work with the basic command here.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

The traceroute command is used to trace the route of network packets and determine the path they take to reach a specific destination.

When working with traceroute , you can simply specify the IP address, hostname, or domain name of the endpoint.

root@ubuntu:~ traceroute <destination address>

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Now, obviously, localhost is just one hop (the network interface itself). You can try this same command with any other domain name or IP address to see all the routers your data packets pass through to reach the destination.

Jump back to commands list ↑

The wget command in Linux

If you want to download a file from within the terminal, the wget command is one of the handiest command-line utilities available. It is one of the important Linux commands you should know when working with source files.

When you specify the link for download, it has to directly be a link to the file. If the file cannot be accessed by the wget command, it will simply download the webpage in HTML format instead of the actual file that you wanted.

Let’s try an example. The basic syntax of the wget command is :

root@ubuntu:~ wget <link to file>

Or,

root@ubuntu:~ wget -c <link to file>

The -c argument allows us to resume an interrupted download.

Jump back to commands list ↑

The ufw and iptables commands

The ufw and iptables commands are used to manage firewalls in Linux.

UFW and IPTables are firewall interfaces for the Linux Kernel’s netfilter firewall. IPTables directly passes firewall rules to Netfilter while UFW configures the rules in IPTables, which then sends those rules to Netfilter.

Why do we need UFW when we have IPTables? Because IPTables is pretty difficult for a newbie. UFW makes things extremely easy. See the below example where we are trying to allow port 80 for our webserver.

root@ubuntu:~# iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
root@ubuntu:~# ufw allow 80

I’m sure you now know why UFW was created! Look at how easy the syntax becomes. Both these firewalls are very comprehensive and can allow you to create any kind of configuration required for your network. Learn at least the basics of UFW or IPTables firewall, as these are the Linux commands you must know.

Learn more about managing firewall with ufw and managing firewall with iptables .

Jump back to commands list ↑

Package Managers in Linux

Different Linux distributions use different package managers. Since we’re working on a Ubuntu server, we have the apt package manager. But for someone working on a Fedora, Red Hat, Arch, or Centos machine, the package manager will be different.

Below are the commands on how to use these package managers on different Linux distributions.

  • Debian and Debian-based distros - apt install <package name>
  • Arch and Arch-based distros - pacman -S <package name>
  • Red Hat and Red Hat-based distros - yum install <package name>
  • Fedora and CentOS - yum install <package>

Getting yourself well versed with the package manager of your distribution will make things much easier for you in the long run. So even if you have a GUI based package management tool installed, try an make use of the CLI based tool before you move on to the GUI utility. Add these to your list of Linux commands you must know.

Jump back to commands list ↑

The sudo command in Linux

“With great power, comes great responsibility” _

This is the quote displayed when a sudo-enabled user(sudoer) first uses the sudo command to escalate privileges. This command is equivalent to logging in as root (based on what permissions you have as a sudoer).

non-root-user@ubuntu:~# sudo <command you want to run>
Password: 

Just add the command sudo before any command that you need to run with escalated privileges, and that’s it. It’s very simple to use, but can also be an added security risk if a malicious user gains access to a sudoer.

Learn more about how to create a new sudo user and how to edit sudoers file.

Jump back to commands list ↑

The cal command in Linux

Have you ever wanted to view the calendar in the terminal? There apparently are people who want it to happen, and well, here it is.

The cal command displays a well-presented calendar on the terminal. Just enter the command cal on your terminal prompt.

root@ubuntu:~# cal
root@ubuntu:~# cal March 2024

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Although we don’t need it often, it’s a great addition! It’s an amazing option for terminal fans.

Jump back to commands list ↑

The alias command

Do you have some commands that you run very frequently while using the terminal? It could be rm -r or ls -l , or it could be something longer like tar -xvzf .

This is one of the productivity-boosting Linux commands you must know.

If you know a command that you run very often, it’s time to create an alias .

What’s an alias? In simple terms, it’s another name for a command that you’ve defined.

root@ubuntu:~# alias lsl="ls -l"
OR
root@ubuntu:~# alias rmd="rm -r"

Now, every time you enter lsl or rmd in the terminal, you’ll receive the output that you’d have received if you had used the full commands.

The examples here are for really small commands that you can still type by hand every time. But in some situations where a command has too many arguments that you need to type, it’s best to create a shorthand version of the same.

Jump back to commands list ↑

The dd command in Linux

This command was created to convert and copy files from multiple file system formats. In the current day, the command is simply used to create bootable USB for Linux but there still are some things important you can do with the command.

The dd command in Linux is a versatile command used for low-level copying and conversion of data. It stands for “data-description” or “data definition,” and it can be used to copy and convert data between different file formats and storage devices.

For example, if we wanted to back up the entire hard drive as is to another drive, we would use the dd command.

root@ubuntu:~# dd if=/dev/sdb of=/dev/sda

The if and of arguments stand for input file and output file .

It’s a powerful and flexible tool, but it can also be dangerous if not used carefully. Always double-check your syntax and make sure you know what the command will do before executing it.

Jump back to commands list ↑

The whereis and whatis commands

The whereis and whatis commands are used in Linux to search for information about programs and files.

The whereis command locates the binary, source, and manual pages for a specific command or program, and the whatis command displays a short description of a command or program.

root@ubuntu:~# whereis sudo
sudo: /usr/bin/sudo /usr/lib/sudo /usr/share/man/man8/sudo.8.gz

The whatis command gives us an explanation of what a command actually is.

root@ubuntu:~# whatis sudo
sudo (8) - execute a command as another user

Jump back to commands list ↑

The top command in Linux

A few sections earlier, we talked about the ps lệnh. You observed that the ps command will output the active processes and end itself.

The top command is like a CLI version of the task manager in Windows.

The top command in Linux is a system monitoring tool that displays real-time information about system processes and resource usage. It provides a dynamic, real-time view of system activity, including CPU usage, memory usage, and process information.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Lưu ý: Here are some examples of using the top command:

  • Sort processes by memory usage:
root@ubuntu:~# top -o MEM

This will sort the process list by memory usage, with the most memory-intensive processes at the top.

  • Display detailed information about a specific process:
root@ubuntu:~# top -p PID

Replace PID with the ID of the process you want to inspect.

  • Display a summary of system resource usage:
root@ubuntu:~# top -n 1

This will display a single summary screen of system resource usage.

It’s a powerful and flexible tool for monitoring system activity and troubleshooting performance issues.

Jump back to commands list ↑

The useradd and usermod commands

The useradd and usermod commands are used in Linux to manage user accounts.

The useradd or adduser commands are the exact same commands where adduser is just a symbolic link to the useradd lệnh. This command allows us to create a new user in Linux.

root@ubuntu:~# useradd JournalDev -d /home/JD

The above command will create a new user named JournalDev with the home directory as /home/JD .

The usermod command, on the other hand, is used to modify existing users. You can modify any value of the user including the groups, the permissions, etc.

For example, if you want to add more groups to the user, you can type in:

root@ubuntu:~# usermod JournalDev -a -G sudo, audio, mysql

Jump back to commands list ↑

The passwd command in Linux

Now that you know how to create new users, let’s also set the password for them. The passwd command lets you set the password for your own account, or if you have the permissions, set the password for other accounts.

Here are some examples of using the passwd command:

  • Change the password for the current user:

    root@ubuntu:~# passwd
    

This will prompt you to enter a new password for the current user.

  • Change the password for a specific user:

    root@ubuntu:~# passwd username
    

Replace username with the name of the user whose password you want to change.

  • Force a user to change their password at the next login:

    root@ubuntu:~# passwd -f username
    
  • Set an expiration date for a user’s password:

    root@ubuntu:~# passwd -e -n days -w warndays username
    

Replace days with the number of days before the password expires and warm days with the number of days before the password expires that the user will be warned.

These are just a few examples of using the passwd command in Linux. By understanding how to use this command effectively, you can manage user accounts and ensure that your system is secure.

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả

Jump back to commands list ↑

Common Errors and Debugging

When working with Linux commands, you may encounter various errors and issues. Here are some common problems and how to resolve them:

1. Fixing “command not found” errors

If you receive a command not found error, it means that the command you are trying to run is not recognized by the system. This can happen for several reasons:

  1. The command is not installed on your system. You can install it using your package manager (e.g., apt , yum , dnf ).

    If you see:

    bash: xyz: command not found
    

    Use:

    which xyz
    

    If the command isn’t installed, try installing:

    apt-get install <package-name>
    
  2. The command is not in your system’s PATH. You can add the directory containing the command to your PATH.

    export PATH=$PATH:/path/to/command
    
  3. You made a typo. Double-check the command for any spelling errors.

2. Resolving permission issues with sudo

If you encounter permission issues, you can use sudo to run the command with superuser privileges. Ví dụ:

sudo command_name

3. Handling File Conflicts

File conflicts can occur when multiple users or processes attempt to modify the same file simultaneously. Here are some ways to handle file conflicts:

  1. Use Version Control Systems (VCS): Tools like Git can help manage file conflicts by allowing users to merge changes and resolve conflicts manually.

    git merge <branch_name>
    

    If there are conflicts, Git will prompt you to resolve them. Open the conflicting files, make the necessary changes, and then commit the resolved files.

    git add <resolved_file>
    git commit -m "Resolved merge conflict"
    
  2. Locking Mechanisms: Implement file locking to prevent multiple processes from writing to the same file simultaneously. Use flock in Linux to create a lock on a file.

    flock -x <file> -c "<command>"
    
  3. Atomic Operations: Use atomic operations to ensure that file writes are completed in a single step, reducing the risk of conflicts.

    mv temp_file target_file
    

    Or, Use mv with -f (force) or cp with -i (interactive):

    mv -f file1 file2
    cp -i file1 file2
    

4. Debugging Performance Bottlenecks

Performance bottlenecks can significantly impact the efficiency of your system. Here are some steps to debug and resolve them:

  1. Identify the Bottleneck: Use tools like top , htop , vmstat , and iostat to monitor system performance and identify the resource causing the bottleneck (CPU, memory, disk I/O, etc.).

    top
    
  2. Analyze Logs: Check system and application logs for any errors or warnings that might indicate performance issues.

    tail -f /var/log/syslog
    
  3. Optimize Code: Review and optimize your code to improve performance. Look for inefficient algorithms, unnecessary computations, and memory leaks.

  4. Profile Your Application: Use profiling tools like gprof , perf , or valgrind to analyze your application’s performance and identify slow functions or memory issues.

    gprof <executable> gmon.out
    
  5. Scale Resources: If the bottleneck is due to resource limitations, consider scaling up your hardware or using load balancing to distribute the load across multiple servers.

FAQs

1. What are the most used Linux commands?

The most used Linux commands include cd , ls , mkdir , rm , cp , mv , echo , cat , grep , find , man , sudo , apt-get , ssh , ping , df , du , free , top , ps , kill , killall , service , systemctl , reboot , shutdown , whoami , uname , uptime , history , and clear .

2. How do I list all available commands in Linux?

You can list all available commands in Linux by using the compgen -c lệnh. This will display a list of all commands that are available on your system.

3. How do I find a file in Linux?

You can find a file in Linux using the find lệnh. The basic syntax is find <path> -name "<filename>" , where <path> is the directory where you want to start searching, and <filename> is the name of the file you’re looking for. For example, find /home/user -name "example.txt" .

4. How do I kill a process in Linux?

You can kill a process in Linux using the kill lệnh. First, you need to find the process ID (PID) of the process you want to kill using the ps or top lệnh. Then, use the kill command followed by the PID. For example, kill 1234 , where 1234 is the PID of the process.

5. What is the difference between cp and mv ?

The cp command is used to copy files or directories, while the mv command is used to move or rename files or directories. cp creates a duplicate of the original file, leaving the original intact, whereas mv moves the file to a new location, removing it from the original location.

6. How do I check my Linux system’s memory usage?

You can check your Linux system’s memory usage using the free lệnh. This command displays the total amount of free and used physical and swap memory in the system. For example, free -h will display the memory usage in a human-readable format.

Kết luận

In this tutorial, you explored over 50 essential Linux commands covering file and directory management, process control, user permissions, networking, text processing, and shell utilities. You also worked through real-world examples, troubleshooting patterns, and comparisons between related commands.

You can now navigate the Linux filesystem, manage processes, configure permissions, and troubleshoot common issues from the command line. These commands form the foundation of effective Linux system administration and scripting.

To go deeper on specific topics, explore these tutorials:

  • The grep Command in Linux/Unix
  • The ls Command in Linux/Unix
  • Process Management in Linux
  • The ps Command in Linux
  • An Introduction to the Linux Terminal

Làm chủ Linux:Hơn 50 lệnh cần thiết để quản lý hệ thống hiệu quả Tác phẩm này được cấp phép theo Giấy phép quốc tế Creative Commons Ghi công-NonCommercial-ShareAlike 4.0.