Ravindra BagaleCourses & study guides English

Linux grep आणि find Commands उदाहरणांसह

Server वर हजारो files असतात, आणि log files मध्ये लाखो lines. त्या एकेक करून उघडता येत नाहीत. या chapter मध्ये प्रत्येक Linux admin आणि DevOps engineer रोज वापरतो त्या दोन commands शिकू: grep files च्या आतला text शोधतो, आणि find files स्वतः शोधतो. Practice आपण Linux server चे users ज्या files मध्ये असतात त्यावर करू, /etc/passwd आणि /etc/shadow, म्हणून आधी Linux users ना नंबर कसे देतो ते बघू.

या class मध्ये काय शिकणार

  • Linux users चे प्रकार: root (UID 0), system users आणि normal users, आणि sudo user म्हणजे काय
  • /etc/passwd आणि /etc/shadow: प्रत्येक line चा अर्थ आणि त्या कोण वाचू शकतं
  • grep: file न उघडता त्यातला text शोधणं; vi मध्ये /, n आणि N ने search
  • Pipe |: एका command चा output पुढच्या command ला देणं
  • grep चे options -i, -r आणि -n, आणि "Is a directory" message
  • find: नावाने, wildcards ने, type ने आणि permission ने; sudo आणि Permission denied
  • find | grep आणि wc -l, आणि -name कधी जास्त चांगलं
  • Admins logs मध्ये grep का करतात: server वर कोणी login केलं

1. झटपट उजळणी: networking

का: grep आणि find servers वर बहुतेक logs मधून networking आणि access चे प्रश्न सोडवायला वापरतात: कोणता IP जोडला गेला, कोणता port fail झाला. त्या ओळी वाचायला basics ताजे हवेत.

Classroom line

As long as you're in DevOps or cloud, never forget networking and IP.

झटपट उजळणी

  1. Security group म्हणजे एका EC2 instance भोवतीचा firewall. फक्त port 80 उघडा असेल तर visitors तुमच्या app पर्यंत फक्त port 80 वरच्या web server मधूनच पोचतात.
  2. Private IP फक्त त्याच्या private network मध्ये (office, किंवा तुमचं VPC) चालतो. Internet वरून पोचायला server ला public IP लागतो.
  3. एका company मधलं private network म्हणजे intranet; internet जगभरातली networks जोडतं.
  4. Private ranges: 10.0.0.0/8, 172.16.0.0/12 आणि 192.168.0.0/16. प्रत्येक IP मध्ये network part आणि host part असतो; subnet mask एक कुठे संपतो ते दाखवतो.
  5. 0.0.0.0 म्हणजे "कोणताही address". 127.0.0.1 म्हणजे localhost, स्वतः machine (पूर्ण 127.0.0.0/8 range loopback आहे).

अजून हवं असेल तर वाचा: Cloud साठी Networking Basics: IP Address, Client आणि Server सोप्या भाषेत आणि Public vs Private IP Address सोप्या भाषेत.

2. Linux users चे प्रकार आणि UIDs

का: तुम्ही काय करू शकता हे Linux तुम्ही कोण आहात त्यावरून ठरवतो. प्रत्येक user ला एक नंबर असतो, UID (user ID), आणि Linux नाव नाही, तो नंबर तपासतो.

काय: users चे तीन प्रकार आहेत.

  1. root, UID 0. Superuser. तो सगळं वाचू, बदलू आणि delete करू शकतो.
  2. System users, UID 1 ते 999. Services चालवायला Linux आणि install केलेलं software त्यांना बनवतं, उदा. sshd किंवा nginx. बहुतेक login करू शकत नाहीत.
  3. Normal users, UID 1000 आणि पुढे. हे म्हणजे माणसं. EC2 instance वरचा पहिला user (Amazon Linux वर ec2-user, Ubuntu वर ubuntu) 1000 असतो; तुम्ही बनवलेल्या पुढच्या user ला 1001 मिळतो.

sudo user हा चौथा प्रकार नाही. तो root म्हणून commands चालवायची परवानगी असलेला normal user आहे, Amazon Linux वर wheel group मधून, Ubuntu वर sudo group मधून.

Classroom line

(Root to a sudo user) Do your work yourself, don't come to me.

कसं: स्वतःचा user तपासा.

  1. id चालवा.
  2. uid=1000(ec2-user) वाचा: तुमचा UID आणि नाव.
  3. groups=... वाचा: wheel (Ubuntu वर sudo) म्हणजे तुम्ही sudo वापरू शकता.
  4. sudo id चालवा. आता ते uid=0(root) दाखवतं, कारण sudo command root म्हणून चालवतो.
कोण कोण: UID नुसार Linux users चे प्रकार root UID 0 Superuser: सगळं करू शकतो. System users UID 1-999 sshd, nginx सारख्या services चालवतात. बहुतेक login नाही. Normal users UID 1000+ माणसं: ec2-user किंवा ubuntu 1000, पुढचा user 1001. sudo user म्हणजे normal user ज्याला root म्हणून commands चालवायची परवानगी आहे(Amazon Linux वर wheel group, Ubuntu वर sudo). UID तेचrange. स्वतः बघा: id तुमचा UID, groups आणि नाव दाखवतो.

Figure 1. UID नुसार Linux users: root 0, system users 1 ते 999, normal users 1000 पासून. sudo user म्हणजे root म्हणून काम करू शकणारा normal user.

3. /etc/passwd आणि /etc/shadow

का: Server वर कोणते users आहेत, त्यांचा UID, home folder किंवा shell काय आहे हे जाणून घ्यायचं असेल तर ते सगळं एकाच file मध्ये आहे.

काय:

  1. /etc/passwd म्हणजे users ची list. एक line = एक user. ती सगळे वाचू शकतात.
  2. /etc/shadow मध्ये password hashes असतात. ती फक्त root वाचू शकतो.
  3. Hash परत password मध्ये बदलता येत नाही. विसरलेला password कधी "परत वाचला" जात नाही; तो sudo passwd USERNAME ने reset केला जातो.

/etc/passwd च्या line मध्ये : ने वेगळे केलेले सात fields असतात

ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash
  1. ec2-user: user name.
  2. x: password /etc/shadow मध्ये ठेवलेला आहे.
  3. 1000: UID.
  4. 1000: group ID.
  5. EC2 Default User: comment.
  6. /home/ec2-user: home folder.
  7. /bin/bash: login shell. इथे /sbin/nologin म्हणजे account login करू शकत नाही, system users साठी हे नेहमीचं आहे.

कसं:

  1. cat /etc/passwd चालवा. सगळे users दिसतात.
  2. cat /etc/shadow चालवा. Permission denied मिळतं: normal user ती वाचू शकत नाही.
  3. sudo cat /etc/shadow चालवा. Root म्हणून ते चालतं.
/etc/passwd ची एक line = एक user ec2-user:x:1000:1000:EC2 DefaultUser:/home/ec2-user:/bin/bash ec2-user 1. user name x 2. password /etc/shadow मध्ये 1000 3. UID 1000 4. group ID EC2 Default User 5. comment /home/ec2-user 6. home folder /bin/bash 7. login shell /etc/passwd: सगळे वाचू शकतात Names, UIDs, home folders, shells. /etc/shadow: फक्त root वाचू शकतो Password hashes. Hash परत वाचता येत नाही. Shell /sbin/nologin = हे account login करू शकत नाही. Passwordविसरलात? Reset करा: sudo passwd USERNAME.

Figure 2. /etc/passwd ची एक line: user name, x, UID, group ID, comment, home folder आणि shell. /etc/shadow मध्ये password hashes, ती फक्त root वाचू शकतो.

4. grep: file न उघडता text शोधा

का: एका log file मध्ये लाखो lines असू शकतात. ती editor मध्ये उघडणं slow असतं, terminal अडकू पण शकतो, आणि एक error शोधत वर scroll करत बसणं म्हणजे वेळ वाया.

Classroom line

(Scrolling up a giant log looking for the error) Go up, where is it, go up... That's why we use grep.

काय: grep file मध्ये एक शब्द (pattern) शोधतो आणि फक्त तो शब्द असलेल्या lines print करतो. File editor मध्ये कधी उघडलीच जात नाही.

Classroom line

It's grep, not grapes!

Classroom line

This is also an interview question: how do you find a pattern without opening the file?

कसं: practice file बनवा, ती vi मध्ये search करा, मग grep ने.

  1. SSH ने तुमच्या EC2 instance ला connect व्हा.
  2. vi demo चालवा (vim demo पण तसंच चालतं).
  3. i दाबा आणि Linux बद्दलच्या काही lines paste करा.
  4. Esc दाबा, :wq type करा आणि Enter दाबा.
  5. vi demo ने परत उघडा.
  6. /Linux type करून Enter दाबा. Cursor पहिल्या match वर जातो.
  7. पुढच्या match साठी n आणि मागच्या साठी N दाबा.
  8. Editor मधून बाहेर पडायला :q type करा.
  9. आता grep "Linux" demo चालवा. फक्त Linux असलेल्या lines print होतात, आणि तो शब्द highlight होतो.
Terminal: the command grep "Linux" demo, followed by three lines of text about package managers in which each Linux is highlighted in red, then the prompt again.

Whiteboard: grep "Linux" demo फक्त Linux असलेल्या lines print करतो, शब्द highlight करून.

आणखी उदाहरणं:

  1. grep ec2-user /etc/passwd तुमची स्वतःची user line print करतो.
  2. grep nologin /etc/passwd login न करू शकणारी accounts दाखवतो.
  3. sudo grep -i "error" /var/log/httpd/error_log Apache log मधले errors दाखवतो (Amazon Linux वर).

Classroom line

As a DevOps engineer, reading logs is your job.

5. Pipe: एका command चा output पुढच्या command ला

का: काही commands खूप print करतात. ते सगळं वाचण्याऐवजी तुम्ही ते थेट grep ला देऊ शकता आणि हवं तेवढंच ठेवू शकता.

काय: Pipe चिन्ह | डावीकडची command जे print करते ते सगळं उजवीकडच्या command ला input म्हणून देतं.

कसं:

  1. cat /etc/passwd चालवा. प्रत्येक user line स्क्रीनवरून जाते.
  2. cat /etc/passwd | grep ec2-user चालवा. फक्त तुमची line उरते.
  3. grep ec2-user /etc/passwd चालवा. तोच result: grep file स्वतः पण वाचू शकतो.
  4. ls /etc | grep conf चालवा. /etc मधली फक्त conf असलेली नावं.

Classroom line

I'll put this into a pipe, and put grep after it.

Pipe: एका command चा output पुढच्या command चाinput $ cat /etc/passwd | grep ec2-user 1. Pipe ने जोडलेल्या दोन commands. 2. cat /etc/passwd ची प्रत्येक line print करतो ... root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ec2-user:x:1000:1000:EC2 DefaultUser:/home/ec2-user:/bin/bash nginx:x:991:991:Nginx webserver:/var/lib/nginx:/sbin/nologin | grep ec2-user match lines ठेवतो 3. ... पण pipe त्या lines screen ऐवजी grep ला देतो. ec2-user:x:1000:1000:EC2 DefaultUser:/home/ec2-user:/bin/bash 4. grep फक्त match झालेली line दाखवतो. हेच: grep ec2-user/etc/passwd sign.

Figure 3. Animation: cat /etc/passwd प्रत्येक line print करतो, pipe त्या lines grep ला देतो, आणि grep फक्त ec2-user असलेली line ठेवतो.

6. grep options: -i, -r आणि -n

का: नेमकं spelling, किंवा कोणत्या file मध्ये शब्द आहे, हे नेहमी माहिती नसतं.

Classroom line

I don't know which file has the word, but I want to find it. How?

काय:

  1. Case महत्त्वाचा. grep साठी Linux आणि linux वेगवेगळे शब्द आहेत.
  2. -i case ignore करतो: Linux, linux आणि LINUX सगळे match होतात.
  3. -r (recursive) folder मधली प्रत्येक file आणि त्याच्या आतल्या folders मधल्या files शोधतो.
  4. -n प्रत्येक match च्या आधी line number लावतो.
  5. Options जोडता येतात: -irn = case ignore + recursive + line numbers.

Classroom line

In Linux, everything is case-sensitive.

कसं:

  1. grep -r "Linux" ~/ तुमच्या home folder मधली प्रत्येक file शोधतो. प्रत्येक line file च्या नावाने सुरू होते.
  2. grep -r "linux" ~/ वेगळ्या lines शोधतो: case महत्त्वाचा.
  3. grep -i "linux" ~/ Is a directory म्हणून थांबतो: grep files वाचतो, आणि folder म्हणजे file नाही.
  4. grep -ir "linux" ~/ चालतो: -r मुळे grep folder मध्ये जातो.
  5. grep -irn "linux" ~/ line numbers पण दाखवतो, file:line:text असे.
  6. man grep पूर्ण manual उघडतो (बाहेर पडायला q). grep --help options ची छोटी list print करतो.
Terminal: grep -r "Linux" /home/ubuntu/ prints matching lines from .viminfo and demo, each starting with the file name in purple; grep -r "linux" prints different lines from .bash_history; two grep -i "linux" /home/ubuntu/ commands each answer grep: /home/ubuntu/: Is a directory.

Whiteboard: grep -r folder मधली प्रत्येक file शोधतो; "Linux" आणि "linux" वेगवेगळ्या lines शोधतात; -r शिवाय folder वर grep केलं तर Is a directory.

Terminal: grep -irn "linux" /home/ubuntu/ prints matches from .viminfo, .bash_history and demo; each line starts with the file name in purple, a green line number such as 14 or 1, then the text with Linux highlighted.

Whiteboard: grep -irn: case ignore, folder मधली प्रत्येक file, आणि प्रत्येक file नावानंतर line number.

Classroom line

The best way: make a mistake, read the error, understand it, and try again.

रोज लागणारे grep options grep "Linux" demo demo file मधल्या Linux असलेल्या lines. फक्त तसंच case. grep -i "linux" demo Case ignore: Linux, linux, LINUX. -i = ignore case grep "linux" ~/ Error: Is a directory grep files वाचतो, folders नाही. grep -r "Linux" ~/ Folder मधल्या आणि आतल्या सगळ्या files. -r = recursive grep -irn "linux" ~/ Case ignore, recursive, line numbers सह. file:line:text Help: man grep (पूर्ण manual, बाहेर q) किंवा grep --help (छोटीlist).

Figure 4. grep options: default नेमका case, case ignore साठी -i, पूर्ण folder साठी -r, line numbers साठी -n. -r शिवाय folder वर grep केलं तर Is a directory.

Listing ची अजून एक युक्ती: ls -R एक folder आणि त्याच्या आतला प्रत्येक folder tree सारखा list करतो. ls -R ~/ करून बघा.

7. find: file स्वतः शोधा

का: grep ला कुठे बघायचं ते माहिती असावं लागतं. File कुठे आहे हेच माहिती नसेल तर find लागतो.

काय: find एका folder मधून आणि त्याखालच्या प्रत्येक folder मधून फिरतो आणि प्रत्येक match चा path print करतो.

find WHERE -type f|d|l -name "NAME"
  1. WHERE: कुठून सुरू करायचं तो folder: . (इथे), ~/, /home किंवा / (पूर्ण server). find ला नेहमी starting folder लागतो; तो pipe मधून list वाचत नाही.
  2. -type f = files, -type d = folders (directories), -type l = links.
  3. -name "NAME": नेमकं नाव. * हे wildcard: "*.log" = .log ने संपणारं प्रत्येक नाव.
  4. -perm 644: नेमकी ती permission असलेल्या files.

Classroom line

It goes into every file and searches.

कसं:

  1. find . -name "demo" सध्याच्या folder मध्ये आणि खाली demo शोधतो.
  2. find /home -name "demo" /home पासून सुरू करतो.
  3. find . -name "*.log" इथल्या आणि खालच्या सगळ्या .log files list करतो.
  4. find / -name "passwd" पूर्ण server वरून, normal user म्हणून, न वाचता येणाऱ्या folders साठी Permission denied lines चा पूर येतो.
  5. sudo find / -name "passwd" प्रत्येक folder वाचतो आणि प्रत्येक match list करतो.
  6. find / -name "passwd" 2>/dev/null हा दुसरा मार्ग: तो error lines लपवतो आणि results ठेवतो.
  7. sudo find / -type f -name "passwd" फक्त files list करतो; passwd नावाचा folder सोडला जातो.
  8. sudo find / -type d -name "nginx" फक्त folders list करतो.
  9. sudo find /var/www -perm 644 permission 644 असलेल्या files list करतो.

नावाआधी -name विसरलात तर find paths must precede expression म्हणून थांबतो. -name जोडा आणि परत चालवा.

Terminal: a few lines find: './var/log/amazon': Permission denied and similar from an earlier run, then the command sudo find . -name "passwd" run from the top folder, followed by a list of paths ending in passwd such as ./snap/core22/2437/etc/pam.d/passwd, ./snap/core22/2437/usr/bin/passwd and ./etc/pam.d/passwd.

Whiteboard: sudo शिवाय, find न वाचता येणाऱ्या folders साठी Permission denied दाखवतो. sudo सह तो प्रत्येक passwd file पूर्ण path सह list करतो.

find command डावीकडून उजवीकडे वाचा sudo find / -type f -name "passwd" sudo प्रत्येक folder वाचा find command / कुठून सुरू -type f फक्त files (d = folders) -name "passwd" नेमकं नाव, wildcards चालतात आणखी उदाहरणं find . -name "*.log" # all .log files here and below sudo find /var/www -type d # only folders sudo find /var/www -perm 644 # files with permission644 find ला नेहमी starting folder लागतो. नावाआधी -name विसरलात?find थांबतो: paths must precede expression.

Figure 5. sudo find / -type f -name "passwd" डावीकडून उजवीकडे वाचा, wildcards, folders आणि permissions ची आणखी उदाहरणं.

find कसा शोधतो: folder मागून folder $ find / -name passwd / → etc/ root/ usr/ home/ … 1. find / पासून सुरू होतो आणि खालचा प्रत्येक folder उघडतो. find: '/root': Permission denied find: '/proc/1/map_files': Permission denied 2. Normal user म्हणून, न वाचता येणारे folders Permissiondenied दाखवतात. $ sudo find / -name passwd 3. sudo सह find प्रत्येक folder वाचतो आणि प्रत्येक match दाखवतो. /etc/passwd /etc/pam.d/passwd /usr/bin/passwd ... every match, in every folder 4. passwd नावाच्या प्रत्येक file चा पूर्ण path.

Figure 6. Animation: find starting point पासून प्रत्येक folder उघडतो. sudo शिवाय काही folders Permission denied दाखवतात; sudo सह प्रत्येक match list होतो.

8. find | grep, आणि wc -l ने मोजणं

का: कधी तुम्हाला ज्यांच्या path मध्ये कुठेही एक शब्द आहे असे सगळे paths हवे असतात, किंवा फक्त ते किती आहेत ते जाणून घ्यायचं असतं.

काय:

  1. find पूर्ण paths print करतो. मग grep तो शब्द असलेल्या lines (paths) ठेवतो.
  2. grep शब्द path मध्ये कुठेही match करतो, folder names सुद्धा.
  3. wc -l lines मोजतो.

कसं:

  1. sudo find / -type f | grep "passwd" ज्यांच्या path मध्ये passwd आहे अशी प्रत्येक file list करतो.
  2. नीट बघा: /usr/share/doc/passwd/copyright सारखी file फक्त तिच्या path मधल्या एका folder चं नाव passwd असल्यामुळे list होते.
  3. ज्यांच्या स्वतःच्या नावात passwd आहे त्या files साठी फक्त find वापरा: sudo find / -type f -name "*passwd*".
  4. मोजायला | wc -l जोडा: sudo find / -type f -name "*passwd*" | wc -l.
find | grep पूर्ण path match करतो; -nameफक्त नाव sudo find / -type f | grep passwd /etc/passwd /etc/pam.d/passwd /usr/bin/passwd /usr/share/doc/passwd/copyright शेवटची file copyright आहे: ती match झाली कारण तिच्या path मधला folder passwd आहे. Lines मोजायला | wc -l जोडा. sudo find / -type f -name "*passwd*" /etc/passwd /etc/pam.d/passwd /usr/bin/passwd फक्त ज्यांच्या स्वतःच्या नावात passwd आहे त्या files. नेमक्या नावासाठी stars काढा. Path मधल्या कोणत्याही शब्दाने find output filterकरायला grep; फक्त file नाव महत्त्वाचं असेल तर-name.

Figure 7. find | grep शब्द path मध्ये कुठेही match करतो; find -name फक्त file चं नाव match करतो. मोजायला | wc -l जोडा.

9. Admins logs मध्ये grep का करतात

का: Logs म्हणजे server ची डायरी. Site बंद पडली किंवा कोणी घुसायचा प्रयत्न केला, की grep लाखो ओळींमधून महत्त्वाच्या पाच ओळी काढतो.

स्वतःच्या servers वर (on-premises, company च्या data centre मध्ये) किंवा EC2 वर, कोणीतरी "काल रात्री कोणी login केलं?" आणि "कोणी आत घुसायचा प्रयत्न केला का?" अशा प्रश्नांची उत्तरं द्यावी लागतात. उत्तरं log files मध्ये असतात, आणि grep ती काही seconds मध्ये शोधतो.

  1. Amazon Linux वर: sudo journalctl -u sshd | grep "Accepted" यशस्वी SSH logins दाखवतो.
  2. sudo journalctl -u sshd | grep -E "Failed|Invalid" fail झालेले प्रयत्न दाखवतो.
  3. Ubuntu वर याच lines /var/log/auth.log मध्ये पण असतात: sudo grep "Accepted" /var/log/auth.log.
कोणी login केलं? डोळ्यांनी नाही, grep ला विचारा $ sudo journalctl -u sshd | grep -E"Accepted|Failed|Invalid" sshd: Accepted publickey for ec2-user fromCLIENT_IP port 50412 ssh2 sshd: Failed password for invalid user admin fromCLIENT_IP port 41022 sshd: Invalid user test from CLIENT_IP port 39816 sshd: Accepted publickey for ec2-user fromCLIENT_IP port 50630 ssh2 Accepted यशस्वी login: कोण, आणि कुठून. Failed, Invalid user कोणी प्रयत्न केला आणि fail: यावर लक्ष ठेवा. Ubuntu या lines /var/log/auth.log मध्ये पण लिहितं: sudo grep"Accepted" /var/log/auth.log

Figure 8. कोणी login केलं? SSH log मध्ये Accepted (यशस्वी logins) आणि Failed किंवा Invalid user (fail झालेले प्रयत्न) साठी grep करा.

Linux हे multi-user system आहे: एकाच वेळी बरेच लोक SSH ने login असू शकतात, प्रत्येक जण आपलं काम करत. आत्ता कोण login आहे ते बघायला who चालवा.

10. पूर्ण flow एका page वर

  1. id तुमचा UID दाखवतो: root 0, system users 1 ते 999, normal users 1000+.
  2. cat /etc/passwd users list करतो; sudo cat /etc/shadow hashes दाखवतो (फक्त root); passwords sudo passwd USERNAME ने reset करा.
  3. grep "word" file match lines print करतो; vi मध्ये /word, n, N.
  4. command | grep word दुसऱ्या command चा output filter करतो.
  5. -i case ignore, -r पूर्ण folder, -n line numbers; Is a directory म्हणजे -r जोडा.
  6. find WHERE -type f -name "NAME"; पूर्ण server साठी sudo, किंवा 2>/dev/null.
  7. find ... | grep word path मध्ये कुठेही match करतो; -name फक्त नाव; | wc -l मोजतो.
  8. journalctl -u sshd | grep "Accepted" (Amazon Linux) किंवा grep "Accepted" /var/log/auth.log (Ubuntu) कोणी login केलं ते दाखवतो.

11. Hands-on lab: terminal मधून users, text आणि files शोधा

हा lab का. Server वर प्रत्येक file scroll करून बघता येत नाही. हा lab खरे प्रश्न (मी कोण, कोणते users आहेत, file कुठे आहे, कोणत्या file मध्ये एखादा शब्द आहे) grep, find आणि pipe ने सोडवतो.

  1. तुमचा UID बघा
    • Command: id
    • काय दिसेल: uid=1000(ec2-user) gid=1000(ec2-user) ने सुरू होणारी ओळ, groups मध्ये wheel.
    • Check करा: id root → uid=0(root) gid=0(root) groups=0(root)
    • का: तुम्ही काय करू शकता हे Linux तुमच्या number वरून ठरवतं: 0 म्हणजे root, 1 ते 999 system users, 1000 आणि पुढे माणसं. wheel मुळे तुमच्यासाठी sudo चालतो.
  2. /etc/passwd मध्ये तुमची ओळ शोधा
    • Command: grep ec2-user /etc/passwd
    • काय दिसेल: ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash
    • Check करा: : ने वेगळे केलेले सात fields; शेवटचा shell.
    • का: grep फक्त जुळणाऱ्या ओळी print करतो, म्हणून पूर्ण file ऐवजी एकच ओळ मिळते.
  3. /etc/shadow वाचून बघा
    • Command: cat /etc/shadow
    • काय दिसेल: cat: /etc/shadow: Permission denied
    • Check करा: sudo grep ec2-user /etc/shadow ec2-user: ने सुरू होणारी एक ओळ print करतो.
    • का: Password hashes /etc/shadow मध्ये असतात, जी फक्त root वाचू शकतो. /etc/passwd मध्ये passwords नसतात म्हणून ती सगळे वाचू शकतात.
  4. Case दुर्लक्षित करा आणि ओळींना numbers द्या
    • Command: grep -in ROOT /etc/passwd
    • काय दिसेल: 1:root:x:0:0:root:/root:/bin/bash सारख्या ओळी, प्रत्येकीसोबत line number.
    • Check करा: -i शिवाय grep ROOT /etc/passwd काहीच print करत नाही.
    • का: Exact spelling माहीत नसेल तेव्हा -i मदत करतो; editor मध्ये कोणती ओळ उघडायची ते -n सांगतो.
  5. पूर्ण folder मध्ये शोधा
    • Command: mkdir -p ~/lab/logs && echo "error disk full" > ~/lab/logs/a.log && echo "all good" > ~/lab/logs/b.log && grep -r error ~/lab
    • काय दिसेल: /home/ec2-user/lab/logs/a.log:error disk full
    • Check करा: फक्त a.log दिसते.
    • का: -r प्रत्येक subfolder मधल्या प्रत्येक file मध्ये बघतो, आणि प्रत्येक match समोर file चं नाव print करतो.
  6. Pipe ने मोजा
    • Command: grep -c bash /etc/passwd
    • काय दिसेल: एक छोटा number, उदाहरणार्थ 2 (root आणि ec2-user).
    • Check करा: grep bash /etc/passwd | wc -l तोच number print करतो.
    • का: Pipe | एका command चं output पुढच्या command मध्ये पाठवतो. इथे दोन्ही पद्धती bash ने login करू शकणारे users मोजतात.
  7. नावाने file शोधा
    • Command: sudo find / -type f -name passwd
    • काय दिसेल: /etc/passwd, /etc/pam.d/passwd आणि /usr/bin/passwd सारखे paths.
    • Check करा: प्रत्येक ओळ नेमक्या passwd ने संपणारा पूर्ण path.
    • का: find file स्वतः शोधतो, / पासून सुरू करून. sudo शेकडो Permission denied ओळी टाळतो; -type f folders सोडतो.
  8. Pattern ने शोधा, मग filter करा
    • Command: find ~/lab -name "*.log"
    • काय दिसेल: /home/ec2-user/lab/logs/a.log आणि /home/ec2-user/lab/logs/b.log.
    • Check करा: find ~/lab -name "*.log" | grep a.log फक्त पहिली ठेवतो.
    • का: find ला दिसण्याआधी shell ने *.log expand करू नये म्हणून quotes. grep मध्ये pipe केल्याने paths कोणत्याही शब्दाने filter होतात.
  9. साफ करा
    • Command: rm -r ~/lab
    • काय दिसेल: काहीच नाही.
    • Check करा: ls ~ मध्ये आता lab नाही. Instance stop करा.
    • का: Server वर राहिलेल्या practice files पुढच्या search मध्ये गोंधळ करतात.

12. Try at home

Try at home

Task 1: users

  1. id आणि sudo id चालवा आणि UIDs ची तुलना करा.
  2. grep ec2-user /etc/passwd चालवा आणि सातही fields ची नावं सांगा.
  3. Login न करू शकणारी accounts मोजायला grep nologin /etc/passwd | wc -l चालवा.
  4. cat /etc/shadow, मग sudo cat /etc/shadow करून बघा.

Task 2: grep

  1. Linux आणि linux असलेल्या काही lines सह demo बनवा.
  2. grep "Linux" demo, मग grep -i "linux" demo चालवा, आणि तुलना करा.
  3. grep -i "linux" ~/ चालवा, message वाचा, मग -r ने fix करा.
  4. -n जोडा आणि line numbers शोधा.

Task 3: find

  1. find ~/ -name "demo".
  2. find / -name "passwd", मग तेच sudo सह.
  3. sudo find / -type f -name "*passwd*" | wc -l.
  4. sudo find / -type f | grep "passwd" | wc -l, आणि count वेगळा का ते समजावा.

Recap

थोडक्यात

  1. root UID 0, system users 1 ते 999, normal users 1000 आणि पुढे; sudo user म्हणजे root म्हणून काम करू शकणारा normal user.
  2. /etc/passwd users list करतो (सात fields); /etc/shadow मध्ये password hashes, फक्त root; विसरलेले passwords reset होतात, वाचले जात नाहीत.
  3. grep file न उघडता फक्त match lines print करतो; vi मध्ये /word, n आणि N.
  4. Pipe | एका command चा output पुढच्या command ला देतो.
  5. grep case-sensitive आहे; -i, -r आणि -n ने case ignore, folders शोधणं आणि line numbers. -r शिवाय folder दिला तर Is a directory.
  6. find WHERE -type f -name "NAME" नाव, type किंवा permission ने files शोधतो; पूर्ण server साठी sudo (किंवा 2>/dev/null).
  7. find | grep path मध्ये कुठेही match करतो, -name फक्त नाव, आणि wc -l मोजतो.
  8. कोणी login केलं ते बघायला admins logs मध्ये grep करतात.

सराव Tasks

या scenario बद्दल

Tokyo Ramen House हा टोकियो मधला रेस्टॉरंट व्यवसाय आहे. Yuki Tanaka त्यांचं IT व cloud काम पाहतात. त्यांचे पैसे JPY मध्ये मोजले जातात आणि AWS resources ap-northeast-1 Region मध्ये चालतात. खालच्या tasks मध्ये तुम्ही या lesson मध्ये शिकलेलं वापरून त्यांची खरी कामं करता. सगळी नावं, account IDs, IP addresses व numbers सरावासाठी काल्पनिक आहेत.

0 / 10

सोपं Memory तपासा

का: RAM भरल्यास server slow होतो किंवा apps बंद होतात.

काय करायचं: वापरलेली व मोकळी memory human-readable units मध्ये दाखवा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

सोपं Log मध्ये errors शोधा

का: grep फक्त शब्द असलेल्या lines दाखवतो, त्यामुळे problems पटकन सापडतात.

काय करायचं: /var/log/tokyoramen/app.log मधल्या ERROR असलेल्या सगळ्या lines दाखवा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम जुळणाऱ्या lines मोजा

का: Count मुळे आज एखादी गोष्ट किती वेळा झाली ते पटकन कळतं.

काय करायचं: /var/log/nginx/access.log मधल्या " 404 " असलेल्या lines मोजा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम Case न पाहता line numbers सह search

का: Logs मध्ये "Timeout" व "TIMEOUT" दोन्ही असतात. Line numbers मुळे नेमकी जागा उघडता येते.

काय करायचं: /var/log/tokyoramen/api.log मध्ये timeout शोधा, upper/lower case न पाहता, line numbers सह.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम नावाने files शोधा

का: find folder tree मध्ये pattern जुळणाऱ्या files शोधतो.

काय करायचं: /var/log/tokyoramen खाली .log ने संपणाऱ्या सगळ्या files शोधा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

मध्यम Permission number

का: Key files private असाव्यात.

काय करायचं: Owner ला read+write व इतरांना काहीच नाही, chmod number?

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

अवघड मोठ्या files शोधा

का: Disk भरल्यावर आधी मोठ्या files शोधा.

काय करायचं: /var खाली कुठेही 100 MB पेक्षा मोठ्या files शोधा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

अवघड पूर्ण folder मध्ये search

का: Settings कोणत्याही file मध्ये असू शकतात. Recursive search सगळ्या तपासतो.

काय करायचं: /etc/tokyoramen खालच्या सगळ्या files मध्ये DB_HOST असलेल्या lines line numbers सह शोधा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

अवघड जुन्या files शोधा

का: जुन्या files disk भरतात. -mtime +N, N दिवसांपेक्षा जुन्या files शोधतो.

काय करायचं: /backup/tokyoramen खाली 42 दिवसांपेक्षा आधी बदललेल्या regular files शोधा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

अवघड चालू process शोधा

का: Pipe पूर्ण process list grep कडे पाठवतो, म्हणजे हवं तेच दिसतं.

काय करायचं: सगळे processes दाखवा व फक्त node असलेल्या lines ठेवा.

कसं करायचं:
  1. हे काम करणारा Linux command शोधा.
  2. Task ला लागणारे options (उदा. -r, -h) जोडा.
  3. File नावं, folders किंवा users योग्य क्रमाने लिहा.
  4. System files/users बदलत असाल तर sudo वापरा. Check answer दाबा.

उजळणी, तुलना आणि मुलाखत cards

हा lesson काही मिनिटांत revise करा. Cards phone वर save करा किंवा PNG download करा. प्रत्येक card वरचा QR हा lesson उघडतो.

उजळणी card: grep ने text शोधा. Logs मध्ये हजारो lines. सगळ्या वाचणं अशक्य. grep फक्त शब्द असलेल्या lines दाखवतो. -i case ignore, -n line numbers, -r folders मध्ये. grep WORD FILE, protected logs साठी sudo. कोण login झालं:
उजळणी card: grep ने text शोधा · PNG download करा
उजळणी card: Pipe |. एक command क्वचितच हवं तेच देतो. | एका command चं output पुढच्या command ला input म्हणून देतो. छोटे tools जोडा: list, filter, count. bash असलेले users मोजा:
उजळणी card: Pipe | · PNG download करा
उजळणी card: find ने files शोधा. File चं नाव माहीत पण कुठे आहे ते नाही. find नाव, type किंवा permission ने folders मध्ये शोधतो. find WHERE -name PATTERN; Permission denied टाळायला sudo. सगळ्या conf files:
उजळणी card: find ने files शोधा · PNG download करा
उजळणी card: /etc/passwd आणि /etc/shadow. Linux users आणि passwords कुठे ठेवतं ते माहीत हवं. /etc/passwd मध्ये users (कोणीही वाचू शकतो). /etc/shadow मध्ये password hashes (फक्त root). passwd cat ने वाचा; shadow ला sudo. passwd ची line:
उजळणी card: /etc/passwd आणि /etc/shadow · PNG download करा
तुलना: grep vs find. grep: Files आतला text शोधतो. find: नाव, type किंवा permission ने files शोधतो.
तुलना: grep vs find · PNG download करा
मुलाखत card: Pipe | काय करतो? उदाहरण द्या.. एका command चं output पुढच्या command मध्ये पाठवतो. छोटे tools मिळून शक्तिशाली one-liners होतात. Failed logins मोजा:
मुलाखत card: Pipe | काय करतो? उदाहरण द्या. · PNG download करा
मुलाखत card: find vs grep: प्रत्येक काय शोधतो?. find file NAMES आणि attributes शोधतो. grep files आतला TEXT. एक file कुठे आहे ते; दुसरा तिच्यात काय आहे ते. find /etc -name "*.conf" vs grep -rn "listen" /etc/nginx.
मुलाखत card: find vs grep: प्रत्येक काय शोधतो? · PNG download करा
मुलाखत card: Normal users /etc/passwd वाचू शकतात पण /etc/shadow नाही, का?. passwd मध्ये public info; shadow मध्ये password hashes. कोणीही वाचू शकलं तर hashes offline crack होऊ शकतात. ls -l /etc/shadow ---------- root दाखवतो.
मुलाखत card: Normal users /etc/passwd वाचू शकतात पण /etc/shadow नाही, का? · PNG download करा

अजून cards: Ravi Sir's Cards.