Informatics1-2017/Practice2
(→Linux permissions) |
(→Linux permissions) |
||
2. sor: | 2. sor: | ||
* Permission groups | * Permission groups | ||
** '''s'''uper'''u'''ser (su), pl. system administrators | ** '''s'''uper'''u'''ser (su), pl. system administrators | ||
− | ** '''owner''', the owner of the given file, who created it, | + | ** '''owner''', the owner of the given file, who created it, e.g. <tt>kkovacs</tt> |
− | ** '''group''', a group of users that the ''owner'' is a part of, | + | ** '''group''', a group of users that the ''owner'' is a part of, e.g. <tt>student</tt> |
** '''others''', everyone else | ** '''others''', everyone else | ||
* Permission types | * Permission types | ||
33. sor: | 33. sor: | ||
| 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 | | 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 | ||
|- | |- | ||
− | | rowspan="2" | | + | | rowspan="2" | type || colspan="3" style="text-align: center;" | owner || colspan="3" style="text-align: center;" | group || colspan="3" style="text-align: center;" | others |
|- | |- | ||
| read || write || execute || read || write || execute || read || write || execute | | read || write || execute || read || write || execute || read || write || execute | ||
47. sor: | 47. sor: | ||
$ _ | $ _ | ||
</PRE> | </PRE> | ||
− | + | The owner of the directory <tt>public_html</tt> is: <tt>borbely</tt>, his group: <tt>student</tt>, the permission: <tt>drwxr-xr-x</tt>: | |
− | * | + | * type '''d''': directory |
− | * | + | * owner '''rwx''', borbely can do anything with the folder |
− | * | + | * other students '''r-x''', they can't write the file, but they can read or execute it |
− | * | + | * everyone else '''r-x''', they can do the same as the student group |
− | + | In a numeric form a 3 digit number represents the permissions. | |
− | + | The first number from the left represents the permissions of the owner, then the group then others. The numbers 4: read, 2: write, 1: execute. | |
− | + | The sum of these is the permission. | |
− | + | E.g. <tt>/home/student/borbely/public_html</tt> permissions: <tt>755</tt>: | |
− | * owner (<tt>borbely</tt>) <tt>7=4+2+1</tt>: | + | * owner (<tt>borbely</tt>) <tt>7=4+2+1</tt>: read, write, execute |
− | * group (<tt>student</tt>) <tt>5=4+1</tt>: | + | * group (<tt>student</tt>) <tt>5=4+1</tt>: read, execute |
− | * other <tt>5=4+1</tt>: | + | * other <tt>5=4+1</tt>: read, execute |
=== chmod === | === chmod === | ||
− | + | Permissions for a file or folder can be set with the <tt>chmod</tt> command. Some examples: | |
− | * <tt>chmod 700 ~/ | + | * <tt>chmod 700 ~/info_homework</tt>: my homework can only be seen by me (and superusers of course) |
− | * <tt>chmod 750 ~/important_work</tt>: | + | * <tt>chmod 750 ~/important_work</tt>: this is some important work, I don't want others to erase or rewrite it, but my group is allowed to look into it |
− | * <tt>chmod 754 -R ~/public_html</tt>: | + | * <tt>chmod 754 -R ~/public_html</tt>: the keyword '''-R''' makes the command recursive so that this permission applies to all files inside this directory |
− | === | + | === More details === |
* <tt>man chmod</tt> | * <tt>man chmod</tt> | ||
− | * [http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilesp.html | + | * [http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilesp.html description] |
== Bash == | == Bash == |
A lap 2017. szeptember 11., 05:53-kori változata
Tartalomjegyzék |
Linux permissions
- Permission groups
- superuser (su), pl. system administrators
- owner, the owner of the given file, who created it, e.g. kkovacs
- group, a group of users that the owner is a part of, e.g. student
- others, everyone else
- Permission types
- w: Write, can write or read the file
- r: Read, can read the file, or access the folder's contents
- x: eXecute, can execute the file (script or program), or access the files / folders inside the folder
- -: nothing
We can pick and choose from these types and groups, for example we can set the permissions for a file so that
- the owner can read, write and execute the file
- the owner's group can read and execute the file
- everybody else can't do anything with the file
Superusers can always do everything with all files.
We can use alphabetic and numerical codes for these permissions:
- u: user, owner
- g: group
- o: others
- a: everyone: user, group and others.
- read: r or 4
- write: w or 2
- execute: x or 1
- nothing: - or 0
The symbol for a differs from o for example it is possible to give execution permissions to others, but deny it to my group.
In the alphabetic denotion, the permissions are usually represented by a 10 character long string:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
type | owner | group | others | ||||||
read | write | execute | read | write | execute | read | write | execute |
$ ls -l ~ drwxr-xr-x 8 borbely student 4096 Aug 30 23:24 Desktop drwxr-xr-x 2 borbely student 4096 Mar 27 2012 Downloads drwxr-xr-x 2 borbely student 4096 Oct 20 2009 Drives drwx------ 2 borbely student 4096 Apr 20 10:42 mail drwxr-xr-x 7 borbely student 4096 Sep 6 13:01 public_html $ _
The owner of the directory public_html is: borbely, his group: student, the permission: drwxr-xr-x:
- type d: directory
- owner rwx, borbely can do anything with the folder
- other students r-x, they can't write the file, but they can read or execute it
- everyone else r-x, they can do the same as the student group
In a numeric form a 3 digit number represents the permissions. The first number from the left represents the permissions of the owner, then the group then others. The numbers 4: read, 2: write, 1: execute. The sum of these is the permission. E.g. /home/student/borbely/public_html permissions: 755:
- owner (borbely) 7=4+2+1: read, write, execute
- group (student) 5=4+1: read, execute
- other 5=4+1: read, execute
chmod
Permissions for a file or folder can be set with the chmod command. Some examples:
- chmod 700 ~/info_homework: my homework can only be seen by me (and superusers of course)
- chmod 750 ~/important_work: this is some important work, I don't want others to erase or rewrite it, but my group is allowed to look into it
- chmod 754 -R ~/public_html: the keyword -R makes the command recursive so that this permission applies to all files inside this directory
More details
- man chmod
- description
Bash
Néhány hasznos link:
Alapvető parancsok
- pwd: aktuális könyvtár kiíratása.
- cd: könyvtárváltás. Ha paraméter nélkül hívjuk meg, akkor a saját mappánkba visz. Pl cd /mnt, cd, cd ..
- ls: könyvtár tartalmának listázása. Pl: ls, ls -h, ls /dev, ls -h -l ~
- mkdir: könyvtár létrehozása. mkdir NewFolder
- cp: másolás. cp mit.txt hova.txt
- mv: mozgatás/átnevezés (az eredeti nem marad meg). mv mit.txt hova.txt
- rm: törlés, Pl: rm alma.txt, rekurzívan töröl minent a mappával együtt: rm -r NewFolder
- quota: A felhasznált/megmaradt helyet írja ki. Ha a kvótánk betelik, akkor nem kapjuk meg a nekünk címzett leveleket és a grafikus terminálra sem tudunk belépni; ilyenkor a parancssoros terminálra belépve tudjuk kitakarítani a home-unkat.
- df, du: lemezen szabad / felhasznált helyet írja ki. A -h kapcsolóval olvasható GB, MB, kB értékeket kapunk, de lehet fix byte-okban is kiírni. Pl: df -h ~
- mc: midnight commander fájlböngésző.
- segíts magadon
- info <parancs>
- man <parancs> az adott parancsról leírást ad. A leírásban navigálni a 'fel', illetve a 'le' billentyűkkel, továbbá a 'PageUp', 'PageDown'-nal lehet. Kereséshez '/' jel után kell beírni a kifejezést. Kilépés 'q'-val. man ls
- <parancs> --help
- help
- history: parancs történet lekérdezése.
- apropos: parancs/program nevek keresése. apropos math
- top: futó alkalmazások.
- kill, killall, xkill: folyamatok leállítása. Egy adott felhasználó összes folyamatának leállítása: killall -u username
- cat: fájl tartalmának kiíratása. Pl: cat .forward
- echo: kiírja az argumentumait, Pl: echo Hello World. Figyelem, beugratós: echo --help
- grep: szöveg mintázatok keresése állományokban. grep "tű" szénakazal.txt
- less: szöveges állományok megjelenítése, könnyebb olvashatóság nagy fájloknál, Pl less valami.txt
- head (-n ), tail (-n ): fájl elejének és végének a kiiratása. head -n 100 valami.txt
- sort (-g -k): szövegállomány rendezése.
- wc (-l): byte/karakterek/sorok számolás adott fájlra.
- ssh: távoli gépre való bejelentkezést teszi lehetővé. Linux-ról Windows-ról
- scp: távoli gépre/gépről való másolás. Linux-ról Windows-ról
- wget: file-ok letöltése internetről, HTTP, HTTPS és FTP használatával.
- exit: kilépés terminálból (vagy ctrl+d).
- sudo: programok futtatása superuserként. (Mat intézetben erre nem lesz lehetőségünk) Pl. sudo rm /home/someone_I_hate/important_file.txt
- halt, reboot: kikapcsol, újraindít (Mat intézetben erre sem lesz lehetőségünk)
Pipe, redirect
Minden program írhat a konzolra, vagy az stderr-re, ez az, amit a konzolon szoktunk látni. Azonban ezeket a képernyő helyett fájlba is írhatjuk.
$ ls -l ~
drwxr-xr-x 8 borbely student 4096 Aug 30 23:24 Desktop
drwxr-xr-x 2 borbely student 4096 Mar 27 2012 Downloads
drwxr-xr-x 2 borbely student 4096 Oct 20 2009 Drives
drwx------ 2 borbely student 4096 Apr 20 10:42 mail
drwxr-xr-x 7 borbely student 4096 Sep 6 13:01 public_html
$ ls -l ~ > folder.txt
$ cat folders.txt
drwxr-xr-x 8 borbely student 4096 Aug 30 23:24 Desktop
drwxr-xr-x 2 borbely student 4096 Mar 27 2012 Downloads
drwxr-xr-x 2 borbely student 4096 Oct 20 2009 Drives
drwx------ 2 borbely student 4096 Apr 20 10:42 mail
drwxr-xr-x 7 borbely student 4096 Sep 6 13:01 public_html
$ _
A > jellel egy parancs kimenetét egy fájlba irányítjuk át, majd a fájlt kiírathatjuk a cat-el. Ugyanakkor figyeljük meg a következőt:
$ ls -l /home/algebra/wettl/ > myfile.txt ls: cannot open directory /home/algebra/wettl/: Permission denied $ cat myfile.txt $ _
A myfile.txt üresen maradt, de a konzolon hibaüzenet jelent meg (nem volt jogunk a mappa listázására). Ez azért van, mert az utóbbi ls parancs nem a standard kimenetre, hanem a hiba-kimenetre, azaz stderr-re írt. Az stderr-t a 2> jelel irányíthatjuk át:
$ ls -l /home/algebra/wettl/ > myfile.txt 2> error.log $ cat myfile.txt $ cat error.log ls: cannot open directory /home/algebra/wettl/: Permission denied $ _
Így az egyik fájlba a standard kimenet (stdout), a másikban az stderr került bele.
Az egyes programok olvashatnak a standard bemenetükről: stdin. Az ú.n. nem-interaktív parancsok olyanok, hogy a parancs kiadása után a befejezéséig nem nyúlunk a billentyűzethez. Ezekkel csak az argumentumaikon keresztül kommunikálhatunk. Interaktív parancsok a futásuk közben olvasnak a standard bemenetükről: stdin
- nem-interaktív parancs: grep "needle" haystack.txt
- interaktív parancs: grep "needle"
Az előbbi esetben a grep egy megadott fájlban keresi a needle kifejezést. Az utóbbi esetben a beírt szövegben keresi ugyanazt.
$ grep "needle" I hate this why can't I find anything why can't I find a needle why can't I find a needle oh, a needle oh, a needle again $ _
A Ctrl+D billentyű-kombinációval megszakítjuk a futást. A needle-t tartalmazó sorok duplán vannak, mert azokat a grep kérésemre kiírta.
Próbáljuk ki így:
$ grep "needle" > needle.txt I hate this why can't I find anything why can't I find a needle oh, a needle again $ cat needle.txt why can't I find a needle oh, a needle $ _
Ezzel kiválasztottuk a bemenetről a needle-t tartalmazó sorokat.
Az egyik parancs kimenete lehet a másik bemenete is, ez a pipe, jele egy függőleges vonal: |.
Példa: írjuk ki a home mappánk azon elemeit, melyek nevükben az info szövegrészletet tartalmazzák.
$ ls -1 ~
Desktop
Downloads
Drives
info1
info_hf.txt
mail
myfile.txt
myfolder
needle.txt
public_html
regi_info
regi_info_zh.txt
$ ls -1 ~ | grep "info"
info1
info_hf.txt
regi_info
regi_info_zh.txt
$ _
Példa: sok elem van egy mappában és apránként végig akarjuk nézni:
$ ls /home/student/ | wc -l 821 $ ls /home/student/ | less
Egy fájlból kivehetjük a különböző sorokat (rendezés és duplikált sorok törlése):
$ cat nevek.txt | sort | uniq > nevek.sorted.txt
Ha egy fájl tartalmát akarjuk pipe-olni egy parancsba, azt így is megtehetjük (<):
$ cat nevek.txt | sort $ sort < nevek.txt
Ez kombinálható > -el.
$ sort < nevek.txt | uniq > nevek.sorted.txt
Feladatok
1)
- Töltsük le a A1nevsor.txt állományt a borbely felhasználó oldaláról
- wget
- Távolítsuk el a neveket az első oszlopból.
- cut
- Rendezzük a sorokat Neptun kód szerint (a fejléc kivételével).
- sort
- Nagybetűsítsük a Neptun kódokat.
- tr
- Írjuk ki az eredményt egy külön fájlba (segédfájlokat használhatunk útközben).
- cat, echo, paste, head, tail
Megoldás
wget math.bme.hu/~borbely/A1nevsor.txt tail -n+2 A1nevsor.txt | cut -f2,3 -d" " | sort | tr [:lower:] [:upper:] > sorted.txt
Windows
A DOS után a Windows-os parancssori lehetőségek nem nagyon fejlődtek. A parancssor közelében sincs a Linux-nak, sem használhatóságban, sem funkcióiban, de azért van valami.
- Parancssor indítása
- Start menü -> Parancssor
- Start menü -> Futtatás -> cmd
- Keresés -> cmd
- leírás
- használható:
help
,help <parancs>
- Windows 10-en új életre kelt a cmd.
- A Windows 10 egy éves évfordulója óta beépített a bash (béta verzió).
Használatos a PowerShell, ami nem mindegyik verzión beépített, de ingyenes és elterjedt.
Különbségek
Pár szembetűnő különbség
- a Windows case-insensitive, vagyis kis és nagybetű nem számít, ellenben a Linux case-sensitive.
- A könytár-, fájlnevek:
- C:\Windows\System32\
- /usr/bin/
- parancssori kapcsolók perrel versus kötőjellel:
- Windows: dir /b
- Unix: ls -l
- script
- Windows-on: .bat
- Linux-on: .sh
- pipe, redirect hasonló
Gyakran emulálják/helyettesítik a unix-os lehetőségeket windows-on: