Linux Fundamentals Part 3

Welcome hai part three mein (aur finale bhi) of the Linux Fundamentals module. Ab tak, is series mein, aapne basic concepts ko practically samjha hai aur kuch important commands use kiye hain.

Is room mein aapko milenge kuch useful utilities aur applications jo rozana ke kaam mein aapko bahut help karenge. Saath hi, aap apni Linux-fu skills ko next level pe le jaoge by learning automation, package management, aur service/application logging ke baare mein.

File Mein Text Store Karna — Ab Efficient Tarika Seekhenge!

Ab tak humne sirf echo command aur pipe operators (> aur >>) se files mein text likha hai. Lekin jab file mein multiple lines hoti hain, toh yeh tarika efficient nahi hota.


Introducing Terminal Text Editors

Linux mein kuch popular text editors hote hain jo terminal mein chalate hain. Inmein se ek bahut easy hai nano, aur doosra thoda advanced hai VIM. (VIM pe TryHackMe pe ek alag room bhi hai!)


Nano

Nano se shuru karna bahut easy hai! Koi bhi file create ya edit karne ke liye bas ye command use karo:

nano filename

Jaise agar file ka naam myfile hai, toh:

nano myfile

Is command ko run karte hi nano launch ho jaayega jahan aap text type kar sakte ho, aur arrow keys se navigate kar sakte ho.


Nano ke Features:

    • Text search kar sakte ho

    • Copy-paste kar sakte ho

    • Kisi line number pe ja sakte ho

    • Current line number dekh sakte ho

Ye sab shortcuts Ctrl key ke saath use karte hain. Jaise Ctrl + X se nano se exit karte hain.


VIM

VIM thoda advanced editor hai. Aapko iske saare features abhi nahi aane chahiye, lekin thoda jaan lena faayde ka hai.


VIM ke Advantages:

    • Customizable hai, matlab apne hisaab se keyboard shortcuts set kar sakte ho

    • Syntax highlighting deta hai — code likhne ke liye perfect hai

    • Har terminal pe chalta hai, jab nano available na ho

    • Online bohot resources mil jaate hain jaise cheatsheets aur tutorials

    • TryHackMe pe ek special room bhi hai jahan VIM sikhaya jaata hai!

Files Download Karna (Wget)

Computing ka ek basic feature hota hai files transfer karna. Jaise ki aapko koi program, script, ya image download karni ho. Humare paas bahut tareeke hain files retrieve karne ke, aur is task mein hum wget command seekhenge.

wget command web se files download karne ke liye use hoti hai, jaise aap browser mein file access karte hain. Bas hume us file ka web address dena hota hai. Example: 

wget https://assets.tryhackme.com/additional/linux-fundamentals/part3/myfile.txt

Files Transfer Karna Apne Host Se – SCP (SSH)

SCP yaani Secure Copy, ek tarah se encrypted file transfer ka tariqa hai. Ye cp command se alag hai kyunki ye SSH protocol use karta hai, jo authentication aur encryption provide karta hai.

SCP kaam karta hai SOURCE aur DESTINATION ke model par:

    • Files/directories copy kar sakte hain current system se remote system pe

    • Files/directories copy kar sakte hain remote system se apne system pe

Bas hume usernames aur passwords pata hone chahiye dono systems ke liye.

Example:

Variable Value
Remote system IP 192.168.1.30
Remote user ubuntu
Local file name important.txt
Remote file name (store as) transferred.txt

Command banegi:

 
scp important.txt ubuntu@192.168.1.30:/home/ubuntu/transferred.txt

Reverse karne ke liye, remote se local pe file copy karne ke liye:

Variable Value
Remote system IP 192.168.1.30
Remote user ubuntu
Remote file name documents.txt
Local file name (store as) notes.txt

Command hogi:

scp ubuntu@192.168.1.30:/home/ubuntu/documents.txt notes.txt

Files Serve Karna Apne Host Se – Web Server (Python3 HTTPServer)

Ubuntu machines mein python3 pre-installed aata hai, jisme ek simple web server module hai HTTPServer. Isse aap apne computer ko ek lightweight web server bana sakte hain jahan se doosre systems files download kar sakte hain (commands jaise curl aur wget se).

Aap jis directory mein ye command chalayenge, wahi files serve hongi:

python3 -m http.server

Example:
tryhackme@linux3:/webserver# python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) …

Phir doosre terminal mein, file download karne ke liye:

wget http://MACHINE_IP:8000/myfile

Yahan MACHINE_IP ko apne machine ke IP se replace karo aur myfile ki jagah file ka naam daalo.

Note: Python3 server chalate waqt usi terminal ko open chhodna hota hai, aur naya terminal kholke wget command chalani hoti hai.


Ek Chhota Sa Limitation

Python3 ka HTTPServer file ka index nahi banata, matlab aapko exact filename aur location pata honi chahiye file download karne ke liye. Isi wajah se advanced lightweight webservers jaise Updog bhi popular hain. Lekin abhi ke liye hum Python ka HTTPServer use karenge.


Questions for You:

    • Connect karo deployed instance se (MACHINE_IP)

    • Python3 HTTPServer start karo tryhackme user ke home directory mein

    • Ek nayi terminal mein, download karo file http://MACHINE_IP:8000/.flag.txt

    • File ka content kya hai?


Tips:

    • Apne files create karo, download karo aur Python3 HTTPServer documentation padh ke aur seekho.

    • Jab kaam ho jaye, Python3 HTTPServer ko Ctrl + C se band kar dena.

Processes wo programs hote hain jo aapke machine pe chal rahe hote hain. Inhe kernel manage karta hai, jahan har process ka ek ID hota hai, jise PID (Process ID) kehte hain. PID is order me increment hota hai jisme process start hota hai. Matlab 60wa process ka PID 60 hoga.

Processes dekhna

Hum friendly ps command ka use kar sakte hain running processes ki list dekhne ke liye, jo user ki session ke processes aur kuch extra info jaise status code, session jo process chala raha hai, CPU ka kitna usage ho raha hai, aur actual program ya command ka naam jo execute ho raha hai, ye sab batata hai.

Note karo ki upar wale screenshot me, doosra process ps ka PID 204 hai, aur neeche diye command me ye PID increment hoke 205 ho jata hai.

Agar humein doosre users ke processes aur wo processes dekhne hain jo kisi session se nahi chal rahe (matlab system processes), toh humein ps command ke saath aux option dena padta hai, jaise:
ps aux

Note karo ki hum total 5 processes dekh sakte hain — aur dhyaan do ki ab humare paas “root” aur “cmnatic” dono dikh rahe hain.

Ek aur bahut useful command hai top command; top aapko real-time statistics deta hai system pe chal rahe processes ke baare mein, ek baar ka snapshot nahi, balki lagataar updated info. Ye statistics har 10 second me refresh hote hain, aur jab aap arrow keys ka use karke alag-alag rows browse karte ho tab bhi refresh ho jata hai. System ko samajhne ke liye ek aur badhiya command hai ye top command.

Processes ko manage karna

Aap processes ko signals bhej sakte ho jo unhe terminate karte hain; signals ke alag-alag types hote hain jo batate hain ki kernel process ko kitna “cleanly” handle karta hai. Kisi command ko kill karne ke liye, hum kill command ka use karte hain aur us process ka PID specify karte hain jise humein kill karna hai. Jaise, PID 1337 ko kill karne ke liye:
kill 1337

Yahan kuch signals hain jo process ko kill karte waqt bheje ja sakte hain:

    • SIGTERM – Process ko kill karta hai, lekin process ko kuch cleanup tasks karne ka time deta hai

    • SIGKILL – Process ko turant kill karta hai, bina cleanup ke

    • SIGSTOP – Process ko stop ya suspend karta hai

Processes kaise start hote hain?

Sabse pehle namespaces ke baare mein baat karte hain. Operating System (OS) namespaces ka use karta hai taaki computer ke resources (jaise CPU, RAM, priority) ko alag-alag processes ke beech divide kar sake. Sochiye ki aap apne computer ko slices mein baant rahe ho — jaise cake ke tukde. Har process ko us slice ka kuch hissa milta hai, jo ki overall available resources ka chhota portion hota hai.

Namespaces security ke liye bahut useful hain kyunki ye processes ko ek dusre se isolate karte hain — sirf wahi processes jo same namespace mein hain, ek dusre ko dekh sakte hain.

PID ke baare mein humne pehle baat ki thi, aur yahi uska role hota hai. Process jiska ID 0 hota hai, wo system ke boot hone par start hota hai. Ye process Ubuntu jaise systems me system ka init hota hai, jaise systemd, jo user ke processes ko manage karta hai aur OS aur user ke beech ka mediator hota hai.

For example, system boot hone ke baad, systemd pehla start hone wala process hota hai. Jo bhi program ya software hum start karte hain, wo systemd ka child process hota hai. Matlab wo systemd ke under control mein rehta hai, lekin apne alag process ke roop mein chalta hai (resources share karte hue). Isse process ko identify karna aur manage karna asaan hota hai.

Boot pe Processes/Services ko Start karwana

Kuch applications aise hote hain jo system ke boot hone par automatically start ho jate hain, jaise web servers, database servers, ya file transfer servers. Ye software aksar critical hote hain aur administrators inhe system ke boot-up ke time start karne ke liye set kar dete hain.

Is example me, hum apache web server ko manually start karenge aur phir system ko batayenge ki boot hone par apache2 ko launch kare.

Yahan aata hai systemctl ka use — ye command humein systemd process/daemon ke saath interact karne deta hai. Systemctl ka syntax kuch is tarah hota hai:
systemctl [option] [service]

Jaise, apache ko start karne ke liye:
systemctl start apache2
Simple lag raha hai na? Agar apache ko stop karna ho, toh option ko stop kar denge:
systemctl stop apache2

Systemctl ke saath hum ye chaar options use kar sakte hain:

    • Start

    • Stop

    • Enable

    • Disable


Linux me Backgrounding aur Foregrounding ka Introduction

Processes do states me chal sakte hain: Background me aur Foreground me. Jo commands aap terminal me run karte ho, jaise echo, wo foreground me chalte hain, matlab wo command terminal me directly dikhta hai aur uska output bhi turant aata hai. Ye commands aise hoti hain jo background me chalne ke liye nahi boli gayi hoti.

echo ka example le lo — iska output foreground me aata hai, lekin agar ye background me chale, toh output aapko turant terminal me nahi dikhega. Neeche wale screenshot me iska example hai.

Yahan hum echo "Hi THM" command chala rahe hain, jisme hum expect karte hain ki output turant terminal me wapas milega, jaise ki shuru me hota hai. Lekin jab hum command ke end me & operator lagate hain, toh output ki jagah humein sirf echo process ka ID milta hai, kyunki wo command background me chal raha hota hai.

Ye bahut useful hota hai aise commands ke liye jaise file copy karna, kyunki iska matlab hai ki aap file copy command ko background me chala sakte ho aur bina wait kiye apne next commands execute kar sakte ho.

Isi tarah, scripts chalate waqt bhi hum ye kar sakte hain — & operator ke alawa hum Ctrl + Z ka use karke bhi kisi process ko background me bhej sakte hain. Ye ek acha tareeka hai process ko temporarily “pause” karne ka, jaise niche example me dikhaya gaya hai.

Ye script baar-baar repeat karega “This will keep on looping until I stop!” jab tak aap is process ko stop ya suspend nahi karte. Ctrl + Z use karne se (jo terminal me T^Z ke roop me dikhata hai), humne process ko suspend kar diya hai. Ab terminal messages se bhara nahi rahega — jab tak hum ise foreground me nahi le aate, jiske baare me hum neeche baat karenge.

Foregrounding a process

Ab jab humare paas koi process background me chal raha hai, jaise hamara script background.sh (jo ps aux command se confirm kiya ja sakta hai), toh hum ise wapas foreground me la sakte hain taaki hum uske saath interact kar saken.

Jab humne apna process background me bhej diya ho, chahe wo Ctrl + Z se ho ya & operator se, tab hum fg command ka use karke us process ko wapas foreground me la sakte hain.

Jaise niche dikhaya gaya hai, fg command background process ko terminal ke focus me le aata hai, jahan script ka output phir se humare samne dikhne lagta hai aur hum uske saath interact kar paate hain.

Users kabhi-kabhi chahte hain ki system ke boot hone ke baad koi specific action ya task automatically chale. Jaise commands run karna, files ka backup lena, ya apne favourite programs jaise Spotify ya Google Chrome ko launch karna.

Ab hum baat karenge cron process ki, aur khaaskar kaise hum isse interact kar sakte hain crontabs ke through. Crontab ek aisa process hai jo boot ke time start hota hai, aur ye cron jobs ko manage aur facilitate karta hai — matlab scheduled tasks ko chalane ka kaam karta hai.

Crontab ek special file hoti hai jisme ek specific format follow kiya jata hai, jo cron process samajh ke ek-ek line ko step-by-step execute karta hai. Crontab me 6 specific values hoti hain:

ValueDescription
MINKis minute execute karna hai
HOURKis ghante execute karna hai
DOMMonth ke kis din execute karna hai
MONSaal ke kis mahine execute karna hai
DOWHafte ke kis din execute karna hai
CMDWo actual command jo execute hoga

Example ke liye, agar aap cmnatic ke “Documents” folder ka backup har 12 ghante me lena chahte hain, toh crontab me ye line likhenge:

0 */12 * * * cp -R /home/cmnatic/Documents /var/backups/

Crontab me ek interesting feature ye hai ki ye wildcard ya asterisk (*) ko support karta hai. Agar aap kisi specific field ke liye value dena nahi chahte, matlab aapko koi farak nahi padta ki wo month, din ya saal kya hai, bas command har 12 ghante execute ho, toh us field me bas * laga dijiye.

Shuru me ye thoda confusing lag sakta hai, isliye kuch ache online tools jaise Crontab Generator aur Cron Guru hain jo aapko friendly interface dete hain apne crontab format generate karne ke liye.

Crontab ko edit karne ke liye aap crontab -e command ka use kar sakte hain, jahan aapko ek editor (jaise Nano) select karne ka option milega jisme aap apna crontab edit kar sakte hain.

Packages aur Software Repos ka introduction

Jab developers apna software community ke liye submit karte hain, toh wo usse ek apt repository me daalte hain. Agar wo approve ho jata hai, toh unke programs aur tools publically available ho jate hain. Yahan Linux ke do bade faayde dikhte hain: user accessibility aur open source tools ki merit.

Ubuntu 20.04 Linux machine pe jab aap ls command chalate hain, toh ye files ek tarah se gateway ya registry ka kaam karti hain, jahan se aapko available software packages ke baare me pata chalta hai.

Operating System vendors apne repositories maintain karte hain, lekin aap apni list me community repositories bhi add kar sakte hain! Isse aap apne OS ki capabilities ko extend kar sakte hain. Additional repositories add karne ke liye aap add-apt-repository command ka use kar sakte hain ya phir kisi doosre provider ka repository list kar sakte hain. Kuch vendors ke repositories unke geographical location ke kareeb hote hain, jisse download tez hota hai.


Repositories ko Manage karna (Add aur Remove karna)

Ubuntu system par software install karne ke liye hum normally apt command use karte hain. Ye command package management software apt ka part hai. Apt ke paas tools ka ek pura suite hota hai jo hume packages aur software sources ko manage karne, install karne aur remove karne ki suvidha deta hai.

Repositories add karne ka ek tareeka hai add-apt-repository command ka use karna, jaise upar bataya gaya, lekin hum ab manually repository add aur remove karna sikhenge. Aap software dpkg jaise package installers se bhi install kar sakte hain, lekin apt ka fayda ye hai ki jab bhi aap system update karte hain, toh added repositories ke software updates bhi check hote hain.


Example: Sublime Text Repository Add Karna

Sublime Text ek text editor hai jo default Ubuntu repositories me nahi hota, isliye hum ise manually add karenge.

Software add karte waqt, integrity verify karne ke liye GPG (Gnu Privacy Guard) keys ka use hota hai. Ye keys developers ki taraf se ek safety check hoti hain, jisme ye prove karte hain ki software asli hai. Agar keys system ke trust ke saath match nahi karti, toh software download nahi hota.


Step 1: Sublime Text ke developers ki GPG key add karna

wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -

Note: TryHackMe instances me internet access nahi hota, isliye ye command wahan fail karega.


Step 2: Sublime Text 3 ka repository add karna

Trusted key add karne ke baad, ab hum Sublime Text 3 ka repository apne apt sources list me add karenge. Achha practice ye hai ki har community/3rd party repository ke liye alag file banai jaye.


Step 2.1: Ek file create karna

sudo nano /etc/apt/sources.list.d/sublime-text.list

Is file me ye repository line add karen (Ubuntu ke version ke hisaab se adjust kar sakte hain):

deb https://download.sublimetext.com/ apt/stable/

File save kar ke exit karen.

2.2. Ab Nano ya apne pasand ka koi text editor use karke is nayi file mein Sublime Text 3 repository add karke save karo:

2.3. Jab humne yeh entry add kar di, toh humein apt ko update karna hota hai taaki yeh nayi entry ko recognize kar sake — yeh apt update command se hota hai.

2.4. Jab successfully update ho jaye, tab hum software install kar sakte hain jo humne trust karke apt mein add kiya hai, apt install sublime-text command use karke.

Packages remove karna bhi utna hi aasaan hai jitna process ko reverse karna. Yeh kaam add-apt-repository --remove ppa:PPA_Name/ppa command use karke ya manually us file ko delete karke kiya ja sakta hai jo humne pehle add ki thi. Remove karne ke baad, bas apt remove [software-name-here] use karo, jaise ki apt remove sublime-text.

Humne thoda sa log files ke baare mein aur unka location Linux Fundamentals Part 1 mein discuss kiya tha. Lekin chalo ek quick recap kar lete hain. /var/log directory mein yeh files aur folders hote hain jo applications aur services ke logging information rakhte hain jo aapke system par chal rahi hoti hain. Operating System (OS) aajkal kaafi acha ho gaya hai in logs ko automatically manage karne mein, jise “rotating” process kehte hain.

Maine kuch logs highlight kiye hain jo Ubuntu machine par chal rahe teen services ke hain:

    • Apache2 web server

    • Fail2ban service ke logs, jo brute force attempts ko monitor karne ke liye use hota hai

    • UFW service, jo firewall ke liye use hota hai

Yeh services aur unke logs system ki health monitor karne aur uski protection ke liye bahut useful hote hain. Sirf itna hi nahi, web server jaise services ke logs mein har ek request ki information hoti hai — jo developers ya administrators ko performance issues diagnose karne ya kisi intruder ki activity investigate karne mein madad karta hai.

Misaal ke taur par, do tarah ke log files jo zyada important hote hain, wo hain:

    • access log

    • error log

Zaroori baat yeh hai ki kuch logs aise bhi hote hain jo OS khud kaise chal raha hai aur users ke actions, jaise authentication attempts, ke baare mein information store karte hain.

Welcome hai aapka Linux Fundamentals module ke end par. Aapki Linux familiarity time ke saath improve hogi jaise-jaise aap iske saath interact karte rahoge. Linux powerful kaam asaani se kar sakta hai (jo aapne hopefully is module mein discover kiya hoga).

Recap karte hain, is room mein aapne ye topics cover kiye:

    • Terminal text editors ka use

    • General utilities jaise content download karna aur python webserver se serve karna

    • Processes ka ek overview

    • System ko maintain aur automate karna crontabs, package management, aur logs review karke

Aap apni learning continue kar sakte ho kuch aur TryHackMe rooms mein jo Linux tools ya utilities ke dedicated hain:

error: Content is protected !!