pptx - SEAS

advertisement
CIS 191: Linux and Unix
Class 0
January 21st, 2015
Outline for today
About This Class
Introduction to Linux
Common File Commands
Permissions
What we’ll learn (generally)
•
•
•
•
•
•
•
•
•
•
Unix commands
A bit about file systems
How to shell script
How to remotely log in to other machines
How to construct regular expressions
How to use a text editor like vi(m) or emacs
The basics of unix system configuration
A little history
How to understand more XKCDs
A little bit about hardware (but just a little)
Class Goals
• Get comfortable working with the command line
– An essential skill in any computer science curriculum!
• Go over essential topics that you’d never know were
essential
• Introduce interesting topics we wouldn’t get to cover
otherwise
• Other stuff you want to know???
What you’re in for
• One lecture per week
– Please participate!
• Weekly in-class quizzes
– At the beginning of each lecture
• Starting the third week
– Based on homework and class
• One homework assignment/week
– Due before class
• Final project; no final exam!
Grading Breakdown…
•
•
•
•
Homework – 40%
Quizzes – 25%
Participation – 5%
Final Project – 30%
On Homework
• We’re going to be do-ers!
– And sometimes, sudo-ers…
– Writing scripts, creating config files, piping commands…
• Sometimes we’ll ask you to think, or write an explanation
– Usually pretty short
– “Why are configuration files human-readable?”
Participation
• Significant contributions in class and on piazza
– We’d like to see five over the course of the semester
– So, one point per point, you might say!
On the shared lecture
• Shared lecture is at night – you guys may or may not be
confused about this
• There will be a meeting starting next Tuesday
• Will meet ~4 times during the semester
Your instructor
• Spencer Lee’s the name
– CIS master’s student
– I took this course in 2012, back when Sam Panzer was teaching
it
– I’ve had a bunch of exposure to UNIX environments through my
experience with RoboCup here at Penn, and through the courses
I’ve taken in CIS and ESE.
Your TA’s
• Xiuruo Zhang
• Jonathan Chen
Contact info (also posted online)
• Spencer Lee
– lesp at seas
– Office Hour – By Appointment
• Xiurou Zhang
– xiuruo at seas
– Office Hours – 4:00-6:00PM Tuesday
– Room TBA
• Jonathan Chen
– jonchen at seas
– Office Hours – 2:00-4:00PM Monday
– Room TBA
Outline for today
About This Class
Introduction to Linux
Common File Commands
Permissions
Time for a history lesson
• Unix – born in Bell Labs in 1969
• Rewritten in C by 1973
– C’s a high-level low-level language
• More in CIS 240
– Unix was one of the first complex, time-critical systems not
written entirely in assembly.
• Ken Thompson and Dennis Ritchie; look them up!
– Full online documentation… we’ll be looking at these.
• Popular in places that could afford it (and to run it)
– In other words… it cost money!
– Used a lot in corporations and universities
GNU and the FOSS UNIX dream
• Richard Stallman founded the GNU project in the hopes
of having a free and open UNIX (GNU’s Not Unix!)
– Close to complete by the 1990s…
– Missing the kernel
– They had a project called HURD (Hird of Unix Replacing
Daemons), but it wasn’t attracting enough interest.
Kernels
• Complex bits of software…
• They convert your data requests into data processing
commands for the CPU
• User-to-hardware intermediary
Courtesy of Wikipedia…
Linus Torvalds – The man, the penguin
• Linus Torvalds decided to write an open source kernel,
when he was ~21
– It was finished in 1991 (incidentally the same year I was born…
coincidence??)
• This kernel was incorporated into existing GNU operating
system components, and LINUX was born.
Those eyes…
Linux is a “Unix-Like” kernel
• Not exactly compatible with earlier versions of Unix…
– But hey, Linux is free and open source
• It provides us with several useful abstractions for user
software
– Input and output, interfacing with other devices…
Why Unix anyway?
•
•
•
•
Processes as we know them today!
Hierarchical file system
Devices as files…
Designed to make it easy to write, test, and run
programs; useful to know if your business is software
• Device-specific details can be left to the operating system
The Unix File System
• The user sees a file system as a tree, with the root being
“/”
– Each node is a file or directory
• Each directory has its own tree
– Refer to itself by . (dot) and its parent by .. (dot dot)
• User’s home directory can be referenced by the ~
shortcut
• Previous directory is stored in the - shortcut
The Unix File System
/
home
docs
passwords.txt
dev
usr
pics
thesis.docx
cheezburger.jpg
inodes
• “Index nodes”
• Datastructure for
representing UNIX
filesystem objects
–
–
–
–
Attributes
Disk block locations
Metadata
Permissions
http://dysphoria.net/OperatingSystems1/images/unix-inode.gif
Important Root Folders (Ubuntu)
•
•
•
•
/bin contains executable binaries
/boot holds the bootloader and the kernel
/dev lists physical and virtual devices (as files)
/etc contains system-wide configurations
– Per-user config files are stored in the user’s home
• /proc holds runtime system information
Outline for today
About This Class
Introduction to Linux
Common File Commands
Permissions
Shell Commands
• Consider the basic Hello World! Command
$echo –n Hello World!
• The dollar sign ($) represents the command prompt
– You’ll also likely see # or >; this can be configured
• echo is the name of the command/program
• -n is an option or flag; here, it tells echo to not print a
trailing newling character
• Hello World! Is a command-line argument to echo.
Before we get into the details…
• I’d like to go over the one command to rule them all
• It’ll help you to understand how to use practically any
other command
man is the man
• man brings up a
program’s manual.
• Lots of useful
information therein…
– Look there first when
you’ve got a question!
xkcd.com/293/
Ten essentials
• ls, cp, mv, cat, touch, rm, mkdir, rmdir, ln, cd
• You won’t believe how often you use these.
• Note that all of these are actually program binaries
– The system knows some locations where they might be stored,
and automatically executes the file as if you were in the correct
directory.
• More on this later
– cd is not a program. It is a shell command. This is slightly
different.
Quick note
• Unix is unforgiving with file manipulations.
• If you tell the system to remove your file, it’ll do it
– NO QUESTIONS ASKED.
• Unless you tell it to ask questions.
• This includes… moving one file over another.
– Say bye to your thesis if you didn’t copy it first.
• Or even copying one file over another.
• Be careful with what you ask for!
ls – list file or directory
• When given a file, ls just lists that file
– $ ls file1 file2
– file1 file2
• ls lists the contents of a directory if given a directory. You
will find yourself typing this reflexively.
• Important options
– ls –l gives a full listing, showing permissions, owner, group,
timestamp, and file size
– ls –a lists all files, including . and .. (-A excludes . and ..)
cp – copy file or directory
• Two use cases
– $ cp source target
• Copies source file to target name (creating the file if necessary)
– $ cp source_list destination_dir
• Copies one or more files in the first argument to the directory
listed in the second argument
• cp doesn’t ask before overwriting existing files!
• Options
– -r copies directories recursively; cp won’t copy directories
without this
– -i asks before overwriting a file; -n won’t overwrite at all
mv – move files
• Behaves like cp, but the original file stops existing
– Can be used for rename
– Can be used in both ways that cp can be
• mv works recursively by default (can move directories)
• Options
– -i and –n work like in cp
rm – remove files
• $ rm file_list
• This command removes all files in the file_list argument.
• By default, this will not work recursively (won’t delete
directories)
• Options
– rm –r removes recursively – it deletes directory contents
– rm –f removes forcefully (no prompting)
– rm –i prompts the user before removing each file
ln – create links to files
• $ ln –s source target
• This command creates a symbolic link at target, pointing
to the source
• Operations at the target will be redirected to the source
• This can be very useful…
– Compatibility layer
• If I have multiple versions of python, which
– Allow a list of version-labeled files to be labeled as current
Symbolic links
• Basically a special file that just points to another file path
• They can be operated on using normal file system
commands like cp and rm, and those commands will
delete the link and not the original file
• Calling a symbolic link will call the original target
• Commands that modify file contents will modify the
original file, not the link
– Everything is redirected to the original file path
• What will happen if I create a symbolic link to the file
~/cats/felix, then I rename it to ~/cats/felinicus?
Symbolic links
• Basically a special file that just points to another file path
• They can be operated on using normal file system
commands like cp and rm, and those commands will
delete the link and not the original file
• Calling a symbolic link will call the original target
• Commands that modify file contents will modify the
original file, not the link
– Everything is redirected to the original file path
• What will happen if I create a symbolic link to the file
~/cats/felix, then I rename it to ~/cats/felinicus?
– The symbolic link will break!
Hard links
• If you run ln without the ‘-s’ option, the program will
create a hard link to the source file.
• This can be thought of as another “file path” that points
to the same i-node as the source file!
• This has interesting implications…
– If I have many hard links, how can I delete the file contents (by
which I mean the i-node hierarchy)?
– How is copying a file different from creating a hard link to it?
Hard links
• If you run ln without the ‘-s’ option, the program will
create a hard link to the source file.
• This can be thought of as another “file path” that points
to the same i-node as the source file!
• This has interesting implications…
– If I have many hard links, how can I delete the file contents (by
which I mean the i-node hierarchy)?
• The file contents will be deleted only when all references (hard
links) are removed.
– How is copying a file different from creating a hard link to it?
• Creating a hard link does not copy the i-node hierarchy; it just
creates another link to an existing one. Each link is the file!
cat – concatenate files
•
•
•
•
$ cat file_list
Displays the contents of each file in file_list
Usually, file_list is just a single file
Often, it is less compute intensive to use other programs
– We will still be using cat for many exercises, for simplicity
• Refers to standard input as a dash
• Concatenates by listing each file in file_list consecutively!
– But just prints to standard output by default…
mkdir, rmdir, touch
• mkdir makes an empty directory
• rmdir removes an empty directory
– If you ant to remove a directory with files in it, then use rm –r
• touch updates the timestamps on a file; if the file doesn’t
exist, then the file is created (as an empty file)
cd – change directory
• This command tells the shell to change to a different
working directory
– A “reference point” when looking at files
• Both relative and absolute paths work; absolute paths
start with the / character (you are starting from the root
directory, after all)
• cd – returns to the previous directory
How to destroy your Linux install
• rm –rf removes both recursively and forcefully
– So it’ll remove everything and not bother to ask you about it
• Many distibutions will assume it was a mistake…
– Ubuntu will report bach with
• rm: it is dangerous to operate recursively on ‘/’
• rm: use –no-preserve-root to override
• Famous way to “pwn n00bs”
Outline for today
About This Class
Introduction to Linux
Common File Commands
Permissions
Unix Permissions
• Three categories of what we can do…
1. Read
2. Write
3. Execute
•
Three categories for who is using the file…
1. User
2. Group
3. Other
•
This leads naturally to a 9 bit permission scheme
Permission scheme breakdown
Special bits; set with setuid
File’s permissions; set with chmod
---
rwx
rwx
rwx
setid
User
Group
Other
• Represented as a bit-vector in unix
• Here, 000111111111
• Or, 0777
Permission scheme breakdown
Special bits; set with setuid
File’s permissions; set with chmod
---
rwx
rwx
rwx
setid
User
Group
Other
• Represented as a bit-vector in unix
• Here, 000111111111
• Or, 0777
We are concerned
with these
What does this mean?
• The User is the owner
– They own the file
• The Group is the group in which the owner is in
• Others is everyone else!
Which permission?
• Prioritized resolution of differences…
• if the user is the owner
– then use the User permission
• otherwise, if the user is in the owner’s group
– then use the Group permission
• otherwise use the Other permission
What do the permissions mean…?
• Read
– I can cat the file, or open it in a text editor in read-only mode
• Write
– I can modify the file using a text editor or some other file
manipulation tool (like cat in append mode).
– I can delete the file if I want to.
• Execute
– I can execute the file if it is an executable binary file
– In other words, I can load the program into memory and run it!
Okay, but what about directories?
• Read
– I can list the directory’s contents
• Write
– I can add files to or remove files from the directory
• Execute
– I can enter the directory (such as with cd)
• Non-intuitive, but sometimes we might want to have
write-only permissions…
– Why?
Changing permissions and owners
• Change the owner of a file with chown
• Change the group that owns a file with chgroup
• Change the file permission scheme with chmod
– This is also known as changing the file’s mode
• These operations may require root access…
• Or they may require you to be the file owner
• See man for more details!
More details on chmod
• Two modes of operation that we are concerned with
– Relative
• $ chmod u+rx file
– Adds read and execute permissions to the user/owner of the file
– Absolute
• $ chmod 755 file
– Set the file’s permission to be rwx r-x r-x
chmod relative calls
• chmod o+r myfile.txt
• Gives the “other” group read permissions on myfile.txt; can also
specify ‘-’ to remote a permission
• Important characters:
a – all
u – owner
g – group
o – others
r – read
w – write
x – execute
Aside: A little more about setid bits
• There are three setid bits
– Sticky
• Off: If the user has write permission on the directory, then that
user can rename or remove files even if the user is not the owner
• On: Only the file owner, directory owner, and root can rename or
remove files that are in the directory
– Setuid – set the execution user id (EUID) of the process to the id
of the file owner
• For example, the passwd binary, which allows a user to change
his/her password, is owned by root – and the setuid is true.
• So, when Jeff executes passwd, it is executed as though root ran it!
– Setgid – like setuid, except it sets the execution group id to the
group id of the file.
What is a group?
• A list of users, for the purpose of the permissions scheme
• Useful if you are doing a class project
• Or if you are working in a company
The superuser
• Also known as root
• Root can do anything.
–
–
–
–
Modify users
Change permissions on files
Kill any process
Delete any directory
Changing users
• It is possible and sometimes useful to switch to another
user
• This can be done using the su command
• $ su user
– This will prompt for the user’s password, and then act as though
the user had logged in
– The working directory will remain the same
• If the superuser uses su, then no password is required
sudo
• Execute commands as another user without knowing the
other user’s password!
– Typically used to execute commands as root.
• Users in the sudoers file can make use of sudo
• sudo –i opens up an interactive root session
– Allows the user to execute commands as root in a running
session
– Useful for running multiple commands
– Also very dangerous.
Remember: Careful what you ask for!
• If you sudo something it
will happen, almost
assuredly.
• Make sure that what you
are asking for is what you
want.
• Root can make a lot of
dangerous system
modifications that basic
users cannot – often for
their protection!
xkcd.com/149
Download