1、,Linux 基础Copyright Sispark Corporation,2009.All,rightreserved.,风云教育 地址:苏州工业园区金鸡湖大道1355号国际科技园科技广场4 楼 邮编:215021 电话:(86-512) 62620800 传真:(86-512) 62620030,Overviews,基本认知,登录登出系统,1,2,Input devices,Output devices,RAM,CPU,I/O,Hard disk,Hardware,应用软件在系统图中的层次,应用的分布,Linux 操作系统,内核 外壳(Shell) 文件系统,Hardware,Kerne
2、l,Shell,CPU,管理设备,内存,应用程序调度等 控制系统呼叫和底层设备的逻辑和接口 管理交换分区,服务进程( daemons ),文件系统,其他功能,内核,Shell是用户界面,用户和系统交互的特殊应用程序Five shells are available in the Linux environment: Bourne shell (sh) Korn shell (ksh) C shell (csh) Bourne-again shell (bash) tcsh,Shell,Written by Stephen Bourne for AT&T Unix The earliest an
3、d best-known Unix shell Common denominator of most shell scripting Bourne-Again Shell (bash)# root $ user,Bourne Shell (sh),Created as part of BSD Unix by Bill Joy, the author of vi, at the University of California at Berkeley Introduced command-line history and a programming environment similar to
4、the C programming language Not compatible with Bourne shell syntax tcsh, an enhanced but totally compatible csh# root % user,C Shell (csh),ksh Invoking Korn Shellsh Invoking Bourne Shellcsh Invoking C Shellbash Invoking Bourne-again Shell,Shell Switches,A hierarchy of directories, subdirectories, an
5、d files grouped together for a specific purpose. Directory A location for other files and directories. The directory structure enables you to create files and directories accessed through a hierarchy of directories. Subdirectory Any directory below another directory. For example, some of the subdire
6、ctories under the / directory are /usr, /etc, and /kernel.,The File System,A pathname uniquely identifies a particular file or directory by specifying its location A slash (/) between each directory name in the path to indicate different directories A slash in the first position represents the / dir
7、ectoryAbsolute Pathname Specifies a file or directory in relation to the / directory Begin with a slash Relative Pathname Specifies a file in relation to the current working directory Do not begin with a slash,Pathnames,The /(root) file system,On /dev/dsk/c0t0d0s0,/,opt,etc,dev,usr,kernel,var,export
8、,default,dsk,adm,home,bin,share,lib,On /dev/dsk/c0t0d0s6,The /usr file system,Application 1,Application 2,user1,user2,user3,On /dev/dsk/c0t0d0s5,The /opt file system,The /export/home file system,On /dev/dsk/c0t0d0s7,Linux Computing environment,Log in and out of a Linux system,2,1,Login telnet 192.16
9、8.0.23 login: (username) password: (users password)Logout exit or logout Ctrl+d,Must be six to eight characters in length Should contain at least two alphabetic characters and one numeric or special character Must differ from the login ID Must differ from the previous password by at least three char
10、acters,Password Requirements,Changing passwords frequently helps avoid unauthorized accessType the passwd command Type your current password and press Return Type your new password and press Return Retype the new password and press Return,Changing Your Password,/etc/passwdroot:x:0:1:Super User:/:/bi
11、n/sh /etc/shadowroot:daGaerRdafGEg: User name vs. UID Usershome directory Userslogin shell,User Accounts,Unix commands,3,5,Basic Unix commands,Metacharacters,Text viewer or editor,1,4,2,Find things on UNIX,Network Basics,$ Command option(s) argumentsA space must be used as a delimiter between each p
12、art of the command line Up to 256 characters can be entered on a single command line Different from DOS Case sensitive Different command prompt: /,Command-Line Syntax,Clear the terminal screenclear,clear,Display the absolute pathname of the current directory (print working directory)pwd,pwd,Displays
13、 the user name corresponding to the effective user IDid option(s)idid rootid a root,id,Change Directories Accept both absolute and relative pathnames Current directory . Parent directory cd directory_namecd /users/sd0801/s080101cd /sd080102cd s080201cdcd /,cd,Displaying a Listing of Files and Direct
14、ories ls a Displaying hidden filesHidden file: a file started with a “.” ls R Displaying a recursive listing ls t Sorts by time stamp instead of by name ls l Displaying a long list- rwx r-x r-x 4 user nobody 1024 Feb 22 09:20 a.txt文 属 属 其 硬 属 属 大 最后一次 文件 链件 它 接 小 修改时间 名类 主 组 人 数 主 组 型 权 权 权 限 限 限,ls
15、,d directory - normal file file filename(s) Text, including ASCII text, English text, commands text and executable scripts Executable or Binary, including 32-bit executable and extensible linking format (ELF) code files and other dynamically linked executables Data, those created by an application r
16、unning on the system ls F Displaying file types目录文件 / 可执行文件 * 文本文件 (none),File Type,r -读权限,对目录来说,是否可以查看该目录中的内容 w -写权限,对目录来说,是否在该目录中创建或者删除文件和子目录 x -执行权限,对目录来说,可进入该目录 - -没有权限 注意:对于目录文件的读写权限必须和执行权限一起使用。 如 - rw- r- r属 属 其它主 组 人 其中 属主-生成文件时登录的人,权限最高, u表示属组-系统管理员分配的同组的一个或几个人, g表示其它人-除属主,属组外的人, o表示所有人-包括属主
17、、属组及其它人, a表示,File Permission,r -读权限,对文件来说,是否可以查看或者打开文件 w -写权限,对文件来说,是否可以修改该文件 x -执行权限,对于文件来说是否可以执行 - -没有权限 注意:对于普通文件的写权限和执行权限必须和读权限一起使用。 如 - rw- r- r属 属 其它主 组 人 其中:属主-生成文件时登录的人,权限最高, u表示属组-系统管理员分配的同组的一个或几个人, g表示其它人-除属主,属组外的人, o表示所有人-包括属主、属组及其它人, a表示,File Permission,Change permissions Symbolic mode u
18、ses combinations of letters and symbolschmod g-r file1chmod u+x,go+r file1chmod a=rw file1,chmod,chmod mode filename,who cp permission(s),r Read w Write x Execute,= Set permissions - Remove access + Give access,u User or owner of the file g Group to which the file belongs o Others outside owner and
19、group a All of the above,Octal mode uses octal numberschmod 555 dantechmod 644 dantechmod 755 dante,Octal Value Permissions,4 Read,2 Write,1 Execute,Octal Value Permissions,7 r w x,6 r w -,5 r - x,4 r - -,3 - w x,2 - w -,1 - - x,0 - - -,Create new, empty files or update access and modification times
20、touch filename(s),touch,Create directories using either an absolute or a relative pathname Must have the appropriate permissions to create a directorymkdir -p directory_namemkdir dir1/dir2mkdir dir3 dir4 dir5mkdir /gamesmkdir -p dir6/dir7/dir8,mkdir,Copy filescp -i source_file destination_filecp -i
21、source_file(s) destination_directorycp beans applecp beans apple dir2cp i beans appleCopy a directory and its contents to another directorycp -r source_directory(s) destination_directorycp -r dir3 dir4cp -r dir3 dir4 /tmp,cp,Move or rename files and directoriesmv -i source target_filemv -i source ta
22、rget_directory 对于目录:如果目标目录不存在,相当于为源目录重命名;如果目标目录存在,相当于剪切操作; mv nuts beansmv /export/home/jerry/brands mv dir5 dir6,mv,Remove filesrm -i filename(s)rm file1 file2 file3rm -i file1chmod 444 file2; rm f file2Removing Directoriesrmdir directory_name(s)rm -ri directory_name(s)rmdir empty_dirrm r dir6rm ir
23、 /dir7,rm & rmdir,Create alternate names or aliases for files and directories Two kinds of links, hard and symbolic (or soft)ln -s source_file target_file,ln,Symbolic links link a file or directory across file systems,Hard links Used to link files on the same file system Files that are hard linked s
24、hare the same inode number and refer to the same data on the hard disk,The UNIX Manpages describes information about the systems on-line commands, system calls, file formats, and system maintenance man name man k keywordScrolling in ManpagesSpacebar Scroll through a manual page one screen at a time
25、Return Scroll through a manual page one line at a time b Move back one screen f Move forward one screen q Quit the man command /string Search forward for information n Find the next occurrence of string,Man - man is your friend,3,5,Basic Unix commands,Metacharacters,Text viewer or editor,2,4,1,Find
26、things on UNIX,Network Basics,Represents zero or more charactersls d*,Asterisk (*),Represents any single characterls d?ls z?.txt,Question Mark (?),Match a set or range of characters to a single character positionls b-fb*ls fFf*a?c,Square Brackets ( ),Enter multiple commands on a command linecd;lsdat
27、e;cal;pwd,Semicolon (;),Use () to redirect the output of a command to a file rather than to the screen Use () to append the output of a command to an existing filecal 1 2008 a.txtcal 1 2008 /dev/nullcal 1 2008 a.txt cat f2.txt,Angle Brackets (, ),设备文件: /dev/stdin : 标准输入设备 /dev/stdout : 标准输出设备 /dev/n
28、ull : 空设备,Takes the output of one command and passes it as input into a following commandls -l /etc | morecat /etc/passwd | more ls l /etc | tee etc.lstls l /etc | tee a etc.lst,Piping (|),1,5,Basic Unix commands,Metacharacters,Text viewer or editor,3,4,2,Find things on UNIX,Network Basics,Configura
29、tion files, web pages, program source code are all text-based Text viewers v.s. editors,Text Editing,Displays the contents of a text file on screen Is often used to join two or more files into one large filecat filename(s)cat /dev/null /b.txtcat /etc/passwd | awk -F: print $1 “t“ $6 | sort /userinfo
30、功能:将/etc/passwd中每一个用户的用户名和主目录保存到userinfo文件中。,cat,Displays the contents of a text file one screen at a timemore filename(s) Spacebar Scroll to the next screen Return Scroll one line at a time b Move back one screen f Move forward one screen h Display a Help menu of more features q Quit and return to
31、the shell prompt /string Search forward for string n Find next occurrence of string,more,Displays the first or last n lines of one or more files Displays first or last 10 lines by defaulthead -n filename(s)tail -n filename(s)tail -30 /usr/dict/wordstail -20 cal cal.lst,head & tail,vi filename Open o
32、r create a filevi Open a new file to be named later,Invoking vi,Command mode,Last-linemode,Entry mode,Escape,Return,: / ?,i a o,vi Full Screen Text Editor,向上 k 向下 j向左 h 向右 l行首 行尾 $文件尾 G 文件首 1G第N行 nG屏幕头 H 屏幕中 M屏幕底 L下翻 Ctrl+f 上翻 Ctrl+b,Positioning,i 在光标当前位置插入a 在光标所在位置后插入o 在光标所在行下插入一空行O 在光标所在行上插入一空行退出输
33、入状态,进入控制状态。,Input,x 删除一个字符dw 删除当前词3dw 删除三个词dd 删除当前行5dd 删除五行:5,10d,Deleting Text,r 替换一个字符cw 替换一个单词cc 替换一行C 替换从光标至行尾,Changing Text,yw 拷贝词 yy 拷贝行(或Y) dw 剪切词 dd 剪切行 p 当前行下粘贴 :1,2co3 拷贝行1,行2在行3之后 :4,5m6 移动行4,行5在行6之后,Copying and Pasting Text,:w 存盘:w newfile 存成新文件:wq 存盘退出Vi(或ZZ,或:x):q! 强行退出不存盘,Saving and Q
34、uitting Files, 改变大小写 J 把当前行和下一行连起来 u 废除刚才的编辑操作(undo) :set nu 显示行代码 (:set nonu) :21 光标停在指定行 21G 光标停在指定行 (G 到文件尾,1G 到文件头) /串 从当前行往下查找 ?串 从当前行往上查找 n 查找继续 :r file2 在光标所在位置插入另一个文件 :1,$s/旧串/新串/g 替换全文(或 :%s/旧串/新串/g),Advanced Editing Options,Systems Testing Technicians: Systems testing technicians execute t
35、est plans to evaluate the performance of software applications and information and telecommunications systems. They are employed in information technology units throughout the private and public sectors.Example Titles: application tester application testing technician software test coordinator softw
36、are tester systems tester systems testing technician,Exercises:,1,5,Basic Unix commands,Metacharacters,Text viewer or editor,4,3,2,Find things on UNIX,Network Basics,Display a compact list of users logged inusers,users,Displays information about all users currently logged on to the local systemwhoTh
37、e who am i command displays information about your real user IDwho am iThe whoami command displays only the login name of the effective userwhoami,Finding People,Find files based on specific criteria, then execute a command on the matching filesfind path expression actionfind / -name perl find . -mt
38、ime 10 print find /etc -user 0 size +400 printfind -perm 777 /holes find /export/home -type f -atime +365 -exec rm ;,find,Searches a file for a specified text string and prints all lines that contain that pattern to the screen case sensitivegrep option(s) string filename -i Ignore case of string whe
39、n searching -v Search for all lines that do not match stringgrep root /etc/passwd cat /etc/passwd | grep rootls -la | grep -i sep 1ls file*|xargs grep dd,grep,Displays a line, word, or character count of a filewc options filename(s) -l Counts lines -w Counts words -C Counts charactersgrep wang /etc/
40、passwd | wc -l,wc,Summarize disk usagedu sk *,du,list the processes currently running on the system/usr/bin/ps -options-e List every process now running-f Generates a full listing-U uidlist Processes whose real UID or login names are givenps ef | grep telnet,ps,Terminate unwanted command processes t
41、hat you cannot quit in the normal waykill -signal process-idkill 12932kill -9 12418sleep 1001 &pkill sleep,kill,Ability to execute several jobs simultaneouslyCommand Value jobs Display which jobs are currently runningfg %n Place a job in the foreground bg %n Place a job in the background stop %n Sus
42、pend a background job kill %n Abort the specified background job Control-c Abort the foreground job Control-z Suspend the foreground job.,Job Control,1,4,Basic Unix commands,Metacharacters,Text viewer or editor,5,3,2,Find things on UNIX,Network Basics,IP address Netmask Default router Domain resolve
43、,Important Concepts,ping s host2 ifconfig a netstat rn,Important Utilities,telnet The telnet server simulates a terminal to allow you to connect to and work on a remote system uses TCP/IP to connect to another systemtelnet hostname portrlogin Establish a remote login session on another workstationrl
44、ogin hostname,telnet and rlogin,Servers with sites set up for downloading files sometimes provide an anonymous ftp accountftp 192.168.0.23Name: (username)Password: (users password)Commands: cd, lcd dir bye bin, asc get, put, mget, mput hash prompt,ftp,UNIX Shell,2,Set up initialization files,Shell S
45、cripting,1,The SunOS 5.x system software provides several default environment variables whose values can be customized Temporarily change the environment variables at the command line, affecting only the current shell Permanently changes it by modifying the initialization files,Environment Variables
46、,setenv environment variable value(全局 变量)%setenv VAR1 abc%echo $VAR1%csh%echo $VAR1set local variable=value(局部变量)%set var2=abc%echo $var2%csh%echo $var2,C Shell,Initialization files contain commands and variable settings that are executed when a shell is started Two levels: System-wide Reside in the
47、 /etc directory User-specific Reside in a users home directory,Shell System-Wide User-Specific(Read First) (Read Second)C /etc/.login $HOME/.login$HOME/.cshrc$HOME/.logout,Features of C Shell Initialization Files,Initialization files contain commands and variable settings that are executed when a sh
48、ell is started Two levels: System-wide Reside in the /etc directory User-specific Reside in a users home directory,Shell System-Wide User-Specific(Read First) (Read Second)Bash /etc/profile $HOME/.bash_profile$HOME/.bashrc$HOME/.bash_logout,Features of B Shell Initialization Files,%cd %vi .cshrc %source .cshrc,C Shell Initialization Files,