Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

บทนำ

ปลดล็อกศักยภาพเต็มที่ของระบบ Linux ด้วยคู่มือครบครันสำหรับคำสั่ง Linux ที่จำเป็น ไม่ว่าคุณจะเป็นผู้ดูแลระบบมืออาชีพหรือเพิ่งเริ่มต้น การเชี่ยวชาญคำสั่งเหล่านี้สำคัญมากสำหรับการจัดการเซิร์ฟเวอร์เขียนสคริปต์ และ troubleshooting ในบทช่วยสอนนี้ คุณจะได้เรียนรู้คำสั่งที่ใช้บ่อยและทรงพลังที่สุดเกิน 50+ คำสั่ง สำหรับจัดการไฟล์ ควบคุมโปรเซส สิทธิ์ผู้ใช้ การตั้งค่าเครือข่าย และ debug ระบบ

คุณจะได้เรียนรู้ คำสั่ง Linux 50+ คำสั่งที่ต้องรู้ ที่จะทำให้คุณกลายเป็น Linux power user จากพื้นฐานไปจนถึงขั้นสูง คำสั่งเหล่านี้จะกลายเป็นเครื่องมือหลักสำหรับรับมือกับงานทุกประเภทที่มาทางเข้าของคุณ

สรุปสำคัญ (Key Takeaways)

  • คำสั่ง Linux จัดกลุ่มตามวัตถุประสงค์: จัดการไฟล์ (ls, cp, mv, rm) ควบคุมโปรเซส (ps, kill, top) สิทธิ์ผู้ใช้ (chmod, chown, sudo) และเครือข่าย (ssh, ping, wget)
  • คำสั่ง man ให้ built-in documentation สำหรับคำสั่ง Linux ใดๆ และเป็น reference หลักเมื่อต้องการ syntax หรือรายละเอียด options
  • ใช้ grep ค้นหาข้อความใน output หรือไฟล์ head/tail ดูเนื้อหาไฟล์เบื้องต้น และ diff/cmp เปรียบเทียบไฟล์
  • File permissions จัดการด้วย chmod (เปลี่ยน mode bits) และ chown (เปลี่ยน owner) ซึ่งสำคัญมากสำหรับความปลอดภัยเซิร์ฟเวอร์และ access control
  • คำสั่งอย่าง tar, zip, unzip จัดการ archiving และ compression; ssh และ scp จัดการ remote access และ file transfer อย่างปลอดภัย
  • Process management commands (ps, kill, killall, top) จำเป็นสำหรับ monitor system health และ terminate unresponsive processes

ข้อกำหนดเบื้องต้น (Prerequisites)

เราจะรันคำสั่งเหล่านี้บน Ubuntu server แต่คุณสามารถปฏิบัติตามได้บน Linux distribution ใดๆ ที่ทันสมัย คุณสามารถ setup Ubuntu server สำหรับบทช่วยสอนนี้ได้จากคู่มือ Initial Server Setup on Ubuntu ของเรา

Deploy frontend applications จาก GitHub โดยใช้ DigitalOcean App Platform ให้ DigitalOcean จัดการ scaling ให้คุณ

มาเริ่มกันเลย!

Top 50 คำสั่ง Linux ที่คุณต้องรู้ในฐานะ Regular User

  1. ls - คำสั่งที่ใช้บ่อยที่สุดใน Linux เพื่อ list directories
  2. pwd - Print working directory command ใน Linux
  3. cd - Linux command สำหรับ navigate ผ่าน directories
  4. mkdir - Command ใช้สร้าง directories ใน Linux
  5. mv - Move หรือ rename files ใน Linux
  6. cp - ใช้งานคล้าย mv แต่สำหรับ copy files ใน Linux
  7. rm - Delete files หรือ directories
  8. touch - สร้างไฟล์ว่าง/empty files
  9. ln - สร้าง symbolic links (shortcuts) ไปยังไฟล์อื่น
  10. clear - ล้างการแสดงผล terminal
  11. cat - แสดงเนื้อหาไฟล์บน terminal
  12. echo - พิมพ์ข้อความที่ตามคำสั่ง
  13. less - Linux command แสดง paged outputs ใน terminal
  14. man - เข้าถึง manual pages สำหรับคำสั่ง Linux ทั้งหมด
  15. uname - Linux command 取得ข้อมูลพื้นฐานเกี่ยวกับ OS
  16. whoami - 取得 active username
  17. tar - Command extract และ compress files ใน linux
  18. grep - ค้นหา string ภายใน output
  19. head - คืนค่าจำนวนบรรทัดที่ระบุจากด้านบน
  20. tail - คืนค่าจำนวนบรรทัดที่ระบุจากด้านล่าง
  21. diff - หาความแตกต่างระหว่างสองไฟล์
  22. cmp - ตรวจสอบว่าไฟล์สองไฟล์เหมือนกันหรือไม่
  23. comm - รวมฟังก์ชันของ diff และ cmp
  24. sort - Linux command เรียงลำดับเนื้อหาไฟล์ขณะ output
  25. export - Export environment variables ใน Linux
  26. zip - Zip files ใน Linux
  27. unzip - Unzip files ใน Linux
  28. ssh - Secure Shell command ใน Linux
  29. service - Linux command เริ่มและหยุด services
  30. ps - แสดง active processes
  31. kill and killall - Kill active processes ด้วย process ID หรือ name
  32. df - แสดงข้อมูล disk filesystem
  33. mount - Mount file systems ใน Linux
  34. chmod - Command เปลี่ยน file permissions
  35. chown - Command ให้ ownership ของไฟล์หรือโฟลเดอร์
  36. ifconfig - แสดง network interfaces และ IP addresses
  37. traceroute - Trace network hops ทั้งหมดเพื่อไปถึง destination
  38. wget - Download files โดยตรงจาก internet
  39. ufw - Firewall command
  40. iptables - Base firewall สำหรับ firewall utilities อื่นๆ interface เข้ากัน
  41. apt, pacman, yum, rpm - Package managers ขึ้นอยู่กับ distribution
  42. sudo - Command escalate privileges ใน Linux
  43. cal - ดูปฏิทิน command-line
  44. alias - สร้าง custom shortcuts สำหรับคำสั่งที่ใช้บ่อย
  45. dd - ใช้หลักสำหรับสร้าง bootable USB sticks
  46. whereis - Locate binary, source, และ manual pages สำหรับคำสั่ง
  47. whatis - หาว่าคำสั่งใช้ทำอะไร
  48. top - ดู active processes แบบ real-time พร้อม system usage
  49. useradd and usermod - เพิ่ม user ใหม่ หรือเปลี่ยนข้อมูล user ที่มีอยู่
  50. passwd - สร้างหรือ update passwords สำหรับ existing users

คำสั่งไฟล์และไดเรกทอรี (File and Directory Commands)

CommandDescriptionExample
lsList directory contents.ls
cdChange directory.cd /path/to/directory
pwdShow current directory.pwd
mkdirCreate a new directory.mkdir new_directory
rmdirRemove an empty directory.rmdir empty_directory
rmDelete files or directories.rm file.txt
touchCreate an empty file.touch new_file.txt
cpCopy files or directories.cp file.txt /path/to/destination
mvMove or rename files.mv file.txt /path/to/new_location
catDisplay file contents.cat file.txt
nano / vimEdit files in terminal.nano file.txt
findSearch for files in a directory hierarchy.find . -name "file.txt"
grepSearch text using patterns.grep "pattern" file.txt
tarArchive and compress files.tar -cvf archive.tar file1.txt file2.txt
dfShow disk usage of file systems.df
duShow directory/file size.du -sh /path/to/directory
chmodChange file permissions.chmod 755 file.txt
chownChange file owner.chown user:group file.txt
mountMount a filesystem.mount /dev/sdb1 /mnt
umountUnmount a filesystem.umount /mnt

คำสั่งเครือข่าย (Networking Commands)

CommandDescriptionSample Usage
pingTest connectivity to a host.ping google.com
ifconfig / ip aDisplay network interfaces.ifconfig or ip a
netstat / ssShow network connections.netstat -tuln or ss -tuln
wgetDownload files via HTTP/FTP.wget https://example.com/file.zip
curlTransfer data using URL syntax.curl -O https://example.com/file.zip
nc (Netcat)Network debugging and data transfer.nc -zv 192.168.1.1 80
tcpdumpCapture and analyze network packets.tcpdump -i eth0
iptablesConfigure firewall rules.iptables -A INPUT -p tcp --dport 22 -j ACCEPT
tracerouteTrace the path packets take to a network host.traceroute example.com
nslookupQuery DNS to obtain domain name or IP address mapping.nslookup example.com
sshSecurely connect to a remote host.ssh user@example.com

คำสั่ง Process และ System Monitoring

CommandDescriptionExample Command
psShow running processes.ps aux
topDynamic process viewer.top
htopEnhanced version of top.htop
killSend a signal to a process.kill <PID>
killallKill processes by name.killall <process_name>
uptimeSystem uptime and load.uptime
whoamiCurrent logged-in user.whoami
envDisplay environment variables.env
straceTrace system calls of a process.strace -p <PID>
systemctlManage systemd services.systemctl status <service_name>
journalctlView system logs.journalctl -xe
freeDisplay memory usage.free -h
vmstatReport virtual memory statistics.vmstat 1
iostatReport CPU and I/O statistics.iostat
lsofList open files by processes.lsof
dmesgPrint kernel ring buffer messages.dmesg

คำสั่ง User และ Permission Management

CommandDescriptionExample Command
passwdChange user password.passwd <username>
adduser / useraddAdd a new user.adduser <username> or useradd <username>
deluser / userdelDelete a user.deluser <username> or userdel <username>
usermodModify user account.usermod -aG <group> <username>
groupsShow group memberships.groups <username>
sudoExecute commands as root.sudo <command>
chageChange user password expiry information.chage -l <username>
idDisplay user identity information.id <username>
newgrpLog in to a new group.newgrp <group>

คำสั่ง File Transfer และ Synchronization

CommandDescriptionExample Command
scpSecurely copy files over SSH.scp user@remote:/path/to/file /local/destination
rsyncEfficiently sync files and directories.rsync -avz /local/directory/ user@remote:/path/to/destination
ftpTransfer files using the File Transfer Protocol.ftp ftp.example.com
sftpSecurely transfer files using SSH File Transfer Protocol.sftp user@remote:/path/to/file
wgetDownload files from the web.wget https://example.com/file.zip
curlTransfer data from or to a server.curl -O https://example.com/file.zip

คำสั่ง Text Processing

CommandDescriptionExample Command
awkPattern scanning and processing.awk '{print $1}' file.txt
sedStream editor for filtering/modifying text.sed 's/old/new/g' file.txt
cutRemove sections from lines of text.cut -d':' -f1 /etc/passwd
sortSort lines of text.sort file.txt
grepSearch for patterns in text.grep 'pattern' file.txt
wcCount words, lines, and characters.wc -l file.txt
pasteMerge lines of files.paste file1.txt file2.txt
joinJoin lines of two files on a common field.join file1.txt file2.txt
headOutput the first part of files.head -n 10 file.txt
tailOutput the last part of files.tail -n 10 file.txt

คำสั่ง Shell Utilities และ Shortcuts

CommandDescriptionExample Command
aliasCreate shortcuts for commands.alias ll='ls -la'
unaliasRemove an alias.unalias ll
historyShow previously entered commands.history
clearClear the terminal screen.clear
rebootReboot the system.reboot
shutdownPower off the system.shutdown now
dateDisplay or set the system date and time.date
echoDisplay a line of text.echo "Hello, World!"
sleepDelay for a specified amount of time.sleep 5
timeMeasure the duration of command execution.time ls
watchExecute a program periodically, showing output fullscreen.watch -n 5 df -h

ตอนนี้มาเจาะลึกแต่ละคำสั่งเพื่อเข้าใจมากขึ้น เรามีบทความที่มีอยู่แล้วสำหรับคำสั่งแต่ละตัว สำหรับความสะดวก เราจะเพิ่มลิงก์ไปยังบทความทั้งหมด และจะอัปเดตบทความนี้เมื่อมีหัวข้อใหม่ครอบคลุม

คำสั่ง ls ใน Linux

คำสั่ง ls ใช้ list files และ directories ใน current working directory นี่จะเป็นหนึ่งในคำสั่ง Linux ที่ใช้บ่อยที่สุดที่คุณต้องรู้

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เห็นได้จากภาพด้านบน การใช้คำสั่งเพียงอย่างเดียวโดยไม่มี arguments จะให้ output พร้อม files และ directories ทั้งหมดใน directory คำสั่งนี้ให้ความยืดหยิงมากในการแสดงข้อมูลใน output

เรียนรู้เพิ่มเติมเกี่ยวกับการใช้คำสั่ง ls

กลับไปที่รายการคำสั่ง ↑

คำสั่ง pwd ใน Linux

คำสั่ง pwd ให้คุณ print current working directory บน terminal มันคือคำสั่งพื้นฐานมากและทำหน้าที่ได้ดี

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

ตอนนี้ terminal prompt ของคุณควรจะมี complete directory อยู่แล้ว แต่ถ้าไม่มี คำสั่งนี้ช่วยให้ดู directory ที่คุณอยู่ได้รวดเร็ว อีก application หนึ่งคือตอนสร้าง scripts ที่คำสั่งนี้ช่วยให้เราหา directory ที่ script ถูก save ไว้

กลับไปที่รายการคำสั่ง ↑

คำสั่ง cd ใน Linux

ขณะทำงานใน terminal การเคลื่อนที่ใน directories เป็นสิ่งจำเป็น คำสั่ง cd เป็นหนึ่งในคำสั่ง Linux สำคัญที่คุณต้องรู้ และจะช่วยคุณ navigate ผ่าน directories เพียงพิมพ์ cd ตามด้วย directory

root@ubuntu:~# cd <directory path> 

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เห็นได้จากคำสั่งด้านบน ผมพิมพ์ cd /etc/ เพื่อเข้า /etc directory เราใช้คำสั่ง pwd เพื่อ print current working directory

กลับไปที่รายการคำสั่ง ↑

คำสั่ง mkdir ใน Linux

คำสั่ง mkdir ให้คุณสร้าง directories จากภายใน terminal

root@ubuntu:~# mkdir <folder name> 

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เห็นได้จาก screenshot ด้านบน เราสร้าง directory ใหม่ด้วยคำสั่งง่ายๆ นี้

กลับไปที่รายการคำสั่ง ↑

คำสั่ง cp และ mv

คำสั่ง cp และ mv เทียบเท่ากับ copy-paste และ cut-paste ใน Windows แต่ Linux ไม่มีคำสั่ง rename files โดยตรง เราจึงใช้ mv rename files และ folders ด้วย

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

ในคำสั่งด้านบน เราสร้าง copy ของไฟล์ชื่อ Sample มาดูว่าเกิดอะไรขึ้นถ้าเราใช้ mv แบบเดียวกัน

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

ในกรณีนี้ เนื่องจากเรา move ไฟล์ภายใน directory เดียวกัน มันทำหน้าที่เป็น rename ชื่อไฟล์เปลี่ยนไปแล้ว

กลับไปที่รายการคำสั่ง ↑

คำสั่ง rm ใน Linux

ในส่วนก่อนหน้า เราลบไฟล์ Sample-Copy คำสั่ง rm ใช้ delete files และ folders เป็นหนึ่งในคำสั่ง Linux สำคัญที่คุณต้องรู้

root@ubuntu:~# rm <file name> 

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เพื่อลบ directory คุณต้องเพิ่ม argument -r โดยไม่มี -r คำสั่ง rm ไม่จะลบ directories

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

Flag -r ในคำสั่ง rm ย่อมาจาก "recursive" เมื่อใช้กับ rm จะลบไม่แค่ไฟล์ที่ระบุ แต่ subdirectories และ files ภายใน subdirectories แบบ recursive ด้วย

หมายเหตุ: สำคัญต้องระวังตอนใช้ rm กับ -r เพราะลบไฟล์และ directories จำนวนมากได้อย่างรวดเร็วและถาวร ควรใช้ -i ร่วมกับ -r ซึ่งจะ prompt ยืนยันก่อนลบแต่ละไฟล์และ directory

ตัวอย่าง ลบ directory mydir และเนื้อหาในมันพร้อม confirmation:

root@ubuntu:~# rm -ri mydir 

จะ prompt ยืนยันก่อนลบแต่ละไฟล์และ directory ภายใน mydir

กลับไปที่รายการคำสั่ง ↑

คำสั่ง touch ใน Linux

คำสั่ง touch ใน Linux สร้าง empty file หรือ update timestamp ของ existing file

root@ubuntu:~# touch <file name> 

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

กลับไปที่รายการคำสั่ง ↑

คำสั่ง ln ใน Linux

เพื่อสร้าง link ไปยังไฟล์อื่น เราใช้คำสั่ง ln นี่เป็นหนึ่งในคำสั่ง Linux สำคัญที่สุดที่คุณควรรู้ถ้าวางแผนทำงานเป็น Linux administrator

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

Flag -s สร้าง symbolic link (หรือ symlink/soft link) ไปยังไฟล์หรือ directory Symbolic link คือไฟล์พิเศษที่ทำหน้าที่เป็น shortcut หรือ pointer ไปยังไฟล์/ directory อื่น

โดย default คำสั่ง ln จะทำ hard links แทน symbolic หรือ soft links

หมายเหตุ: สมมติคุณมี text file ถ้าทำ symbolic link ไปยังไฟล์นั้น link เป็นเพียง pointer ไปยัง original file ถ้าลบ original file link จะ broken เพราะไม่มีอะไรให้ point แล้ว

Hard link คือ mirror copy ของ original file พร้อม contents เหมือนกันทุกประการ เหมือน symbolic links ถ้า edit original file การเปลี่ยนแปลงจะสะท้อนใน hard link แต่ถ้าลบ original file hard link จะยังทำงานได้ และ view/edit ได้เหมือน normal copy ของ original file

เรียนรู้เพิ่มเติมเกี่ยวกับ Soft และ Hard Links

กลับไปที่รายการคำสั่ง ↑

คำสั่ง clear ใน Linux

คำสั่ง clear ใน Linux ล้าง terminal screen มันลบ text และ output ที่แสดงอยู่ทั้งหมด และให้ clean slate มาทำงาน

ตัวอย่างการใช้ clear:

root@ubuntu:~# clear 

จะล้าง terminal screen และเลื่อน cursor ไปมุมซ้ายบน

คุณยังใช้ clear ร่วมกับคำสั่งอื่นได้ เช่น:

root@ubuntu:~# ls -l; clear 

จะ list files และ directories ใน current directory แล้วล้าง terminal screen

หมายเหตุ: คำสั่ง clear ไม่ลบไฟล์หรือข้อมูลจากระบบของคุณ มันส่งผลต่อการแสดงผลของ terminal เท่านั้น

คำสั่ง cat, echo, และ less

เมื่อคุณต้องการ output เนื้อหาไฟล์หรือพิมพ์อะไรก็ตามไป terminal output เราใช้คำสั่ง cat หรือ echo มาดู basic usage

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เห็นได้จากตัวอย่างด้านบน คำสั่ง cat ใช้กับ New-File พิมพ์ contents ของไฟล์ ในเวลาเดียวกัน echo พิมพ์สิ่งที่ตามหลังคำสั่ง

คำสั่ง less ใช้เมื่อ output ที่พิมพ์โดยคำสั่งใดๆ ใหญ่กว่า screen space และต้อง scroll less ให้ user breakdown output และ scroll ผ่าน enter หรือ space keys

วิธีง่ายคือใช้ pipe operator (|)

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

หมายเหตุ: ใช้ flag -S กับ less เพื่อ enable line wrapping จะดู long lines ได้โดยไม่ต้อง scroll horizontally

ใช้ flag -N กับ less เพื่อแสดง line numbers มีประโยชน์ตอนต้องการรู้ line number ของ text เฉพาะ

ใช้ flags เหล่านี้ได้ดังนี้:

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

การใช้ less กับ pipe operator มีประโยชน์ในหลายสถานการณ์:

  • Viewing output ของ long-running command เช่น top หรือ htop.
  • Searching specific text ใน output ของคำสั่ง เช่น grep หรือ cat.

กลับไปที่รายการคำสั่ง ↑

คำสั่ง man ใน Linux

คำสั่ง man เป็นคำสั่ง Linux ที่มีประโยชน์มากที่ต้องรู้ ตอนทำงานกับ Linux packages ที่ download มาอาจมี functionality มากมาย รู้หมดเป็นไปไม่ได้

คำสั่ง man ใน Linux ใช้แสดง manual page สำหรับคำสั่งเฉพาะ ให้ข้อมูลรายละเอียดเกี่ยวกับคำสั่ง รวมถึง syntax, options, และ examples

ตัวอย่างการใช้ man:

  1. เปิด terminal พิมพ์ man ls เพื่อแสดง manual page สำหรับคำสั่ง ls.
root@ubuntu:~# man ls 

จะแสดงหน้าที่ประมาณนี้:

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; ... 

กลับไปที่รายการคำสั่ง ↑

คำสั่ง uname และ whoami

คำสั่ง uname และ whoami ให้คุณเข้าถึงข้อมูลพื้นฐานบางอย่างที่ useful ตอนทำงานหลาย systems

คำสั่ง uname ใน Linux แสดงข้อมูลเกี่ยวกับ kernel ของระบบ รวมถึง kernel name, hostname, kernel release, kernel version, และ machine hardware name

คำสั่ง whoami ใน Linux คืนค่า username ของ current user ย่อมาจาก "who am I?" และมักใช้ determine current user identity ใน shell scripts หรือ terminal

มาดู output ของทั้งสองคำสั่งและวิธีใช้:

root@ubuntu:~# uname -a 

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

Parameter -a กับ uname ย่อมาจาก "all" พิมพ์ข้อมูลครบ ถ้าไม่ใส่ parameter output จะได้แค่ "Linux"

หมายเหตุ: Flags สำคัญบางอย่างสำหรับ uname:

  1. ใช้ uname -s แสดง kernel name.
  2. ใช้ uname -n แสดง hostname.
  3. ใช้ uname -r แสดง kernel release.
  4. ใช้ uname -v แสดง kernel version.
  5. ใช้ uname -m แสดง machine hardware name.

กลับไปที่รายการคำสั่ง ↑

คำสั่ง tar, zip, และ unzip

คำสั่ง tar ใน Linux ใช้ create และ extract archived files เราสามารถ extract multiple archive files ได้ด้วย tar

เพื่อสร้าง archive ใช้ parameter -c และ extract ใช้ -x มาดูการทำงาน

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

บรรทัดแรก เราสร้าง archive ชื่อ Compress.tar ด้วย New-File และ New-File-Link คำสั่งถัดไป extract ไฟล์เหล่านั้นจาก archive

มา談 zip และ unzip ทั้งคู่ straightforward มาก ใช้ได้โดยไม่ต้องใส่ parameters มาดูตัวอย่าง

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เพราะเรามีไฟล์เหล่านั้นใน directory เดียวกัน unzip prompt ก่อน overwrite ไฟล์เหล่านั้น

กลับไปที่รายการคำสั่ง ↑

คำสั่ง grep ใน Linux

คำสั่ง grep เป็น powerful และ versatile text search tool ใน Linux และ Unix-based OS มันค้นหา specific patterns หรือ strings ในหนึ่งหรือหลายไฟล์ และ filter output ของคำสั่งอื่นๆ

คำสั่ง grep ย่อมาจาก "global regular expression print" สะท้อนความสามารถในการค้นหา regular expressions ข้ามหลายบรรทัดและไฟล์

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

นี่เป็น demonstration เรียบง่าย เรียนรู้เพิ่มเติมเกี่ยวกับ grep command

กลับไปที่รายการคำสั่ง ↑

คำสั่ง head และ tail

ตอน output ไฟล์ใหญ่ head และ tail ช่วยมาก คำสั่งเหล่านี้แสดงต้นหรือท้ายของไฟล์ ตามลำดับ ใช้กันทั่วไปเพื่อดูเนื้อหาไฟล์แบบเร็วโดยไม่ต้องเปิดใน text editor

head และ tail แสดง 10 บรรทัดแรก/ท้ายของไฟล์ by default เพื่อแสดงจำนวนบรรทัดที่ต่างกัน ใช้ option -n ตามด้วยจำนวนบรรทัด

ตัวอย่างการใช้ head และ tail:

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เห็นได้ head command แสดง 10 บรรทัดจากด้านบนของไฟล์

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

Tail command output 10 บรรทัดท้ายจากไฟล์

คำสั่งเหล่านี้ใช้ quick view file contents, monitor real-time updates สำหรับ troubleshooting, filter output จากคำสั่งอื่น และ log analysis

กลับไปที่รายการคำสั่ง ↑

คำสั่ง diff, comm, และ cmp

คำสั่ง diff, comm, และ cmp ใช้ compare files ใน Linux และ Unix-based OS คำสั่งเหล่านี้ระบุความแตกต่างระหว่างสองไฟล์ merge changes และ file comparison tasks อื่นๆ

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เห็นได้ด้านบน เราเพิ่ม text เล็กน้อยว่า "This line is edited" ใน New-File-Edited file

คำสั่ง cmp ใช้ compare สองไฟล์และแสดง first byte ที่ต่างกัน ใช้ระบุความแตกต่างของ binary files หรือ check corruption ในไฟล์

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

cmp บอกแค่ line number ที่ต่างกัน ไม่ใช่ actual text

คำสั่ง comm ใช้ compare สอง sorted files และแสดง lines ที่ unique แต่ละไฟล์ รวมถึง lines ที่ common ทั้งสอง

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

Text ชิดซ้ายอยู่แค่ file 1 Text กึ่งกลางอยู่แค่ file 2 และ text ชิดขวาอยู่ในทั้งสองไฟล์

ดูเหมือน comm ทำความเข้าใจได้ดีที่สุดตอน compare larger files และอยากเห็นทุกอย่างจัดเรียงไว้ด้วยกัน

ทั้งสามคำสั่งเป็น essential tools สำหรับทำงานกับไฟล์ใน Linux/Unix ด้วยการเข้าใจใช้ diff, comm, cmp อย่างมีประสิทธิภาพ คุณระบุความแตกต่าง merge changes และ file comparison tasks อื่นๆ ได้

คำสั่งเหล่านี้ช่วย identify และ resolve issues กับไฟล์ รวมถึง track changes และ maintain version control ไม่ว่าคุณจะเป็น developer หรือ system admin คำสั่งเหล่านี้เป็นส่วนจำเป็นของ toolkit

กลับไปที่รายการคำสั่ง ↑

คำสั่ง sort ใน Linux

คำสั่ง sort ใช้ sort lines ใน text file หรือ standard input ใน Linux/Unix เรียง ascending หรือ descending และ sorting operations อื่นๆ เช่น sort by fields หรือ custom sorting order

Basic syntax ของ sort:

root@ubuntu:~# sort <filename> 

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

By default sort เรียง lines ใน ASCII collating sequence ซึ่งอาจให้ unexpected results ตอน sort numbers หรือ special characters เพื่อ sort numbers ใน numerical order ใช้ option -n

ตัวอย่างใช้ -n:

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

คำสั่งด้านบนจะ sort lines ใน file.txt ใน numerical order

sort ยังใช้ sort lines based on specific fields ด้วย option -k

ตัวอย่างใช้ -k:

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

คำสั่งนี้ sort lines ใน file.txt based on field ที่สอง

sort เป็น powerful และ flexible tool สำหรับทำงานกับ text files ใน Linux/Unix ด้วยการเข้าใจใช้อย่างมีประสิทธิภาพ คุณสามารถ sort lines, sort by specific fields และ sorting operations อื่นๆ

คำสั่งเหล่านี้ช่วย organize และ analyze data และ file manipulation tasks อื่นๆ ไม่ว่าคุณจะเป็น developer หรือ system admin sort เป็น essential part ของ toolkit

กลับไปที่รายการคำสั่ง ↑

คำสั่ง export ใน Linux

คำสั่ง export ใน Linux/Unix ใช้ set environment variables Environment variables ใช้ store information ที่ processes หรือ commands ใช้ได้

เมื่อ environment variable ถูก set มันเข้าถึงได้โดย process หรือ command ใดๆ ที่รันใน shell เดียวกัน

Environment variables เก็บข้อมูลหลากหลายได้ เช่น configuration settings, user preferences, หรือ system information

ตัวอย่างใช้ export:

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เรียนรู้เพิ่มเติมเกี่ยวกับคำสั่ง export

กลับไปที่รายการคำสั่ง ↑

คำสั่ง ssh ใน Linux

คำสั่ง ssh ใน Linux/Unix สร้าง secure shell connection ไป remote server คำสั่งให้ secure encrypted connection ระหว่าง local และ remote servers ให้ users รันคำสั่งและ transfer files อย่างปลอดภัย

Basic syntax ของ ssh:

root@ubuntu:~ ssh username@remote-server 

คำสั่งนี้สร้าง ssh connection ไป remote-server ใช้ account username

ssh รองรับ options และ configurations หลากหลาย รวมถึง:

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

คำสั่ง service ใน Linux

คำสั่ง service ใน Linux ใช้ manage system services ซึ่งเป็น long-running processes ที่เริ่มที่ boot time และรันใน background Services เหล่านี้ให้ system functionalities ต่างๆ เช่น networking, database management, user authentication

คำสั่ง service ใช้ start, stop, restart, และ check status ของ services นี้ มันเป็น front-end ของคำสั่ง systemctl ที่จัดการ systemd service manager

Basic syntax ด้านล่าง:

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

เห็นได้จากภาพ ssh server กำลังรันบนระบบของเรา

กลับไปที่รายการคำสั่ง ↑

คำสั่ง ps, kill, และ killall

คำสั่ง ps, kill, และ killall ใช้ manage processes ใน Linux

ps ใช้แสดงข้อมูลเกี่ยวกับ current running processes บนระบบ ตัวอย่างใช้ ps:

Display list of all running processes:

root@ubuntu:~ ps -ef 

Display list of all processes for specific PID:

root@ubuntu:~ ps -p PID 

มาดูใน action:

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

เพื่อ demonstration เราสร้าง shell script ด้วย infinite loop และรันใน background

ด้วย symbol & เราส่ง process เข้า background เห็นได้มี bash process ใหม่ PID 14490 สร้างขึ้น

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

ตอนนี้ kill process ด้วย kill พิมพ์ kill ตามด้วย PID

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

แต่ถ้าคุณไม่รู้ PID และแค่อยาก kill process ด้วยชื่อ ใช้ killall

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

สังเกต PID 14490 ยัง active อยู่ เพราะทั้งสองครั้งเรา kill sleep process

เรียนรู้เพิ่มเติมเกี่ยวกับ ps command และ kill command

กลับไปที่รายการคำสั่ง ↑

คำสั่ง df และ mount

ตอนทำงานกับ Linux df และ mount เป็น utilities ที่มีประสิทธิภาพสำหรับ mount filesystems และได้รายละเอียด file system

df ใช้แสดง disk space used และ available บน file systems และ mount ใช้ mount file system หรือ device ไป specific directory

เวลาบอก mount หมายถึงเรา connect device ไป folder เพื่อ access files จาก filesystem ของเรา Default syntax ต่ำไป:

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

ในกรณีนี้ /dev/cdrom คือ device ที่ต้อง mount ปกติ mountable device อยู่ใน /dev folder mnt คือ destination folder ที่ mount device คุณเปลี่ยนเป็น folder ไหนก็ได้ แต่เราใช้ /mnt เพราะเป็น system default folder สำหรับ mount devices

ดู mounted devices และข้อมูลเพิ่มเติม ใช้ df command พิมพ์ df ได้ data ใน bytes ไม่อ่านง่าย ใช้ parameter -h ทำให้ human-readable

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

กลับไปที่รายการคำสั่ง ↑

คำสั่ง chmod และ chown

คำสั่ง chmod และ chown ใช้ modify file permissions และ ownership ใน Linux

chmod ใช้เปลี่ยน permissions ของไฟล์หรือ directory และ chown ใช้เปลี่ยน ownership

Default syntax ทั้งคู่คือ chmod <parameter> filename และ chown <user:group> filename

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

ในตัวอย่างด้านบน เราเพิ่ม executable permissions ให้ loop.sh ด้วย chmod เพิ่มเติมด้วย chown ทำให้ accessible เฉพาะ root user และ users ใน root group

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

สังเกต root root ส่วนตอนนี้เปลี่ยนเป็น www-data ซึ่งเป็น new user ที่มี full file ownership

เรียนรู้เพิ่มเติมเกี่ยวกับ Linux file Permissions และการใช้ chmod command

กลับไปที่รายการคำสั่ง ↑

คำสั่ง ifconfig และ traceroute

คำสั่ง ifconfig และ traceroute จัดการ network interfaces และ trace route ของ network packets ใน Linux

ifconfig ให้ list ของ all network interfaces พร้อม IP addresses, MAC addresses และข้อมูลอื่นๆ ของ interface

root@ubuntu:~ ifconfig 

มี multiple parameters แต่เราทำงานกับ basic command

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

คำสั่ง traceroute ใช้ trace route ของ network packets และ determine path ที่พวกมันใช้ไป destination

ตอนใช้ traceroute คุณระบุ IP address, hostname, หรือ domain name ของ endpoint

root@ubuntu:~ traceroute <destination address> 

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

ตอนนี้ localhost แค่ one hop (network interface เอง) คุณลองคำสั่งเดียวกันกับ domain name หรือ IP อื่นเพื่อดู routers ทั้งหมดที่ data packets ผ่านไป destination

กลับไปที่รายการคำสั่ง ↑

คำสั่ง wget ใน Linux

ถ้าอยาก download file จากภายใน terminal wget เป็นหนึ่งใน command-line utilities ที่ handy ที่สุด เป็นคำสั่ง Linux สำคัญที่ควรรู้ตอนทำงานกับ source files

เวลาระบุ link สำหรับ download ต้องเป็น direct link ไปยัง file ถ้า file ไม่สามารถ access โดย wget ได้ มันจะ download webpage ใน HTML format แทน actual file ที่คุณอยากได้

ลองตัวอย่าง Basic syntax ของ wget:

root@ubuntu:~ wget <link to file> 

หรือ,

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

Argument -c ให้ resume interrupted download ได้

กลับไปที่รายการคำสั่ง ↑

คำสั่ง ufw และ iptables

คำสั่ง ufw และ iptables ใช้ manage firewalls ใน Linux

UFW และ IPTables เป็น firewall interfaces สำหรับ Linux Kernel's netfilter firewall IPTables ส่ง firewall rules ไป Netfilter โดยตรง ในขณะที่ UFW config rules ใน IPTables ซึ่งส่ง rules ไป Netfilter

ทำไมต้อง UFW ตอนมี IPTables? เพราะ IPTables ยากสำหรับมือใหม่ UFW ทำให้ง่ายมาก ดูตัวอย่างลอง allow port 80 สำหรับ webserver

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

มั่นใจว่าคุณเข้าใจแล้วว่าทำไม UFW ถูกสร้างขึ้น! ดู syntax ง่ายแค่ไหน Firewalls ทั้งสอง comprehensive มาก สร้าง configuration ใดๆ สำหรับ network ของคุณได้ เรียนรู้พื้นฐาน UFW หรือ IPTables อย่างน้อย เพราะนี่คือ Linux commands ที่คุณต้องรู้

เรียนรู้เพิ่มเติมเกี่ยวกับ managing firewall ด้วย ufw และ iptables

กลับไปที่รายการคำสั่ง ↑

Package Managers ใน Linux

Linux distributions ต่างๆ ใช้ package managers ต่างกัน เราทำงานบน Ubuntu server จึงมี apt package manager แต่คนทำงานบน Fedora, Red Hat, Arch, หรือ Centos package manager จะต่างออกไป

ด้านล่างคือ commands วิธีใช้ package managers บน Linux distributions ต่างๆ

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

ให้ตัวเองเก่งกับ package manager ของ distribution ของคุณ จะทำให้สิ่งต่างๆ ง่ายขึ้นใน long run แม้คุณจะมี GUI based package management tool ติดตั้ง ลองใช้ CLI based tool ก่อนย้ายไป GUI utility เพิ่มคำสั่งเหล่านี้ในรายการ Linux commands ที่คุณต้องรู้

กลับไปที่รายการคำสั่ง ↑

คำสั่ง sudo ใน Linux

"With great power, comes great responsibility"

นี่คือ quote ที่แสดงเมื่อ sudo-enabled user (sudoer) ใช้ sudo ครั้งแรกเพื่อ escalate privileges คำสั่งนี้เทียบเท่า login as root (ขึ้นอยู่กับ permissions ที่คุณมีเป็น sudoer)

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

เพียงเพิ่มคำสั่ง sudo หน้าคำสั่งที่ต้องการรันด้วย escalated privileges แค่นั้นเอง ใช้ง่ายมาก แต่ก็อาจเป็น security risk เพิ่มถ้าผู้ใช้ malicious เข้าถึง sudoer ได้

เรียนรู้เพิ่มเติมเกี่ยวกับการสร้าง sudo user ใหม่ และ edit sudoers file

กลับไปที่รายการคำสั่ง ↑

คำสั่ง cal ใน Linux

เคยอยากดูปฏิทินใน terminal ไหม? จริงๆ มีคนอยากให้มันเกิดขึ้น และนี่มันมาแล้ว

คำสั่ง cal แสดงปฏิทินที่นำเสนอดีบน terminal เพียงพิมพ์คำสั่ง cal บน terminal prompt

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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

แม้เราไม่ต้องบ่อย แต่มันเพิ่มเติมที่ยอดเยี่ยม! เป็นตัวเลือกสุดโตสำหรับ terminal fans

กลับไปที่รายการคำสั่ง ↑

คำสั่ง alias

คุณมีคำสั่งที่รันบ่อยมากตอนใช้ terminal? อาจเป็น rm -r หรือ ls -l หรืออาจยาวกว่านั้นอย่าง tar -xvzf

นี่คือหนึ่งใน Linux commands ที่ boost productivity ที่คุณต้องรู้

ถ้าคุณรู้คำสั่งที่รันบ่อย มาสร้าง alias กัน

Alias คืออะไร?簡単พูด มันคืออีกชื่อหนึ่งสำหรับคำสั่งที่คุณ define เอง

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

ตอนนี้ ทุกครั้งที่คุณพิมพ์ lsl หรือ rmd ใน terminal คุณจะได้ output เหมือนใช้ full commands

ตัวอย่างข้างบนเป็น really small commands ที่พิมพ์มือได้ทุกครั้ง แต่ในบางสถานการณ์คำสั่งมี arguments เยอะเกินพิมพ์ ควรสร้าง shorthand version

กลับไปที่รายการคำสั่ง ↑

คำสั่ง dd ใน Linux

คำสั่งนี้สร้างขึ้นเพื่อ convert และ copy files จาก multiple file system formats ในปัจจุบัน ใช้แค่สร้าง bootable USB สำหรับ Linux แต่ยังมีสิ่งสำคัญบางอย่างทำได้

คำสั่ง dd ใน Linux เป็น versatile command สำหรับ low-level copying และ conversion of data ย่อมาจาก "data-description" หรือ "data definition" ใช้ copy และ convert data ระหว่าง different file formats และ storage devices

ตัวอย่าง ถ้าอยาก backup entire hard drive as-is ไปอีก drive ใช้ dd

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

if และ of ย่อมาจาก input file และ output file

มัน powerful และ flexible แต่อันตรายถ้าไม่ใช้ระวัง ตรวจสอบ syntax และแน่ใจว่ารู้ว่าคำสั่งจะทำอะไรก่อน execute เสมอ

กลับไปที่รายการคำสั่ง ↑

คำสั่ง whereis และ whatis

คำสั่ง whereis และ whatis ใช้ใน Linux search information เกี่ยวกับ programs และ files

whereis locate binary, source, และ manual pages สำหรับคำสั่งหรือ program เฉพาะ และ whatis แสดง short description ของคำสั่งหรือ program

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

whatis ให้ explanation ว่าคำสั่งจริงๆ คืออะไร

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

กลับไปที่รายการคำสั่ง ↑

คำสั่ง top ใน Linux

ไม่กี่ส่วนก่อน เราพูดถึง ps command คุณสังเกต ps จะ output active processes และจบตัวเอง

top command เหมือน CLI version ของ task manager ใน Windows

คำสั่ง top ใน Linux เป็น system monitoring tool แสดง real-time information เกี่ยวกับ system processes และ resource usage ให้ dynamic real-time view ของ system activity รวมถึง CPU usage, memory usage, และ process information

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

หมายเหตุ: ตัวอย่างใช้ top:

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

จะ sort process list by memory usage โดย processes ที่ใช้ memory มากสุดอยู่บนสุด

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

แทนที่ PID ด้วย ID ของ process ที่ต้องการ inspect

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

จะแสดง single summary screen ของ system resource usage

มัน powerful และ flexible tool สำหรับ monitor system activity และ troubleshoot performance issues

กลับไปที่รายการคำสั่ง ↑

คำสั่ง useradd และ usermod

คำสั่ง useradd และ usermod ใช้จัดการ user accounts ใน Linux

useradd หรือ adduser เป็นคำสั่งเดียวกัน adduser เพียง symbolic link ไป useradd คำสั่งนี้ให้เราสร้าง user ใหม่ใน Linux

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

คำสั่งด้านบนสร้าง user ใหม่ชื่อ JournalDev พร้อม home directory /home/JD

usermod ใช้ modify existing users คุณ modify ค่าใดๆ ของ user ได้ รวมถึง groups, permissions, etc.

ตัวอย่าง อยากเพิ่ม groups ให้ user พิมพ์:

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

กลับไปที่รายการคำสั่ง ↑

คำสั่ง passwd ใน Linux

ตอนนี้คุณรู้สร้าง user ใหม่แล้ว มาตั้ง password ให้พวกเขา คำสั่ง passwd ให้คุณตั้ง password สำหรับ account ของตัวเอง หรือถ้ามี permissions ตั้งให้ accounts อื่นๆ

ตัวอย่างใช้ passwd:

  • เปลี่ยน password สำหรับ current user:

    root@ubuntu:~# passwd 

จะ prompt ให้ enter new password สำหรับ current user

  • เปลี่ยน password สำหรับ specific user:

    root@ubuntu:~# passwd username 

แทนที่ username ด้วยชื่อ user ที่อยากเปลี่ยน password

  • Force user ให้เปลี่ยน password ที่ next login:

    root@ubuntu:~# passwd -f username 
  • Set expiration date สำหรับ user's password:

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

แทนที่ days ด้วยจำนวนวันก่อน password expires และ warn days ด้วยจำนวนวันก่อนหมดอายุที่ user จะได้ warning

นี่เพียงตัวอย่างเล็กน้อยของการใช้ passwd ใน Linux ด้วยการเข้าใจใช้คำสั่งนี้อย่างมีประสิทธิภาพ คุณจัดการ user accounts และ ensure ระบบของคุณปลอดภ

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพ

กลับไปที่รายการคำสั่ง ↑

Common Errors และ Debugging

ตอนทำงานกับ Linux commands คุณอาจเจอ errors และ issues ต่างๆ นี่คือปัญหา common และวิธีแก้:

1. แก้ "command not found" errors

ถ้าได้ command not found error หมายถึงคำสั่งที่พยายามรันไม่ถูก recognize โดย system เกิดจากหลายเหตุผล:

  1. คำสั่งไม่ได้ installed บนระบบของคุณ Install ได้ผ่าน package manager (เช่น apt, yum, dnf)

    ถ้าเห็น:

    bash: xyz: command not found 

    ใช้:

    which xyz 

    ถ้าคำสั่งไม่ได้ installed ลอง install:

    apt-get install <package-name> 
  2. คำสั่งไม่อยู่ใน system's PATH ของคุณ เพิ่ม directory ที่มีคำสั่งเข้า PATH

    export PATH=$PATH:/path/to/command 
  3. คุณ typo ตรวจสอบ spelling ของคำสั่ง

2. แก้ permission issues ด้วย sudo

ถ้าเจอ permission issues ใช้ sudo รันคำสั่งด้วย superuser privileges เช่น:

sudo command_name 

3. จัดการ File Conflicts

File conflicts เกิดเมื่อ multiple users หรือ processes พยายาม modify ไฟล์เดียวกันพร้อมกัน วิธีจัดการ:

  1. Use Version Control Systems (VCS): Tools อย่าง Git ช่วย manage file conflicts โดยให้ users merge changes และ resolve conflicts manually

    git merge <branch_name> 

    ถ้ามี conflicts Git จะ prompt ให้ resolve เปิด conflicting files ทำ changes ที่จำเป็น และ commit resolved files

    git add <resolved_file> git commit -m "Resolved merge conflict" 
  2. Locking Mechanisms: Implement file locking ป้องกัน multiple processes เขียนไฟล์เดียวกันพร้อมกัน ใช้ flock ใน Linux สร้าง lock บนไฟล์

    flock -x <file> -c "<command>" 
  3. Atomic Operations: ใช้ atomic operations ensure file writes complete ใน single step ลด risk ของ conflicts

    mv temp_file target_file 

    หรือ ใช้ mv กับ -f (force) หรือ cp กับ -i (interactive):

    mv -f file1 file2 cp -i file1 file2 

4. Debug Performance Bottlenecks

Performance bottlenecks ส่งผลต่อ efficiency ของระบบมาก ขั้นตอน debug และ resolve:

  1. Identify the Bottleneck: ใช้ tools อย่าง top, htop, vmstat, iostat monitor system performance และ identify resource causing bottleneck (CPU, memory, disk I/O, etc.)

    top 
  2. Analyze Logs: Check system และ application logs สำหรับ errors หรือ warnings ที่อาจบ่งชี้ performance issues

    tail -f /var/log/syslog 
  3. Optimize Code: Review และ optimize code เพื่อ improve performance หา inefficient algorithms, unnecessary computations, memory leaks

  4. Profile Your Application: ใช้ profiling tools อย่าง gprof, perf, valgrind analyze application performance และ identify slow functions หรือ memory issues

    gprof <executable> gmon.out 
  5. Scale Resources: ถ้า bottleneck มาจาก resource limitations พิจารณา scale up hardware หรือใช้ load balancing distribute load across multiple servers

FAQs

1. คำสั่ง Linux ที่ใช้บ่อยที่สุดคืออะไร?

คำสั่ง Linux ที่ใช้บ่อยที่สุดรวมถึง 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, และ clear

2. ฉันจะ list all available commands ใน Linux ได้อย่างไร?

คุณ list all available commands ใน Linux ได้ด้วยคำสั่ง compgen -c จะแสดง list ของ all commands ที่ available บนระบบของคุณ

3. ฉันจะหาไฟล์ใน Linux ได้อย่างไร?

คุณหาไฟล์ใน Linux ได้ด้วยคำสั่ง find Basic syntax คือ find <path> -name "<filename>" ที่ <path> คือ directory ที่เริ่มค้นหา และ <filename> คือชื่อไฟล์ที่หา เช่น find /home/user -name "example.txt"

4. ฉันจะ kill process ใน Linux ได้อย่างไร?

คุณ kill process ใน Linux ได้ด้วยคำสั่ง kill หา process ID (PID) ของ process ที่อยาก kill ด้วย ps หรือ top แล้วใช้ kill ตามด้วย PID เช่น kill 1234 ที่ 1234 คือ PID

5. ความแตกต่างระหว่าง cp และ mv คืออะไร?

cp ใช้ copy files หรือ directories ในขณะที่ mv ใช้ move หรือ rename files หรือ directories cp สร้าง duplicate ของ original file ทิ้ง original ไว้ ในขณะที่ mv move file ไป location ใหม่ ลบจาก original location

6. ฉันจะ check Linux system's memory usage ได้อย่างไร?

คุณ check memory usage ได้ด้วยคำสั่ง free คำสั่งนี้แสดง total amount ของ free และ used physical และ swap memory ในระบบ เช่น free -h แสดง memory usage ใน human-readable format

สรุป

ในบทช่วยสอนนี้ คุณสำรวจคำสั่ง Linux ที่จำเป็นกว่า 50 คำสั่ง ครอบคลุม file และ directory management, process control, user permissions, networking, text processing, และ shell utilities คุณยังทำงานผ่าน real-world examples, troubleshooting patterns, และ comparisons ระหว่างคำสั่งที่เกี่ยวข้อง

ตอนนี้คุณ navigate Linux filesystem, manage processes, configure permissions, และ troubleshoot common issues จาก command line ได้แล้ว คำสั่งเหล่านี้เป็น foundation ของ Linux system administration และ scripting ที่มีประสิทธิภาพ

เพื่อไปลึกกว่าบนหัวข้อเฉพาะ สำรวจ 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

เชี่ยวชาญ Linux: 50+ คำสั่งจำเป็นสำหรับการจัดการระบบอย่างมีประสิทธิภาพงานนี้ได้รับอนุญาตภายใต้ Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.