มัน frustrating มากตอนคอมพิวเตอร์ไม่ทำงาน 제대로 และคุณไม่รู้ว่าทำไม แม้ไม่มี easy resolutions สำหรับทุก issue ที่คุณอาจเจอใน Ubuntu คุณสามารถใช้ resources เช่น Ubuntu error logs เพื่อ troubleshoot และ diagnose PC woes ของคุณ
Ubuntu Logs คืออะไร?
Ubuntu logs เป็น comprehensive files ที่ store record ของ all events ที่เกิดขึ้นบนคอมพิวเตอร์ รวมถึง logged information เกี่ยวกับ hardware, operating system และแม้แต่บาง software
Logs นี้ invaluable สำหรับ troubleshooting และ diagnosing process พวกมันให้ข้อมูลเช่น timestamps, context และ in-depth details เกี่ยวกับ events ที่เกิดทั่วระบบ เพื่อใช้ logs เหล่านี้ คุณต้องรู้ important types ก่อน
Types ของ Ubuntu Logs
แทนที่จะเขียนทุก bit ของ information ลงใน one log Ubuntu บันทึกข้อมูลใน separate logs ออกแบบสำหรับ different purposes แม้จะมี logs มากเกินไปที่จะ list ทั้งหมด มี important logs (และ log types) ไปป์น้อยที่คุณควร aware ก่อนพยายามใช้ troubleshooting
Perhaps the most prominent ของ all logs คือ systemd journal systemd เป็น integral service manager ใน Linux ดังนั้น issues กับหลาย operating system services ถูก recorded ใน systemd journal คุณ navigate log นี้ด้วยคำสั่ง journalctl
System logs ก็ very important เช่นกัน Logs นี้ directly pertain ถึง parts ต่างๆ ของ Ubuntu และอาจรวม things เช่น system messages System logs บางอย่างรวมถึง:
- The Authorization Log: Process ใดๆ ที่ต้องการ authorization เช่น sudo command หรือ user logins จะถูก recorded ใน log นี้
- The Daemon Log: Log นี้ pertain ถึง background services (หรือ daemons) เช่น Bluetooth และ SSH
- The Debug Log: Log นี้ให้ debugging information จาก system และ applications ที่ log ไป syslogd
- The Kernel Log: Log นี้รวม records ของ activity ที่เกี่ยวกับ Linux kernel
- The System Log: Log นี้ store records ที่รวม most types ของ global activity บนระบบของคุณ
- The Fail Log: Log นี้ store records ของ failed logins ทำให้มัน particularly helpful สำหรับเช็คว่ามีใครพยายาม break into ระบบของคุณหรือไม่
คอมพิวเตอร์ของคุณอาจมี application logs (เช่น Apache log files หรือ MySQL log files) เก็บอยู่ใน directory /var/log คุณใช้คำสั่ง ls เพื่อดู log files ทั้งหมดที่เก็บอยู่บนคอมพิวเตอร์:
ls /var/log
วิธีอ่าน Logs บน Ubuntu
ตอนนี้คุณ familiar กับ log types ต่างๆ บนระบบแล้ว คุณพร้อมจะเจาะลึกข้อมูลที่พวกมันเก็บ ก่อนเริ่ม สำคัญที่จะ note ว่า not all system logs ถูกเขียนในลักษณะเดียวกัน
บาง logs เก็บเป็น plaintext files ในขณะที่อีกบางส่วนเก็บเป็น binary files คุณต้อง familiar กับทั้งสองประเภทของไฟล์ - และ command-line tools ที่จับคู่กับพวกมัน - หากคุณอยาก effectively parse through records บนระบบ
Plaintext file logs ใช้ template ที่รู้จักในชื่อ RSYSLOG_TraditionalFileFormat และมี four basic fields: timestamp, hostname, application, และ message ตัวอย่าง kernel log ใช้ template นี้:
Unlike plaintext logs binary file logs ไม่สามารถอ่านได้ง่าย คุณต้องใช้ command-line utilities เช่น who, last, และ lastb เพื่ออ่าน logs เช่น utmp, wtmp, และ btmp ตามลำดับ คำสั่งเช่น utmpdump สำหรับ utmp variants และ systemctl สำหรับ journald ก็ออกแบบมาเพื่อ print binary log information ใน readable format
ใน most situations การรู้วิธี parse through logs อย่างมีประสิทธิภาพใน terminal จะสำคัญ คุณใช้คำสั่งอย่าง grep และ tail เพื่อ pull specific information โดยไม่ต้องอ่านทั้งหมดของ system logs อย่างเจ็บปวด
Best commands ที่คุณใช้ได้รวมถึง:
- grep: Searches สำหรับ string of characters ใน file
- tail: Prints 10 lines จากท้ายของ file
- head: Prints 10 lines จากหน้าของ file
- sort: Prints file reorganized ตาม specifications ของคุณ
Command-line tools เช่น utmpdump และ systemctl ก็มี helpful flags ที่คุณควรจดจำตอนทำงานใน terminal Flags เหล่านี้จะให้คุณ modify command และมี increased control over ماที่พิมพ์ใน terminal
Flags ที่เป็นประโยชน์โดยเฉพาะสำหรับ journalctl command:
- -b: ทำให้ journalctl return เฉพาะ entries ที่รวบรวมหลัง last reboot
- --since "YYYY-MM-DD HH:MM:SS" --until "YYYY-MM-DD HH:MM:SS": สั่ง journalctl return เฉพาะ entries ก่อนและ/หรือหลัง dates ที่ระบุ
- -p NUM: Filters entries โดย syslog priority levels (range จาก 0/emerg ถึง 7/debug)
Troubleshooting Errors ด้วย Ubuntu Logs
ตอนนี้คุณ familiar กับ system log types ต่างๆ และรู้วิธีอ่านผ่านพวกมันอย่างมีประสิทธิภาพแล้ว สิ่งที่เหลือคือ apply information ที่คุณรวบรวมไป troubleshooting process กระบวนการนี้มักต้องการ creativity
เป็น good idea ที่จะ approach troubleshooting โดยคิดถึง prominent characteristics ของ issue ที่คุณจัดการก่อน Issue เกิดตอนเปิด application 某个? ระบบ crash และ reboot ทุกครั้งที่ issue เกิด?
ตอนคุณคิดถึง characteristics ของ issue มันจะ naturally lead คุณไปยัง best logs บางส่วนสำหรับ glean information เกี่ยวกับมัน ตัวอย่าง หากระบบของคุณมี issues ระหว่าง boot process คุณอาจได้ helpful information โดย reference boot entries ใน journald
ใส่คำสั่งต่อไปนี้เพื่อ print all logged boots ใน journald:
journalctl --list-boots
Terminal จะ print list of recorded boots most recent boots จะอยู่ด้านล่างของ list ดู recorded dates และ times สำหรับแต่ละ boot จนคุณหา logged boot ที่ error เกิดขึ้น
เอา number จาก leftmost column เป็น NUM และใส่คำสั่งต่อไปนี้เพื่อได้ข้อมูลเพิ่มเติมเกี่ยวกับ boot:
journalctl -b -NUM -n
Comprehensive record ของข้อมูลเกี่ยวกับ boot จะ displayed หากมี unusual errors เกิดขึ้นระหว่าง boot process คุณใช้ information จาก record นี้ไปขั้นตอนต่อไปในการ troubleshoot
Principle เดียวกัน applies to many other issues หากคุณไม่รู้มากเกี่ยวกับ issue ที่คอมพิวเตอร์ประสบ อย่างไรก็ตาม อาจยากที่จะรู้เริ่มจากไหน มี logs ไปป์น้อยที่ stand out เป็น particularly helpful สำหรับ wide range ของ issues กับ Ubuntu systems
syslog เป็น best log เพื่อเริ่มต้นใน troubleshooting process เนื่องจากมัน effectively global log มัน very likely ที่จะมี information เกี่ยวกับ issue ที่คุณจัดการ หากคุณมี issues กับ authentication (เช่น sudo password not working error) หรือ booting ให้เช็ค auth.log หรือ boot.log
Search through most relevant logs ด้วย keywords related to your problem ตัวอย่าง หากคุณมี issues authenticating คุณ pull records จาก auth.log ด้วยคำสั่ง:
cat /var/log/auth.log | grep 'Authentication failure'
ในที่สุด คุณแน่นอนจะพบ information ที่ให้ excellent insight เมื่อคุณ pair information จาก error logs กับ research บนเว็บ คุณ very likely จะหา resources ที่ช่วย resolve issues กับระบบของคุณใน no time
อ้างอิง Online Resources เพื่อ Troubleshoot Linux Errors
คุณไม่ต้องผ่าน troubleshooting process คนเดียว ขณะที่คุณรวบรวม information เกี่ยวกับ issue ของคอมพิวเตอร์ คุณควรใช้ online resources เช่น Ask Ubuntu และ detailed guides ที่ MakeUseOf เพื่อเข้าใกล้ finally diagnosing และ repairing PC ของคุณ