Linux
操作系统和Linux命令
·操作系统如何开始运行
·终端、控制台、外壳
·命令行
·文件系统-管理文件
· I/O重定向
·终端的高级使用- tmux
Operating System(操作系统)
Turning on the computer (or pressing the reset button) forces the CPU to begin executing instructions from a fixed location in the computer memory
• The contents of memory starting at this location need to be non-volatile, i.e. to survive a power down/power on – usually use ROM (Read Only Memory)
打开计算机(或按复位按钮)强制CPU开始执行来自计算机内存中的固定位置
·内存的内容从这个开始位置必须是非易失性的,即在断电/通电时仍能生存-通常使用ROM(只读存储器)。
The ROM that stores the program used to start the operating system is
called the boot ROM. On most PCs,the boot program is called the BIOS
(Basic Input/Output System)
• Don’t store the complete OS in the boot ROM
• ROM is slow and expensive compared to RAM
• ROM is hard to update
• OS needs frequent updates
存储程序的ROM用于启动操作系统的称为boot ROM。在大多数PC上,
boot程序称为BIOS(基本输入/输出系统)
·不要将完整的操作系统存储在boot只读存储器
· ROM速度慢,价格昂贵,比较RAM
· ROM很难更新
·操作系统需要频繁更新
So load the operating system in stages:
• The boot ROM contains a small program that is able to read a fixed-
size block of bytes from a fixed position on the disk (the bootloader)
• Note the boot program doesn’t need to know about the file system – it just has to be able to read a block of raw bytes from a known location
• The bootloader may have a digital signature to ensure that it hasn’t been tampered with
• Once the BIOS has loaded the bootloader into memory, it jumps to
its first instruction and starts executing code from there
所以把操作系统装载进去
阶段:
·bootROM包含一个小的程序可以从固定的磁盘上的位置(引导加载程序)读取一个固定的大小的字节块
·注意boot程序不需要了解文件系统-它只是能够从已知位置读取一个原始字节块
·引导加载程序可能具有数字签名,以确保它没有被篡改
·BIOS加载bootloader到内存中,它会跳转到它的第一条指令,从那里执行代码
So load the operating system in stages:
• The job of the bootloader is to load the OS kernel into memory
• The OS kernel is usually stored in the file system on disk, so the bootloader needs to know how to find a file in the file system
and read it.
• Once the kernel has been loaded, it can initialise its data structures and then start the first process (called init in Linux), which can start a login process so that the user can login and start work.
所以把操作系统装载进去阶段:
·bootloader的工作是加载操作系统内核进入内存
·OS内核通常存储在系统磁盘的文件中,因此引导加载程序需要知道如何在文件系统中查找文件和读取文件
·一旦内核被加载,它可以初始化其数据结构,然后启动第一个进程(在
Linux),它可以启动登录过程以便用户可以登录并开始工作
Terminal, Console, Shell
DEC VT220 terminal, popular in the 1980s
• A physical terminal used to communicate with a mini or mainframe computer over a serial communication link
• A console was the keyboard/monitor directly connected to the console port of the compute
DEC VT 220终端,流行于20世纪80年代
·用于通过串行通信链路与小型计算机或大型计算机通信的物理终端
·控制台是直接连接到计算机控制台端口的键盘/显示器
console:keyboard
Gnome terminal – a virtual terminal that emulates the behaviour of a physical terminal
• The shell is a program that receives command input from the terminal and makes calls to the computer operating system to execute the commands.
Gnome终端-一个虚拟终端,模拟物理终端的行为
·外壳是接收来自终端的命令输入并调用计算机操作系统以执行命令的程序。不是一个物理设备。
Command line – why bother?(命令行)
• Command line advantages
• Available everywhere – many servers and small embedded systems don’t have a GUI
• Uses less resources
• More efficient once you’ve learnt the commands – faster than clicking and scrolling, then clicking and scrolling, then clicking and scrolling some more just to do something simple
• Can easily compose commands to do complex tasks
• Can automate commands, go away and leave them running
·命令行优势
·无处不在-许多服务器和小型嵌入式系统没有GUI
·使用更少的资源
·一旦你学会了命令,更有效率-比点击和滚动更快,然后点击和滚动,然后点击和滚动更多只是为了做一些简单的事情
·可以轻松编写命令来执行复杂任务
·可以自动化命令,离开并让它们运行
GUIs are also good
• Browse the web using GUI
• Read email using GUI (although there are good terminal mail readers)
• Read pdf documents
• View and edit photos
• Use graphical design tools, stream video, .
GUI也不错
·使用GUI浏览网页
·使用GUI阅读电子邮件(尽管有很好的终端邮件阅读器)
·阅读PDF文档
·查看和编辑照片
·使用图形设计工具,流视频,. …
Terminal for command line(命令行终端)
• Linux
• Native
• Mac
• Native
• Windows
• Native: MS-DOS commands
• Bash on Windows Subsystem for Linux (WSL)
• gitbash
File system tree
When we first login, we are positioned in the file system
directory tree at our home directory, e.g. /home/bill
当我们第一次登录时,我们被定位在文件系统中目录树位于我们的主目录,例如/home/bill
Wherever we are in the tree is called the current working directory
我们在树中的任何位置都被称为当前工作目录
Every directory has a parent directory (except the top-level directory, /, called the root directory). The parent of a directory is the one directly above it in the tree, e.g. /home is the parent of /home/bill
每个目录都有一个父目录(除了顶级目录/,称为根目录)。一个目录的父目录是树中直接位于它上面的目录,例如/home是/home/bill的父目录
• pwd where am I? (print working directory)
• cd go somewhere else (change directory)
• ls what’s in here? (list contents of directory)
A path name is a description of the location of a directory in the file
system tree
• We can use relative path names, which start at the current working
directory, e.g. ls books
• or absolute path names, which start at the root of the file system, e.g. ls/home/bill/books
路径名是对文件中目录位置的描述系统树
·我们可以使用相对路径名,从当前工作路径开始。
目录,例如ls books
·或绝对路径名,从文件系统的根目录开始,例如ls/home/bill/books
Useful commands for working with files
ls lists contents of directory
• less display contents of text file
• file indicate file type
• cp copy files and directories
• mv move/rename files and directories
• rm remove files and directories
• Much more…
• Practice in practical sessions
Command format: command -option arguments
• e.g. ls -l /home/bill
• man look up the manual entry for a command
• e.g. man ls
Wildcards(通配符)
Using wildcard characters can make the use of the file commands
even more powerful
I/O Redirection – Output
Most the commands that we’ve used generate some output
• Sometimes the output is the data that we were after, sometimes it’s
an error message or status information
• All output is sent to a file: good data is usually sent to the standard
output file, error messages etc. are sent to the standard error file
Usually the standard output file and the standard error file are both
mapped to our display, so we see the output appearing on the screen
• We can choose to redirect the standard output to a different file,
• e.g. ls -l /home/bill/books > ls_output.txt
• We can also redirect standard error,
• e.g. ls -l /bill/home/books 2> ls_errors.txt
I/O Redirection – Input
Just as the standard output and standard error files are usually
mapped to the display, the standard input file is usually mapped to
the keyboard
• cat is a program that normally reads data from the standard input
(keyboard) and sends the data to the standard output (display),
e.g.
$ cat
Much have I travelled in the realms of gold
Much have I travelled in the realms of gold
We can redirect the output as usual, e.g.
$ cat > gold.txt
Much have I travelled in the realms of gold
Advanced use of the terminal: tmux
tmux – terminal multiplexer
• NOTE: tmux may not work very well when using Putty, a windows SSH connection tool.
create multiple panes within a window
• Makes it more convenient to use a terminal to manage a remote serve
There’s a nice tmux blog post
(http://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/)
• And a tmux home page
(https://github.com/tmux/tmux/wiki).
tmux: screen multiplexer
Installation ( you need to have the permission to use sudo, privileged
access to system resource)
• $ sudo apt-get update
• $ sudo apt-get install tmux
Common commands
• Prefix : Ctrl + b
• To split a pane horizontally: ctrl+b+ ”
• To split a pane vertically: ctrl+b+%
• To move from pane to pane: ctrl+b+[arrow key]
• To kill current pane: ctrl+b+x
We’ve just scratched the surface of the capabilities of the command
line
• The key steps to making progress are to read more and to play
around with a Linux system as much as possible
Secure shell (SSH)
• Secure shell (SSH) is used to connect to command line sessions on
remote systems using a secure (encrypted) connection.
• Tools:
• Windows: Putty (used in our lab)
• Linux and Mac OS: command in terminal
• Some quick knowledge about cryptography before instruction on using SSH …
ciphertext, encrypted with key K
密文,用密钥K加密
symmetric key crypto: Bob and Alice share same (symmetric) key: K
• e.g., key is knowing substitution pattern in mono alphabetic substitution cipher
对称密钥加密:Bob和Alice共享相同的密钥(对称)密钥:K
·例如,关键是了解单声道中的替换模式字母置换密码
Public Key Cryptography
symmetric key crypto
• requires sender,receiver know shared secret key
• Q: how to agree on key in first place(particularly if never “met”)
对称钥匙密码
·要求发送方、接收方知道共享密钥
·问:如何首先就关键问题达成一致(特别是如果从未“见过”)
public key crypto
1.radically different approach [Diffie-Hellman76, RSA78]
2. sender, receiver do not share secret key
3.public encryption key known to all
4.private decryption key known only to receiver
公钥密码
1.完全不同的方法[Diffie-Hellman 76,RSA 78]
2.发送方和接收方不共享密钥
3.公开密钥
4.只有接收方知道的私有解密密钥
RSA: Creating public/private key pair
1.choose two large prime numbers p, q.
(e.g., 1024 bits each)
2.compute n = pq, z = (p-1)(q-1)
3.choose e (with e<n) that has no common factors
with z (e, z are “relatively prime”).
4.choose d such that ed-1 is exactly divisible by z.(in other words: ed mod z = 1 ).
5.public key is (n,e). private key is (n,d).
Why is RSA secure?
• suppose you know Bob’s public key (n,e). How hard is it to determine d?
• essentially need to find factors of n without knowing the two factors p and q
• fact: factoring a big number is hard
Shell Scripting
The shell is a command interpreter
• It acts as an interface between the user and the operating system kernel
• Usually,
• The user enters lines at the terminal
• The terminal passes each line to the shell which tries to interpret the line as a command
• The shell makes calls to the operating system to execute the command; this may generate some output that is sent to the display
• The shell returns a result
• The shell can perform sophisticated parameter expansion and command substitution and I/O redirection
• There is an IEEE/Posix Standard, IEEE Std 1003.1-2008, 2016 Edition that defines the standard behaviour of a shell
• The shell command language is defined as part of the POSIX standard
shell是一个命令解释器
·它充当用户和操作系统内核之间的接口
·通常,
·用户在终端输入行
·终端将每一行传递给shell,shell试图将该行解释为命令
·shell调用操作系统来执行命令;这可能会生成一些发送到显示器的输出
·shell返回一个结果
·shell可以执行复杂的参数扩展和命令替换以及I/O重定向
·IEEE/Posix标准,IEEE Std 1003.1-2008,2016版定义了外壳的标准行为shell命令语言被定义为POSIX标准的一部分
Operating systems such as Linux, Mac OS X, FreeBSD, etc.
allow the user to choose one of a number of different shells.
There are two distinct types:
• Bourne-compatible shells (commands: sh bash ksh zsh)
• C-shell-compatible shells (commands: csh tcsh)
• The IEEE Posix standard defines the capabilities that a compliant shell should provide
• Even Microsoft is looking to provide a standard shell for Windows
• We focus on bash (Bourne Again SHell)
操作系统,如Linux、Mac OS X、FreeBSD等。
允许用户选择多个不同shell中的一个。
有两种不同的类型:
·Bourne兼容的shell(命令:sh bash ksh zsh)
· C-shell兼容的shell(命令:csh tcsh)
IEEE Posix标准定义了兼容shell应提供的功能
·甚至微软也在寻求为Windows提供一个标准的shell
《谍影重重》(Bourne Again Shell)
A shell script is an executable text file that contains shell commands
• This allows us to build new commands from already existing commands
• The shell command language has many features. We focus here on the basics. The references provide guidance for advanced features
• At its simplest, a script is just a sequence of commands. . .
shell脚本是包含shell命令的可执行文本文件
·这允许我们从现有命令中构建新命令
shell命令语言有很多特性。我们在这里着重于基础。这些参考为高级功能提供了指导
·最简单的情况下,脚本只是一个命令序列。. .
• Ignore the first line, #!/bin/bash for the moment. The 6 remaining
lines:
• make a subfolder called src
• moves all files with names ending “.c…" there (eg C or C++ source files)
• echo a message on the console
• make a subfolder called obj
• moves all files with names ending “.o" there
• echo another message on the console
• This script, saved in a file tidy, is run at a Unix command prompt:
$ ./tidy
创建一个名为src的子文件夹
·移动名称以“. c…”结尾的所有文件。“there(例如C或C++源文件)
·在控制台上回显消息
创建一个名为obj的子文件夹
·将名称以“.o”结尾的所有文件移动到此处
·在控制台上回显另一条消息
·此脚本保存在文件tidy中,在Unix命令提示符下运行
• … and it does those 6 operations in order. This might be a useful script for tidying up a programming project working folder
• Any sequence of Unix commands may be saved in a file and run like this.
• The file’s execute permission must be set –
chmod a+x tidy
• You can switch on execute permission with the chmod
command –
• What about the first line, #!/bin/bash?
• This “shebang" line specifies which of several shells should provide the command interpreter to run the script: in this case bash
• If you don’t specify which shell to use, the default shell will be used.
This may, or may not, be what you intend
• Make this the first line of all your Unix scripts
·您可以使用chmod打开执行权限
命令─
·第一行怎么样,#!/ bin/bash?
·“shebang”行指定了几个shell中的哪一个应该提供命令解释器来运行脚本:在本例中是bash
·如果您没有指定要使用的shell,则将使用默认shell。
这可能是,也可能不是你想要的
·将其作为所有Unix脚本的第一行
chmod
In Unix and Unix-like operating systems, chmod is the command
and system call which is used to change the access permissions of file system objects (files and directories).
• The “who” values we can use are:
• u: User, meaning the owner of the file.
• g: Group, meaning members of the group the file belongs to.
• o: Others, meaning people not governed by the u and g permissions.
• a: All, meaning all of the above.
• If none of these are used, chmod behaves as if “a” had been used.
在Unix和类Unix操作系统中,chmod是命令以及系统调用,用于改变文件系统对象(文件和目录)的访问权限。
·我们可以使用的“谁”值是:
u:User,表示文件的所有者。
· g:组,表示文件所属的组的成员。
o:其他人,表示不受u和g权限控制的人。
a:全部,指的是以上所有的。
·如果这些都没有使用,chmod的行为就像使用了“a”一样。
The “what” values we can use are:
• –: Minus sign. Removes the permission.
• +: Plus sign. Grants the permission. The permission is added to the
existing permissions. If you want to have this permission and only this permission set, use the = option, described below.
• =: Equals sign. Set a permission and remove others.
• The “which ” values we can use are:
• r: The read permission.
• w: The write permission.
• x: The execute permission
我们可以使用的“什么”值是:
· -:负号。获得许可。
· +:加号。授予赠款。权限被添加到现有权限。如果您希望拥有此权限并且只设置此权限,请使用=选项,如下所述。
· =:等于符号。设置权限并删除其他权限。
·我们可以使用的“哪些“值是:
R:读取权限。
w:写权限。
· x:执行权限
• You can make your scripts more flexible by using command line arguments
• Here’s a version of the tidy script that allows you to specify which directory should be tidied
·您可以通过使用命令行参数使脚本更加灵活
·这是一个整理脚本的版本,允许您指定应该整理哪个目录
• Each word on the command line, after the command, can be referred to in the script using the positional names,
• $1, $2, $3, . . .
• You can refer to the argument list using $ *
• The command name itself can be referred to using $0
• The number of arguments can be referred to using $#
·命令行上的每个单词,在命令之后,可以在脚本中使用位置名称引用,
· $1,$2,$3,. . .
·您可以使用 $ * 引用参数列表
·命令名称本身可以使用$0引用
·可以使用 $#引用参数的数量
• The variables in the tidy3 script are tidyDir, srcDir and objDir
• Variables do not need to be declared
• Assign a value to them using =
• Notice there are no spaces before or after =
• Prefix the variable name with a $ to refer to its value
·tidy 3脚本中的变量是tidyDir、srcDir和objDir
·变量不需要声明
·使用=为它们赋值
·注意在=之前或之后没有空格
·在变量名前加上$以表示其值
Conditional expressions (tests) and if statement
• A test is written in between square brackets [ . . . ]
• Notice the space after [ and before ]
• This previous example shows a test of integers : relations
- l t -le -eq -ge -gt
• Other useful tests
·测试写在方括号[ . . . ]
·注意在[和]之前的空格
·上一个例子展示了整数的测试:关系 - l t -le -eq -ge -gt
·其他有用的测试
• What does the following mean?
stat -t $CFILES >/dev/null 2>&1,
• On Unix-like operating systems, the stat command displays the detailed status of a particular file or a file system.
• >/dev/null : redirects standard output (stdout) to /dev/null, and discards it.
• 2>&1 : redirects standard error (2) to standard output (1), and discards it as well.
·以下是什么意思?
stat -t $CFILES >/dev/null 2>&1,
·在类Unix操作系统上,stat命令显示特定文件或文件系统的详细状态。
· >/dev/null:将标准输出(stdout)重定向到/dev/null,并丢弃它。
· 2>&1:将标准错误(2)重定向到标准输出(1),并将其丢弃。
Loops - the while loop
• A much cleaner structured repetition construct. Note
• use of do and done to mark the beginning and end of the repeated part
• the test [ ] used in the same way as in conditionals.
• read –p ?
• read - Read a line from the standard input and split it into fields.
• -p prompt: output the specific string prompt without a trailing newline before attempting to read
read -p “No, try again! ->” answer
A loop helper - shifting positional parameters
• If this script is saved in file shiftEx, run it with some command-line arguments:
·如果此脚本保存在文件shiftEx中,请使用一些命令行参数运行它:
$ shiftEx one two buckle my shoe
• You should see the command-line arguments one, two, buckle, my, shoe
• displayed one below another.
• In this script, s h i f t copies all the command-line parameters down one place:
• $2 → $1, $3 → $2, $4 → $3, etc. This is repeated by the script until all the arguments
have been shifted down to position $1 and displayed by echo
• $2 is a list: a string. The for command will break the string into words and execute the body of the scrip on each word.
• This script, procFiles0, might be called like this:
$ procFiles0 workFolder “file1 file2 file3 file4”
• It will iterate through the list of files (they presumably exist in the workFolder) and run “subroutine" script procFiles1 on each one – ie, on workFolder/file1 then workFolder/file2 then workFolder/file3, then workFolder/file4
• What is procFiles1? Here it is -
• It lists the details (ls - l) of the file given in the argument $1 and offers to delete it or leave (skip) it.
• Note that this script is begin called repeatedly by procFiles0,although it can also be run directly
·它列出了参数$1中给出的文件的详细信息(ls - l),并提供删除或离开(跳过)它。
·请注意,此脚本由procFiles 0重复调用开始,尽管它也可以直接运行