660 likes | 841 Views
Introduction to Linux. References. “ UNIX&Linux Shell 設計程式篇” 第一章 請參考 pages 18 - 32 “ Practical UNIX Programming - A Guide to Concurrency, Communication, and Multithreading” : Chapter 3 Files 1- 網路管理講義 -v1.pdf h ttp://linux.vbird.org 鳥哥的 Linux 私房菜. Online.
E N D
References • “UNIX&Linux Shell 設計程式篇” 第一章 • 請參考 pages 18 - 32 • “Practical UNIX Programming - A Guide to Concurrency, Communication, and Multithreading”:Chapter 3 Files • 1-網路管理講義-v1.pdf • http://linux.vbird.org 鳥哥的 Linux 私房菜
Online • Introduction of Linux System • Simple Commands • File System • Original Files • Some Important Shell Commands
Operating Systems • OS resembles an organization with several managers at the top level. • Memory manager • Process manager • Device manager • File manager • Interface or Shell
UNIX • UNIX is a multiuser, multiprocessing, portable operating system. • It is designed to facilitate programming, text processing and communication.
UNIX History • UNIX was originally developed in 1969 by Thompson and Ritchie of the Computer Science Research Group at Bell Laboratories. • UNIX has gone through many versions since then. It has been a popular operating system among computer programmers and computer scientists.
Linux History (1/2) • In 1991, Linus Torvalds, a Finnish student at the University of Helsinki, developed a new operating system “Linux”. • The initial kernel, which was similar to a small subset of UNIX, has grown into a full-scale operating system today. • After 2.0 version, Linux has all features traditionally attributed to UNIX. • Unix-like computer operating systems • Use GNU free software
Linux History (2/2) • All the underlying source code can be used, freely modified, and redistributed, both commercially and non-commercially, by anyone under licenses such as the GNU General Public License (GPL). • Low cost, ease of modification • Linux- Wikipedia • http://en.wikipedia.org/wiki/Linux
Linux Distribution • Typically Linux is packaged in a format known as a Linux distribution. • Ex: Debian, Ubnutu, Fedora, openSUSE • Linux distributions usually include the Linux kernel, utilities and libraries.
Use of Linux • Desktop • Servers, mainframes • LAMP for Web servers • About 91.7% on top 500 supercomputers • Embedded systems • Smartphone, cell phones, PDA • Android is based on Linux (25.5% at 2010 Q3) • TiVo digital video recorder • Tone generator, digital piano • Firewalls, routers Linux, Apache, MySQL, Perl/PHP/Python
Shell • Shell is a program to setup your working environment. • Shell receives and interprets the commands entered by the user. • To do anything in the system, we must give the shell a command. • There are many kinds of shells. • Ex: bash User space applications shell OS Kernel system software Firmwave Hardware
Some Commands for Management • System (for all user) • useradd, passwd, userdel, shutdown, reboot, dmesg, who, whoami, finger, top, ps • Memory • free • File system • fdisk, df, du • Network • ifconfig, route, ping, traceroute, netstat, host
Simple Commands (1/3) • passwd -- change password • exit -- exit • man command -- list the menu of the command “command” • keyin q to quit • ls -- list the files in the directory • ls –a (list all files) • ls –l (list detail information)
Simple Commands (2/3) • cd directory -- change directory • cd .. ( to parent) • cd . ( to itself) • cd subdirectory • mkdir directory -- make directory • rmdir directory -- remove directory • cat filename • cp file1 file2 -- copy the content of file1 to file2
Simple Commands (3/3) • rm file1 -- remove a file with name “file1” • rm –i (will ask again) • top ---- monitor the usage of CPU/memory • ps ---- monitor the active processes in system • ps -aux | grep sshd • echo -- show the sentence again in screen • echo “ABC” > test1 • su -- to login in to another account.
vi -- On-line Editor Shell • vi is an on-line editor. • Vim is OK. • Two modes are used: • Command mode • Edit mode • i : insert, a: append • Then you are in editor mode. • Use “Esc” to return to command mode. Command mode a / i ESC Edit mode
vi command mode (1/2) • In editor mode, you can insert any word and Enter at the position of the cursor. • In command mode, you can use • :q to leave the vi • :w to leave with saving the file • :q! to leave without saving the file
vi editor mode • x to delete the character on the cursor. • dd to delete the line on the cursor. • :1 go to the line 1 • /abc to search “abc” • :se nu to see number of each line • You can use man vito get more information
joe editor • Joe is another on-line editor. • It has an online help. • Ctrl-KX : save file • Ctrl-Shift-d: delete a charater
An Example of Files • ls -la • -rw-r----- 1 wrlai 980 Mar 23 12:00 .cshrc • -rw------ 1 wrlai 2302 Sep 10 16:32 .login • -rw----r-- 1 wrlai 2354 Feb 22 10:35 .mailrc • --x--x--- 1 wrlai 3302 Jan 14 16:32 makefile • -rw-rw--- 1 wrlai 3302 Jan 14 16:32 myfile • -rw-r--r-- 1 wrlai 3880 Feb 12 10:00 abc • drwxr-xr-x 1 wrlai 880 Feb 12 10:00 test • drwxr-xr-x 1 wrlai 880 Feb 12 10:00 project
File System • Files • Ordinary files (-) • Directory files(d) • Special Files:socket(s)、names pipe(p)、symbolic link(l)、character device(terminal)(c)、block device(disk)(b) • Hierarchical File System • Tree-like structure • Each node presents a file
The Tree Structure wrlai abc myfile .cshrc .mailrc project Cprogram proj1 proj2 .login t1.cc t2.cc h1* a.out file1.cc
Files • All are files! • Original files • Directories • I/O devices: monitor, disk • /dev/hda the 1st IDE disk • /dev/hdb the 2nd IDE disk • /dev/hda2 the 1st partition on the 1st IDE disk • /dev/sda the 1st SATA disk • /dev/sda2 the 1st partition on the 2st SATA disk
Current/ Home Directory The directory you are working in, is called workingdirectory or currentdirectory. </home/staff/wrlai>cd Cprogram </home/staff/wrlai/Cprogram> When you are first log in, the working directory is your home directory. Use 〜 to present the home directory </usr/bin>cd 〜 </home/staff/wrlai>
pwd • pwd -- print working directory </home1/staff/wrlai/Cprogram>cd /(到目錄 /) </>pwd(顯示目前所在的目錄位置) / </>
Linux Hierarchy / bin boot etc sysconfig rc.d skel … proc dev inet rd … home staff student … sbin tmp usr lib sbin src … bin doc var lock spool … cache
Files • Every file has a filename. • No two files in the same directory can have the same name. • A full (absolute) pathname is its filename and the complete path. Ex: /usr/prof/alex/project/assign1.c • A relative file name is its filename and path from current directory. Ex: alex/project/assign1.c in </usr/prof>
Unix File Representation • A Unix file has a description which is stored in a structure called an inode. • Each file is segmented and located in a tree-structured representation. • Features of inode: • Fairly efficient for small files • Flexible if the size of the file changes
inode file information: size (in bytes) owner UID and GID relevant times (3) link and block counts permissions the time of creation the time of last access the time of last modification ... file block direct pointers to beginning file blocks ... ... ... single indirect pointers ... ... ... double indirect pointers ... ... ... triple indirect pointers ... ... ... ... ... ... point block
inode – Small File file information: size (in bytes) owner UID and GID relevant times (3) link and block counts permissions the time of creation the time of last access the time of last modification 1 direct pointers 1, 2, 3 2 3 single indirect pointers double indirect pointers triple indirect pointers
inode – Larger File file information: size (in bytes) owner UID and GID relevant times (3) link and block counts permissions the time of creation the time of last access the time of last modification 1 file block ... 13 direct pointers 1, 2, …, 12 12 ... 14 ... ... single indirect pointers ... point block double indirect pointers ... triple indirect pointers
File Information • ls -la -rw-rw---- 1 wrlai cs 3302 Jan 14 16:32 myfile • The name of file • The time and date the file was • created or last modified • The size of the file in bytes • The name of group • The name of the owner • The number of hard links • Access Permissions ( 9 bits )
Access Permissions(1/3) • Type of file (one bit) : d : directory - : ordinary file s : socket p : names pipe (FIFO) l : symbolic link c : character device (terminal) b : block device (disk)
Access Permissions(2/3) • Three types of users can access a file(Owner/Group/Other) • A user can attempt to access a file in three ways(Read/Write/Execute) • Ordinary file • Read : you can read from file • Write : you can write to the file • Execute : you can execute the file • Directory • Read : you can read the directory • Write : you can create, move, copy or remove entries • Execute : you can search (i.e., enter) the directory
Access Permissions(3/3) - r w - r w - r - - Other’s privileges (r-- means readable but not writable or executable) Group’s privileges (rw- means readable and writable but not executable) Owner’s privileges (rw- means readable and writeable but not executable) File mode (- indicates an ordinary file)
Change Mode(1/4) • You can use chmod to change the access permissions of a file • Example : % ls -l myfile -rw-rw-rw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod g+x myfile -rw-rwxrw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod o-w myfile -rw-rwxr-- 1 wrlai cs 3302 May 20 12:20 myfile
Change Mode(2/4) WHO u User g Group o Other a All (ugo) Operator - Remove permission + Add permission = Set permission Permissions r Read w Write x Execute l Set locking privilege s Set user or group ID mode t Set save text (sticky bit) mode u User’s current permissions g Group’s current permission o Other’s current permissions
Change Mode(3/4) -r--r--r-- 1 wrlai cs 3302 May 20 12:20 myfile % chmod 666 myfile -rw-rw-rw- 1 wrlai cs 3302 May 20 12:20 myfile read write executeValue read write execute - - - 0 0 + 0 + 0 - - Yes 1 0 + 0 + 1 - Yes - 2 0 + 2 + 0 - Yes Yes 3 0 + 2 + 1 Yes - - 4 4 + 0 + 0 Yes - Yes 5 4 + 0 + 1 Yes Yes - 6 4 + 2 + 0 Yes Yes Yes 7 4 + 2 + 1
Change Mode(4/4) % chmod 666 myfile -rw-rw-rw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod 676 myfile -rw-rwxrw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod 674 myfile -rw-rwxr-- 1 wrlai cs 3302 May 20 12:20 myfile
16 bits Attribute(1/3) • Every file has 16 attribute bits. • File Type(12-15 bits) • 12:S_IFIC(Named pipe) • 13:S_IFCHR/S_IFBLK(Character or Block) • 14:S_IFDIR(Directory) • 15:S_IFREG(Original file) • Only one bit can be set in bits 12-15.
16 bits Attribute(2/3) • Access permissions(0-8 bits)rwxrwxrwx • Sticky bit 黏著位元(bit 9):S_ISVTX • 當此 bit=1,file 會一直留在記憶體中而不會被swap out。 • 對於 Directory,使用者執行位元為 t,表示只有超級使用者可刪除此目錄。 • Ex:drw-rw-rwt
16 bits Attribute(3/3) • Set Group ID(bit 10):S_ISGID • 當此 bit=1,表示對此file,Group 內的人均有和file 擁有者相同的執行權。 • Set User ID(bit 11):S_ISUID • 當此 bit=1,表示對此file,每個人均有和file 擁有者相同的執行權(bit 6 = ‘s’)。 • 執行此file 的使用者,EUID 會被改成和 file 擁有者相同的 UID 。 %-r-s--x--x 1 root 18852 Mar 7 2005 17:56 /usr/bin/passwd %-rw-r--r-- 1 root 2773 Sep 29 17:56 /etc/passwd %-r-------- 1 root 2773 Sep 29 17:36 /etc/shadow
UID、GID、EUID、EGID • 這四個 ID 用於決定某一使用者對於某一檔案的使用權。 • In /etc/passwd, every user has his • UID:real User ID • GID:Group ID(可以有很多個) • Login shell 時,EUID=UID、EGID=GID。 • EUID:Effective User ID • EGID:Effective Group ID
Example of UID & EUID • 某一使用者的 UID = ncliu,但可以利用 passwd 修改 /etc/passwd 的內容,如同擁有和root 相同的權限(EUID = root)。 被執行的指令 UID EUID SUID /bin/passwd ncliu root on
chown, chgrp • To change the owner and group associated with a file. % chown user1 myfile % chown user1.group1 myfile % chgrp group1 myfile % chgrp [-Rh] groupID files • R : Recursive operation • h : Change the ownership or group of the symbolic link file itself, not the file to which it refers