391lecture04 18sp
391lecture04 18sp
Lecture 4
Persistent shell settings; users/groups; permissions
slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
http://www.cs.washington.edu/391/
1
Lecture summary
• Persistent settings for your bash shell
• File permissions
2
.bash_profile and .bashrc
• Every time you log in to bash (e.g. ssh attu), the commands in
~/.bash_profile are run
you can put any common startup commands you want into this file
useful for setting up aliases and other settings for remote login
• Every time you launch a non-login bash terminal (e.g. bash), the
commands in ~/.bashrc are run
useful for setting up persistent commands for local shell usage, or
when launching multiple shells
Do not put things that would produce output in .bashrc (e.g. echo)
often, .bash_profile is configured to also run .bashrc, but not
always
4
Making Changes Visible
• After editing your .bashrc or .bash_profile, how do you
make the aliases etc. in the file take effect?
.bash_profile
• log on again (e.g ssh attu), or
• bash -l (el not one) will start a login shell, or
• source .bash_profile
.bashrc
• start another bash shell (type: bash), or
• source .bashrc
5
.plan
• Another fun settings file
• Stored in your home directory
• Contains information you’d like others to be able to see
is displayed when the finger protocol is run
• Exercise: create a quick .plan file, and make sure it works with
finger
6
Users
Unix/Linux is a multi-user operating system.
10
File permissions Examples
Permissions are shown when you type ls –l:
temp2.txt:
owner of the file (rea) has read & write permission
group (orca) members have write permission (but no read
permission – can add things to the file but cannot cat it)
others have no permissions (cannot read or write)
11
Changing permissions
• letter codes: chmod who(+-)what filename
chmod u+rw myfile.txt (allow owner to read/write)
chmod +x banner (allow everyone to execute)
chmod ug+rw,o-rwx grades.xls (owner/group can read and
note: -R for recursive write; others nothing)
12
chmod and umask
chmod u+rw myfile.txt (allow owner to
read/write)
Note: leaves “group” and “other” permissions as they were.
chmod 664 grades.dat (owner rw; group rw;
other r)
Note: sets permissions for “owner”, “group” and “other” all at once.
13
Exercises
• Change the permissions on myfile.txt so that:
Others cannot read it.
Group members can execute it.
Others cannot read or write it.
Group members & Others can read and write it.
Everyone has full access.
14
Exercises (Solutions)
• Change the permissions on myfile.txt so that:
Others cannot read it. chmod o-r myfile.txt
Group members can execute it. chmod g+x myfile.txt
Others cannot read or write it. chmod o-rw myfile.txt
Group members & Others
can read and write it. chmod go+rw myfile.txt
Everyone has full access. chmod ugo+rwx myfile.txt
15
Directory Permissions
• Read, write, execute a directory?
Read - permitted to read the contents of directory (view files and sub-
directories in that directory, run ls on the directory)
Write - permitted to write in to the directory (add, delete, or rename & create
files and sub-directories in that directory)
Execute - permitted to enter into that directory (cd into that directory)
• It is possible to have any combination of these permissions:
Try these:
Have read permission for a directory, but NOT execute permission
• ????
Have execute permission for a directory, but NOT read permission
• ???
*Note: permissions assigned to a directory are not inherited by the files within that directory
16
Directory Permissions
• Read, write, execute a directory?
Read - permitted to read the contents of directory (view files and sub-directories in
that directory, run ls on the directory)
Write - permitted to write in to the directory (add, delete, or rename & create files
and sub-directories in that directory)
Execute - permitted to enter into that directory (cd into that directory)
• It is possible to have any combination of these permissions:
Have read permission for a directory, but NOT execute permission
• Can do an ls from outside of the directory but cannot cd into it, cannot access files in
the directory
Have execute permission for a directory, but NOT read permission
• Can cd into the directory, can access files in that directory if you already know their
name, but cannot do an ls of the directory
*Note: permissions assigned to a directory are not inherited by the files within that directory
17
Permissions don’t travel
• Note in the previous examples that permissions are separate from
the file
If I disable read access to a file, I can still look at its permissions
If I upload a file to a directory, its permissions will be the same as if I
created a new file locally
18
Careful with -R
• Say I have a directory structure, with lots of .txt files scattered
I want to remove all permissions for Others on all of the text files
First attempt:
• chmod –R o-rwx *.txt
• What happened?
19
Careful with –R (fix)
• Say I have a directory structure, with lots of .txt files scattered
I want to remove all permissions for Others on all of the text files
First attempt:
• chmod –R o-rwx *.txt
• What happened?
20
Super-user (root)
command description
sudo run a single command with root privileges (prompts for password)
su start a shell with root privileges (so multiple commands can be run)
Courtesy XKCD.com
22
Playing around with
power…
• Create a file, remove all permissions
Now, login as root and change the owner and group to root
Bwahaha, is it a brick in a user’s directory?
23
tar files
description
tar create or extract .tar archives (combines multiple files into one .tar file)
Originally used to create “tape archive” files
Combines multiple files into a single .tar file
You probably always want to use –f option and IT SHOULD COME LAST
• To create a single file from multiple files:
$ tar -cf filename.tar stuff_to_archive
-c creates an archive
-f read to/from a file
stuff_to_archive - can be a list of filenames or a directory
• To extract files from an archive:
$ tar -xf filename.tar
-x extracts files from an archive
24
Compressed files
command description
zip, unzip create or extract .zip compressed archives
gzip, gunzip GNU free compression programs (single-file)
bzip2, bunzip2 slower, optimized compression program (single-file)
• To compress a file:
$ gzip filename produces: filename.gz
• To uncompress a file:
$ gunzip filename.gz produces: filename
Similar for zip, bzip2. See man pages for more details.
25
.tar.gz archives
• Many Linux programs are distributed as .tar.gz archives (sometimes
called .tgz)
• You could unpack this in two steps:
1. gzip foo.tar.gz produces: foo.tar
2. tar –xf foo.tar extracts individual files
• You can also use the tar command to create/extract compressed
archive files all in one step:
$ tar -xzf filename.tar.gz
-x extracts files from an archive
-z filter the archive through gzip (compress/uncompress
it)
-f read to/from a file
Handy tip: You can use the “file” command to see what type a file is,
just changing the file extension on a file does not change its type.
26
tar examples
You can combine options (-v, -z, etc.) various ways:
Create a single .tar archive file from multiple files (without compression):
$ tar -cvf filename.tar stuff_to_archive
-c creates an archive file called filename.tar
-v verbosely list the files processed
-f read to/from a file (as opposed to a tape archive)
stuff_to_archive - can be filenames or a directory
27
tar
28