Introduction
במערכת לינוקס, הכל מטופל כקובץ. עם זאת, ישנם קבצים המקבלים טיפול מיוחד, בין אם בגלל המיקום שבו הם מאוחסנים, כגון קבצים זמניים, או בגלל האופן שבו הם פועלים עם מערכת הקבצים, כגון קישורים. בשיעור זה נלמד היכן קבצים אלו ממוקמים, כיצד הם פועלים וכיצד לנהל אותם.
קבצים זמניים
קבצים זמניים הם קבצים המשמשים תוכניות לאחסון נתונים הנדרשים לזמן קצר בלבד. אלו יכולים להיות נתוני תהליכים פעילים, קבצי יומן קריסה, קבצים שנשמרו אוטומטית, קבצי ביניים המשמשים בזמן המרת קובץ, קבצי מטמון וכדומה.
מיקום הקבצים הזמניים
גרסה 3.0 של תקן ההיררכיה של מערכת הקבצים (FHS) מגדירה מיקומים סטנדרטיים עבור קבצים זמניים במערכות לינוקס. כל מיקום נועד למטרה והתנהגות שונה, ומומלץ למפתחים לעקוב אחר הקונבנציות שהגדיר ה-FHS בעת כתיבת נתונים זמניים לדיסק.
/tmp– על פי ה-FHS, תוכניות לא אמורות להניח שקבצים שנכתבו כאן יישמרו בין הפעלת התוכניות. ההמלצה היא שתיקייה זו תימחק במהלך אתחול המערכת, אם כי אין חובה לכך./var/tmp– מיקום נוסף לקבצים זמניים, אך זה לא אמור להימחק במהלך אתחול המערכת, כלומר קבצים המאוחסנים כאן בדרך כלל ישמרו בין הפעלות./run– תיקייה זו מכילה נתוני משתנים בזמן ריצה המשמשים תהליכים פעילים, כגון קבצי מזהה תהליך (.pid). תוכניות הזקוקות ליותר מקובץ ריצה אחד עשויות ליצור כאן תיקיות משנה. מיקום זה חייב להימחק במהלך אתחול המערכת.
הרשאות על קבצים זמניים
ניהול תיקיות זמניות במערכת מרובת משתמשים מציב אתגר לגבי הרשאות גישה. על פניו, אפשר לחשוב שתיקיות אלו יהיו ניתנות לכתיבה לכל, אך איך נוכל למנוע ממשתמש אחד למחוק או לשנות קבצים שנוצרו על ידי אחר?
הפתרון הוא הרשאה מיוחדת שנקראת “ביט הדבקה” (sticky bit), החלה על תיקיות וקבצים. עם זאת, מסיבות אבטחה, קרנל הלינוקס מתעלם מביט הדבקה כאשר הוא מוחל על קבצים.
תיקיות עם ביט הדבקה מופעלות מראות את האות t במקום ה-x בהרשאות לאחרים בתוצאה של הפקודה ls -l. לדוגמה, בואו נבדוק את ההרשאות לתיקיות /tmp ו-/var/tmp:
$ ls -ldh /tmp/ /var/tmp/
drwxrwxrwt 25 root root 4,0K Jun 7 18:52 /tmp/
drwxrwxrwt 16 root root 4,0K Jun 7 09:15 /var/tmp/
כפי שניתן לראות, האות t מחליפה את x בהרשאות לאחרים, מה שמציין ששתי התיקיות מוגדרות עם ביט הדבקה.
כדי להגדיר את ביט הדבקה על תיקייה באמצעות chmod בסימון מספרי, יש להשתמש בסימון ארבע ספרות עם הספרה הראשונה 1. לדוגמה:
$ chmod 1755 temp
במצב סימבולי, השתמש בפרמטר t. לדוגמה, +t כדי להפעיל את ביט הדבקה ו--t כדי לבטל אותו:
$ chmod +t temp
הבנת קישורים
כבר אמרנו שבמערכת לינוקס הכל מטופל כקובץ. אבל יש סוג מיוחד של קובץ שנקרא קישור, ובמערכת לינוקס קיימים שני סוגים של קישורים:
- קישורים סימבוליים – נקראים גם “קישורים רכים”. הם מצביעים על הנתיב של קובץ אחר. אם תמחק את הקובץ אליו הקישור מצביע (הנקרא יעד), הקישור ימשיך להתקיים, אבל הוא “יפסיק לפעול” כיוון שהוא מצביע על “שום דבר”.
- קישורים קשיחים – חשבו על קישור קשיח כשם נוסף לקובץ המקורי. הם לא עותקים, אלא ערכים נוספים במערכת הקבצים המצביעים לאותו מקום (inode) בדיסק.
TIP
Inode הוא מבנה נתונים המאחסן תכונות עבור אובייקט (כמו קובץ או תיקייה) במערכת קבצים. בין התכונות הנמצאות בו ניתן למצוא שם הקובץ, הרשאות, בעלות, ועל אילו בלוקים בדיסק שמורים הנתונים של האובייקט. חשבו על זה ככניסה במאגר, ומכאן השם שנגזר מ-“index node”.
עבודה עם קישורים קשיחים
יצירת קישורים קשיחים
הפקודה ליצירת קישור קשיח בלינוקס היא ln. התחביר הבסיסי הוא:
$ ln TARGET LINK_NAME
ה-TARGET חייב להיות קיים כבר (זהו הקובץ שהקישור יפנה אליו), ואם היעד אינו נמצא בתיקייה הנוכחית או אם ברצונך ליצור את הקישור במקום אחר, יש לציין את הנתיב המלא אליו. לדוגמה, הפקודה:
$ ln target.txt /home/carol/Documents/hardlink
תיצור קובץ בשם hardlink בתיקיית /home/carol/Documents/ המצביע לקובץ target.txt בתיקייה הנוכחית.
אם תדלג על הפרמטר האחרון (LINK_NAME), ייווצר קישור עם אותו שם כמו היעד בתיקייה הנוכחית.
ניהול קישורים קשיחים
קישורים קשיחים הם ערכים במערכת הקבצים שיש להם שמות שונים אך מצביעים על אותם נתונים בדיסק. כל השמות הללו שווים ויכולים לשמש להפניה לקובץ. אם תשנה את התוכן של אחד השמות, התוכן של כל שאר השמות המצביעים לאותו קובץ ישתנה, כיוון שכל השמות הללו מצביעים על אותם נתונים.
אתה יכול לבדוק זאת באמצעות הפרמטר -i של הפקודה ls. לדוגמה, שים לב לתוכן התיקייה הבא:
$ ls -li
total 224
3806696 -r--r--r-- 2 carol carol 111702 Jun 7 10:13 hardlink
3806696 -r--r--r-- 2 carol carol 111702 Jun 7 10:13 target.txt
המספר שלפני ההרשאות הוא מספר ה-Inode. שים לב ששני הקבצים hardlink ו-target.txt חולקים את אותו מספר 3806696. זה בגלל שאחד הוא קישור קשיח של השני.
אבל איזה מהם הוא המקורי ואיזה מהם הוא הקישור? אתה לא יכול לדעת, כי לצרכים פרקטיים הם אותו הדבר.
שימו לב שכל קישור קשיח המצביע על קובץ מגדיל את ספירת הקישורים של הקובץ. זוהי המספר שמופיע מיד לאחר ההרשאות בתוצאת הפקודה ls -l. כברירת מחדל, לכל קובץ יש ספירת קישורים של 1 (לתיקיות יש ספירה של 2), וכל קישור קשיח שמתווסף מגדיל את הספירה ב-1. לכן, הסיבה לספירת הקישורים של 2 בקבצים ברשימה למעלה.
בניגוד לקישורים סימבוליים, ניתן ליצור קישורים קשיחים רק לקבצים, ושני הקבצים (הקישור והיעד) חייבים להיות באותה מערכת קבצים.
העברת והסרת קישורים קשיחים
מכיוון שקישורים קשיחים מטופלים כקבצים רגילים, ניתן למחוק אותם עם rm ולהעבירם או לשנות את שמם במערכת הקבצים עם mv. מאחר שקישור קשיח מצביע על אותו inode של היעד, ניתן להעביר אותו בחופשיות ללא חשש של “שבירת” הקישור.
קישורים סימבוליים
יצירת קישורים סימבוליים
הפקודה המשמשת ליצירת קישור סימבולי היא גם ln, אך עם הפרמטר -s. לדוגמה:
$ ln -s target.txt /home/carol/Documents/softlink
זה ייצור קובץ בשם softlink בתיקיית /home/carol/Documents/, המצביע על הקובץ target.txt בתיקייה הנוכחית.
כמו בקישורים קשיחים, ניתן לדלג על שם הקישור כדי ליצור קישור עם אותו שם כמו היעד בתיקייה הנוכחית.
ניהול קישורים סימבוליים
קישורים סימבוליים מצביעים על נתיב אחר במערכת הקבצים. ניתן ליצור קישורים רכים לקבצים ולתיקיות, אפילו על מחיצות שונות. קל לזהות קישור סימבולי בתוצאת הפקודה ls:
$ ls -lh
total 112K
-rw-r--r-- 1 carol carol 110K Jun 7 10:13 target.txt
lrwxrwxrwx 1 carol carol 12 Jun 7 10:14 softlink -> target.txt
בדוגמה שלמעלה, האות הראשונה בהרשאות עבור הקובץ softlink היא l, המציינת קישור סימבולי. כמו כן, אחרי שם הקובץ ניתן לראות את שם היעד אליו הקישור מצביע, הקובץ target.txt.
שימו לב שבתצוגות של קבצים ותיקיות, לקישורים הרכים עצמם תמיד מוצגות ההרשאות rwx עבור המשתמש, הקבוצה ואחרים, אך בפועל ההרשאות עבורם זהות לאלה של היעד.
העברת והסרת קישורים סימבוליים
כמו קישורים קשיחים, ניתן למחוק קישורים סימבוליים באמצעות rm ולהעבירם או לשנות את שמם באמצעות mv. עם זאת, יש להיזהר במיוחד בעת יצירתם כדי למנוע “שבירת” הקישור אם הוא מועבר מהמיקום המקורי שלו.
בעת יצירת קישורים סימבוליים יש לזכור שאם לא צוינה דרך מלאה, המערכת מתייחסת למיקום היעד כאל נתיב יחסי למיקום הקישור. זה עלול ליצור בעיות אם הקישור או הקובץ אליו הוא מצביע יועברו.
הדוגמה הבאה תסביר זאת טוב יותר: נניח שיש לנו קובץ בשם original.txt בתיקייה הנוכחית, ואנו רוצים ליצור קישור סימבולי אליו בשם softlink. נוכל להשתמש בפקודה:
$ ln -s original.txt softlink
ולכאורה הכל יהיה בסדר. נבדוק עם ls:
$ ls -lh
total 112K
-r--r--r-- 1 carol carol 110K Jun 7 10:13 original.txt
lrwxrwxrwx 1 carol carol 12 Jun 7 19:23 softlink -> original.txt
כפי שניתן לראות, הקישור softlink מצביע על (→) original.txt. עם זאת, נניח שנעביר את הקישור לתיקיית האב וננסה להציג את התוכן שלו באמצעות הפקודה less:
$ mv softlink ../
$ less ../softlink
../softlink: No such file or directory
מכיוון שהנתיב ל-original.txt לא צוין במלואו, המערכת מניחה שהוא נמצא באותה תיקייה כמו הקישור. כאשר זה כבר לא נכון, הקישור מפסיק לעבוד.
הדרך למנוע זאת היא לציין תמיד את הנתיב המלא ליעד בעת יצירת הקישור:
$ ln -s /home/carol/Documents/original.txt softlink
כך, לא משנה לאן תעביר את הקישור, הוא עדיין יעבוד, כיוון שהוא מצביע על המיקום המוחלט של היעד. נבדוק עם ls:
$ ls -lh
total 112K
lrwxrwxrwx 1 carol carol 40 Jun 7 19:34 softlink -> /home/carol/Documents/original.txt
תשובות לתרגילים מודרכים
-
תוכנית צריכה ליצור קובץ זמני לשימוש חד פעמי, שלא יהיה צורך בו לאחר סגירת התוכנית. באיזו ספריה נכון ליצור את הקובץ הזה?
התשובה היא: בספריית
/tmp, שמיועדת לקבצים זמניים שלא צריכים להישמר לאחר סגירת התוכנית. -
איזו ספריה זמנית חייבת להתנקות במהלך תהליך האתחול (boot process)?
התשובה היא: ספריית
/tmp, בהתאם להנחיות ה-FHS (Filesystem Hierarchy Standard). -
מהו הפרמטר ב-
chmodבמצב סימבולי להפעלת ה-“sticky bit” על ספריה?התשובה היא: הפרמטר הוא
+t, לדוגמה:chmod +t directory_name -
יש קובץ בשם
document.txtבספריית/home/carol/Documents. מהי הפקודה ליצור קישור סימבולי אליו בשםtext.txtבספריה הנוכחית?התשובה היא:
ln -s /home/carol/Documents/document.txt text.txt -
הסבר את ההבדל בין קישור קשיח (hard link) לבין עותק של קובץ.
קישור קשיח הוא לא כפילות של הקובץ אלא שם נוסף שמצביע על אותו inode (בלוקי מידע) במערכת הקבצים. כל שינוי בקובץ יגרום לשינוי בקישור הקשיח וההפך. עותק, לעומת זאת, הוא עותק עצמאי עם בלוקים נפרדים, כך ששינויים בקובץ המקורי לא ישפיעו על העותק.
תשובות לתרגילים לחקר עצמי
-
בתוך ספריה נוצר קובץ בשם
recipes.txt. בספריה זו נוצרו קישור קשיח בשםreceitas.txtוקישור סימבולי בשםrezepte.txt. מה יקרה לקישורrezepte.txtאם הקובץreceitas.txtיימחק? ומדוע?מכיוון ש-
rezepte.txtהוא קישור סימבולי, אם הקובץreceitas.txtיימחק, הקישורrezepte.txtישבר ולא יפעל. זה יקרה כי הקישור הסימבולי יפנה לקובץ שכבר לא קיים. -
יש לך כונן USB מחובר למערכת, ומותקן ב-
/media/youruser/FlashA. אתה רוצה ליצור בבית שלך קישור בשםschematics.pdfהמפנה לקובץesquema.pdfבכונן ה-USB. אתה כותב את הפקודה:ln /media/youruser/FlashA/esquema.pdf ~/schematics.pdfמה יקרה? ומדוע?
הפקודה תיכשל מכיוון שקישורים קשיחים אינם יכולים לחצות בין מערכות קבצים שונות, וכונן ה-USB נמצא על מערכת קבצים נפרדת.
-
הנה הפלט הבא של
ls -lah:$ ls -lah total 3.1M drwxr-xr-x 2 carol carol 4.0K Jun 17 17:27 . drwxr-xr-x 5 carol carol 4.0K Jun 17 17:29 .. -rw-rw-r-- 1 carol carol 2.8M Jun 17 15:45 compressed.zip -rw-r--r-- 4 carol carol 77K Jun 17 17:25 document.txt -rw-rw-r-- 1 carol carol 216K Jun 17 17:25 image.png -rw-r--r-- 4 carol carol 77K Jun 17 17:25 text.txt- כמה קישורים מצביעים על הקובץ
document.txt? התשובה: 4. - האם מדובר בקישורים קשיחים או קישורים סימבוליים? התשובה: קישורים קשיחים.
- איזה פרמטר צריך להוסיף ל-
lsכדי לראות את מספר ה-inode של כל קובץ? התשובה: הפרמטר הוא-i.
- כמה קישורים מצביעים על הקובץ
-
יש קובץ בשם
clients.txtב-~/Documentsוספריה בשםsomedirשבה קובץ אחר בשםclients.txt. יצרת קישור סימבולי בשםpartners.txtשמצביע עלclients.txtב-somedir. העברת את הקישור לתיקיית~/Documentsובדקת את התוכן שלו. האם הקישור יפעל? ואם כן, איזה קובץ יוצג?הקישור לא יעבוד מכיוון שהקישור הסימבולי מצביע על קובץ עם שם יחסי, ולכן אחרי ההעברה הקישור יישבר.
-
הנה הפלט של שני הקבצים:
-rw-r--r-- 1 carol carol 19 Jun 24 11:12 clients.txt lrwxrwxrwx 1 carol carol 11 Jun 24 11:13 partners.txt -> clients.txtמה הן הרשאות הגישה של
partners.txt? מדוע?הרשאות הגישה הן
rwxrwxrwx, כי קישורים סימבוליים תמיד מוצגים עם הרשאות מלאות, אך בפועל ההרשאות האמיתיות הן אלו של הקובץ שהקישור מצביע עליו (במקרה זהclients.txt).
סיכום
בשיעור זה למדת על:
- מיקום הקבצים הזמניים.
- ההרשאות המיוחדות החלות על קבצים זמניים.
- ההבדל בין קישורים סימבוליים לקישורים קשיחים.
- כיצד ליצור קישורים.
- כיצד להעביר, לשנות שם או למחוק קישורים.
הפקודות שנלמדו בשיעור זה:
ln- הפרמטר
-iבפקודתls
תשובות לתרגילים לחקר עצמי
-
בתוך ספריה נוצר קובץ בשם
recipes.txt. בנוסף, נוצרו קישור קשיח בשםreceitas.txtוקישור סימבולי בשםrezepte.txt. הפלט של הספריה צריך להיראות כך:$ touch recipes.txt $ ln recipes.txt receitas.txt $ ln -s receitas.txt rezepte.txt $ ls -lhi total 160K 5388833 -rw-r--r-- 4 carol carol 77K jun 17 17:25 receitas.txt 5388833 -rw-r--r-- 4 carol carol 77K jun 17 17:25 recipes.txt 5388837 lrwxrwxrwx 1 carol carol 12 jun 24 10:12 rezepte.txt -> receitas.txtאם
receitas.txtנמחק, מה יקרה לקישורrezepte.txt? ומדוע?הקישור הסימבולי
rezepte.txtיפסיק לפעול כי קישורים סימבוליים מצביעים על שמות קבצים ולא על inode. לאחר מחיקתreceitas.txt, השם כבר לא קיים והקישור יישבר. -
יש לך כונן USB מותקן במערכת בנתיב
/media/youruser/FlashA. אתה רוצה ליצור קישור לבית שלך בשםschematics.pdf, שמצביע לקובץesquema.pdfבכונן ה-USB. אתה מריץ את הפקודה:$ ln /media/youruser/FlashA/esquema.pdf ~/schematics.pdfמה יקרה? ומדוע?
הפקודה תיכשל עם שגיאת
Invalid cross-device link. זה נובע מכך שקישורים קשיחים אינם יכולים להצביע לקבצים שנמצאים על התקנים או מחיצות שונות. במקרה זה יש להשתמש בקישור סימבולי על ידי הוספת-s. -
הפלט הבא של
ls -lah:$ ls -lah total 3,1M drwxr-xr-x 2 carol carol 4,0K jun 17 17:27 . drwxr-xr-x 5 carol carol 4,0K jun 17 17:29 .. -rw-rw-r-- 1 carol carol 2,8M jun 17 15:45 compressed.zip -rw-r--r-- 4 carol carol 77K jun 17 17:25 document.txt -rw-rw-r-- 1 carol carol 216K jun 17 17:25 image.png -rw-r--r-- 4 carol carol 77K jun 17 17:25 text.txt- כמה קישורים מצביעים על הקובץ
document.txt? התשובה: 3 קישורים, משום שמספר הקישורים הכולל הוא 4. - האם אלו קישורים קשיחים או קישורים סימבוליים? התשובה: קישורים קשיחים, כי מספר הקישורים גדל רק בקישורים קשיחים.
- איזה פרמטר יש להוסיף לפקודת
lsכדי לראות את מספר ה-inode של כל קובץ? התשובה: הפרמטר הוא-i.
- כמה קישורים מצביעים על הקובץ
-
בתוך ספריית
~/Documentsישנו קובץ בשםclients.txtותיקיה בשםsomedirשמכילה קובץ בשםclients.txtאחר. לאחר יצירת קישור סימבולי בשםpartners.txt, ביצעת העברה ל-~/Documents. האם הקישור יפעל?הקישור יפעל ויציג את הקובץ
clients.txtשנמצא ב-~/Documents, מכיוון שהקישור הסימבולי פונה לשם יחסי, ולכן הוא יפנה לקובץ שנמצא באותה תיקיה לאחר ההעברה. -
שני הקבצים:
-rw-r--r-- 1 rigues rigues 19 Jun 24 11:12 clients.txt lrwxrwxrwx 1 rigues rigues 11 Jun 24 11:13 partners.txt -> clients.txtמה הן הרשאות הגישה של
partners.txt? מדוע?הרשאות הגישה הן
rwxrwxrwx, אך בפועל הקישור הסימבולי יורש את ההרשאות של הקובץ אליו הוא מצביע, במקרה זהclients.txt, שבו ההרשאות הןrw-r--r--.
Introduction
On Linux, everything is treated as a file. However, some files get a special treatment, either because
of the place they are stored, such as temporary files, or the way they interact with the filesystem,
such as links. In this lesson, we will learn where such files are located, how they work and how to
manage them.
Temporary Files
Temporary files are files used by programs to store data that is only needed for a short time. These
can be the data of running processes, crash logs, scratch files from an autosave, intermediary files
used during a file conversion, cache files and so on.
Location of temporary files
Version 3.0 of the Filesystem Hierarchy Standard (FHS) defines standard locations for temporary files
on Linux systems. Each location has a different purpose and behavior, and it is recommended that
developers follow the conventions set by the FHS when writing temporary data to disk.
Linux Essentials (Version 1.6) | 5.4 Special Directories and Files
Version: 2022-04-29 | Licensed for Cyber School. | learning.lpi.org | 417
/tmp
According to the FHS, programs should not assume that files written here will be preserved
between invocations of a program. The recommendation is that this directory be cleared (all files
erased) during system boot-up, although this is not mandatory.
/var/tmp
Another location for temporary files, but this one should not be cleared during the system bootup, i.e. files stored here will usually persist between reboots.
/run
This directory contains run-time variable data used by running processes, such as process
identifier files (.pid). Programs that need more than one run-time file may create subdirectories
here. This location must be cleared during system boot-up. The purpose of this directory was once
served by /var/run, and on some systems /var/run may be a symbolic link to /run.
Note that there is nothing which prevents a program to create temporary files elsewhere on the
system, but it is good practice to respect the conventions set by the FHS.
Permissions on temporary files
Having system-wide temporary directories on a multiuser system presents some challenges
regarding access permissions. At first thought one may think that such directories would be “worldwritable”, i.e. any user could write or delete data in it. But if this were to be true, how could we
prevent a user from erasing or modifying files created by another?
The solution is a special permission called the sticky bit, which applies both to directories and files.
However, for security reasons, the Linux kernel ignores the sticky bit when it is applied to files.
When this special bit is set for a directory, it prevents users from removing or renaming a file within
that directory unless they own the file.
Directories with the sticky bit set show a t replacing the x on the permission for others in the output
of ls -l. For example, let’s check the permissions for the /tmp and /var/tmp directories:
$ ls -ldh /tmp/ /var/tmp/
drwxrwxrwt 25 root root 4,0K Jun 7 18:52 /tmp/
drwxrwxrwt 16 root root 4,0K Jun 7 09:15 /var/tmp/
As you can see by the t replacing the x on the permission for others, both directories have the sticky
bit set.
Linux Essentials (Version 1.6) | Topic 5: Security and File Permissions
418 | learning.lpi.org | Licensed for Cyber School. | Version: 2022-04-29
To set the sticky bit on a directory using chmod in numeric mode, use the four-digit notation and 1 as
the first digit. For example:
$ chmod 1755 temp
will set the sticky bit for the directory named temp and the permissions as rwxr-xr-t.
When using the symbolic mode, use the parameter t. So, +t to set the sticky bit, and -t to disable it.
Like so:
$ chmod +t temp
Understanding Links
We have already said that on Linux everything is treated as a file. But there is a special kind of file,
called a link, and there are two types of links on a Linux system:
Symbolic links
Also called soft links, they point to the path of another file. If you delete the file the link points to
(called target) the link will still exist, but it “stops working”, as it now points to “nothing”.
Hard links
Think of a hard link as a second name for the original file. They are not duplicates, but instead
are an additional entry in the file system pointing to the same place (inode) on the disk.
TIP
An inode is a data structure that stores attributes for an object (like a file or directory)
on a filesystem. Among those attributes are the filename, permissions, ownership and
on which blocks of the disk the data for the object is stored. Think of it as an entry on
an index, hence the name, which comes from “index node”.
Working with Hard Links
Creating Hard Links
The command to create a hard link on Linux is ln. The basic syntax is:
$ ln TARGET LINK_NAME
Linux Essentials (Version 1.6) | 5.4 Special Directories and Files
Version: 2022-04-29 | Licensed for Cyber School. | learning.lpi.org | 419
The TARGET must exist already (this is the file the link will point to), and if the target is not on the
current directory, or if you want to create the link elsewhere, you must specify the full path to it. For
example, the command
$ ln target.txt /home/carol/Documents/hardlink
will create a file named hardlink on the directory /home/carol/Documents/, linked to the file
target.txt on the current directory.
If you leave out the last parameter (LINK_NAME), a link with the same name as the target will be
created in the current directory.
Managing Hard Links
Hard links are entries in the filesystem which have different names but point to the same data on
disk. All such names are equal and can be used to refer to a file. If you change the contents of one of
the names, the contents of all other names pointing to that file change since all these names point to
the very same data. If you delete one of the names, the other names will still work.
This happens because when you “delete” a file the data is not actually erased from the disk. The
system simply deletes the entry on the filesystem table pointing to the inode corresponding to the
data on the disk. But if you have a second entry pointing to the same inode, you can still get to the
data. Think of it as two roads converging on the same point. Even if you block or redirect one of the
roads, you can still reach the destination using the other.
You can check this by using the -i parameter of ls. Consider the following contents of a directory:
$ ls -li
total 224
3806696 -r–r–r– 2 carol carol 111702 Jun 7 10:13 hardlink
3806696 -r–r–r– 2 carol carol 111702 Jun 7 10:13 target.txt
The number before the permissions is the inode number. See that both the file hardlink and the file
target.txt have the same number (3806696)? This is because one is a hard link of the other.
But which one is the original and which one is the link? You can’t really tell, as for all practical
purposes they are the same.
Note that every hard link pointing to a file increases the link count of the file. This is the number
right after the permissions on the output of ls -l. By default, every file has a link count of 1
Linux Essentials (Version 1.6) | Topic 5: Security and File Permissions
420 | learning.lpi.org | Licensed for Cyber School. | Version: 2022-04-29
(directories have a count of 2), and every hard link to it increases the count by one. So, that is the
reason for the link count of 2 on the files in the listing above.
In contrast to symbolic links, you can only create hard links to files, and both the link and target
must reside in the same file system.
Moving and Removing Hard Links
Since hard links are treated as regular files, they can be deleted with rm and renamed or moved
around the filesystem with mv. And since a hard link points to the same inode of the target, it can be
moved around freely, without fear of “breaking” the link.
Symbolic links
Creating Symbolic Links
The command used to create a symbolic link is also ln, but with the -s parameter added. Like so:
$ ln -s target.txt /home/carol/Documents/softlink
This will create a file named softlink in the directory /home/carol/Documents/, pointing to the file
target.txt in the current directory.
As with hard links, you can omit the link name to create a link with the same name as the target in
the current directory.
Managing Symbolic Links
Symbolic links point to another path in the filesystem. You can create soft links to files and
directories, even on different partitions. It is pretty easy to spot a symbolic link on the output of ls:
$ ls -lh
total 112K
-rw-r–r– 1 carol carol 110K Jun 7 10:13 target.txt
lrwxrwxrwx 1 carol carol 12 Jun 7 10:14 softlink -> target.txt
In the example above, the first character on the permissions for the file softlink is l, indicating a
symbolic link. Furthermore, just after the filename we see the name of the target the link points to,
the file target.txt.
Linux Essentials (Version 1.6) | 5.4 Special Directories and Files
Version: 2022-04-29 | Licensed for Cyber School. | learning.lpi.org | 421
Note that on file and directory listings, soft links themselves always show the permissions rwx for
the user, the group and others, but in practice the access permissions for them are the same as those
for the target.
Moving and Removing Symbolic Links
Like hard links, symbolic links can be removed using rm and moved around or renamed using mv.
However, special care should be taken when creating them, to avoid “breaking” the link if it is
moved from its original location.
When creating symbolic links you should be aware that unless a path is fully specified the location
of the target is interpreted as relative to the location of the link. This may create problems if the link,
or the file it points to, is moved.
This is easier to understand with an example. Say that we have a file named original.txt in the
current directory, and we want to create a symbolic link to it called softlink. We could use:
$ ln -s original.txt softlink
And apparently all would be well. Let’s check with ls:
$ ls -lh
total 112K
-r–r–r– 1 carol carol 110K Jun 7 10:13 original.txt
lrwxrwxrwx 1 carol carol 12 Jun 7 19:23 softlink -> original.txt
See how the link is constructed: softlink points to (→) original.txt. However, let’s see what
happens if we move the link to the parent directory and try to display its contents using the
command less:
$ mv softlink ../
$ less ../softlink
../softlink: No such file or directory
Since the path to original.txt was not specified, the system assumes that it is in the same directory
as the link. When this is no longer true, the link stops working.
The way to prevent this is to always specify the full path to the target when creating the link:
Linux Essentials (Version 1.6) | Topic 5: Security and File Permissions
422 | learning.lpi.org | Licensed for Cyber School. | Version: 2022-04-29
$ ln -s /home/carol/Documents/original.txt softlink
This way, no matter where you move the link it will still work, because it points to the absolute
location of the target. Check with ls:
$ ls -lh
total 112K
lrwxrwxrwx 1 carol carol 40 Jun 7 19:34 softlink ->
/home/carol/Documents/original.txt
Guided Exercises
1. Imagine a program needs to create a one-use temporary file that will never be needed again after
the program is closed. What would be the correct directory in which to to create this file?
2. Which is the temporary directory that must be cleared during the boot process?
3. What is the parameter for chmod in symbolic mode to enable the sticky bit on a directory?
4. Imagine there is a file named document.txt on the directory /home/carol/Documents. What is
the command to create a symbolic link to it named text.txt on the current directory?
5. Explain the difference between a hard link to a file and a copy of this file.
Linux Essentials (Version 1.6) | Topic 5: Security and File Permissions
424 | learning.lpi.org | Licensed for Cyber School. | Version: 2022-04-29
Explorational Exercises
1. Imagine that inside a directory you create a file called recipes.txt. Inside this directory, you
will also create a hard link to this file, called receitas.txt, and a symbolic (or soft) link to this
called rezepte.txt.
$ touch recipes.txt
$ ln recipes.txt receitas.txt
$ ln -s receitas.txt rezepte.txt
The contents of the directory should appear like so:
$ ls -lhi
total 160K
5388833 -rw-r–r– 4 carol carol 77K jun 17 17:25 receitas.txt
5388833 -rw-r–r– 4 carol carol 77K jun 17 17:25 recipes.txt
5388837 lrwxrwxrwx 1 carol carol 12 jun 24 10:12 rezepte.txt -> receitas.txt
Remember that, as a hard link, receitas.txt points to the same inode that recipes.txt. What
would happen to the soft link rezepte.txt if the name receitas.txt is deleted? Why?
2. Imagine you have a flash drive plugged into your system, and mounted on
/media/youruser/FlashA. You want to create in your home directory a link called
schematics.pdf, pointing to the file esquema.pdf in the root directory of the flash drive. So, you
type the command:
$ ln /media/youruser/FlashA/esquema.pdf ~/schematics.pdf
What would happen? Why?
3. Consider the following output of ls -lah:
Linux Essentials (Version 1.6) | 5.4 Special Directories and Files
Version: 2022-04-29 | Licensed for Cyber School. | learning.lpi.org | 425
$ ls -lah
total 3,1M
drwxr-xr-x 2 carol carol 4,0K jun 17 17:27 .
drwxr-xr-x 5 carol carol 4,0K jun 17 17:29 ..
-rw-rw-r– 1 carol carol 2,8M jun 17 15:45 compressed.zip
-rw-r–r– 4 carol carol 77K jun 17 17:25 document.txt
-rw-rw-r– 1 carol carol 216K jun 17 17:25 image.png
-rw-r–r– 4 carol carol 77K jun 17 17:25 text.txt
◦ How many links point to the file document.txt?
◦ Are they soft or hard links?
◦ Which parameter should you pass to ls to see which inode each file occupies?
4. Imagine you have in your ~/Documents directory a file named clients.txt containing some
client names, and a directory named somedir. Inside this there is a different file also named
clients.txt with different names. To replicate this structure, use the following commands.
$ cd ~/Documents
$ echo “John, Michael, Bob” > clients.txt
$ mkdir somedir
$ echo “Bill, Luke, Karl” > somedir/clients.txt
You then create a link inside somedir named partners.txt pointing to this file, with the
commands:
$ cd somedir/
$ ln -s clients.txt partners.txt
So, the directory structure is:
Linux Essentials (Version 1.6) | Topic 5: Security and File Permissions
426 | learning.lpi.org | Licensed for Cyber School. | Version: 2022-04-29
Documents
|– clients.txt
`– somedir
|– clients.txt
`– partners.txt -> clients.txt
Now, you move partners.txt from somedir to ~/Documents, and list its contents.
$ cd ~/Documents/
$ mv somedir/partners.txt .
$ less partners.txt
Will the link still work? If so, which file will have its contents listed? Why?
5. Consider the following files:
-rw-r–r– 1 carol carol 19 Jun 24 11:12 clients.txt
lrwxrwxrwx 1 carol carol 11 Jun 24 11:13 partners.txt -> clients.txt
What are the access permissions for partners.txt? Why?
Linux Essentials (Version 1.6) | 5.4 Special Directories and Files
Version: 2022-04-29 | Licensed for Cyber School. | learning.lpi.org | 427
Summary
In this lesson, you learned:
• Where temporary files are stored.
• What is the special permission applied to them.
• What links are.
• The difference between symbolic and hard links.
• How to create links.
• How to move, rename or remove them.
The following commands were discussed in this lesson:
• ln
• The -i parameter to ls
