CSE 390A, Spring 2014 Assignment 4: Users, Groups, Permissions

惠泳
2023-12-01

This assignment continues to practice using the bash shell and basics of combining commands using redirection and pipes.  Electronically turn in three files: homework4.txt, .plan, and .bash_profile as described in this document.  To receive credit, your .plan and .bash_profile should also be present in your home directory on attu. 

Task 1: .plan file (on attu)        

A file named .plan can be placed into your home directory to tell other users of the system about you.  In this file you can write any text you like, and the text will be displayed to other users when they use the finger command on you.    Set up a .plan file for yourself on attu that contains the following contents, you also need to turn this file in. 

 At least one sentence about yourself (can be real or made up) 

 A cow message as would be output from the ~rea/390/cowsay or ~rea/390/cowthink programs 

 One other piece of ASCII art, or a short message in large text  

 Want more cows in your plan?  Check out: http://www.sunnyspot.org/asciiart/gallery/cow.html 

Test your .plan file by running finger on yourself.  Turn in this file as part of your assignment submission, but part of this task is also making sure that the file can be seen by other students successfully when they use the finger command.  The file must have read permissions for all users.  Ask a classmate to try this command on you to see whether they see your .plan.  In grading I will check whether you have a .plan file by running finger on each student. 

Answer: 

.plan文件如下:

Hello everybody! Welcome to zxy's linux system.
 ____
< hi >
 ----
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


                   \  |  /         ___________
    ____________  \ \_# /         |  ___      |       _________
   |            |  \  #/          | |   |     |      | = = = = |
   | |   |   |  |   \\#           | |`v'|     |      |         |
   |            |    \#  //       |  --- ___  |      | |  || | |
   | |   |   |  |     #_//        |     |   | |      |         |
   |            |  \\ #_/_______  |     |   | |      | |  || | |
   | |   |   |  |   \\# /_____/ \ |      ---  |      |         |
   |            |    \# |+ ++|  | |  |^^^^^^| |      | |  || | |
   |            |    \# |+ ++|  | |  |^^^^^^| |      | |  || | |
^^^|    (^^^^^) |^^^^^#^| H  |_ |^|  | |||| | |^^^^^^|         |
   |    ( ||| ) |     # ^^^^^^    |  | |||| | |      | ||||||| |
   ^^^^^^^^^^^^^________/  /_____ |  | |||| | |      | ||||||| |
        `v'-                      ^^^^^^^^^^^^^      | ||||||| |
         || |`.      (__)    (__)                          ( )
                     (oo)    (oo)                       /---V
              /-------\/      \/ --------\             * |  |
             / |     ||        ||_______| \
            *  ||W---||        ||      ||  *
               ^^    ^^        ^^      ^^
                        "Cow Town"

May you have a good time ! ! !

Task 2: Bash shell commands (not on attu) For each item below, determine a single bash shell statement that will perform the operation(s) requested.  Each solution must be a one-line shell statement, but you may use input/output redirection operators such as >, <, |, and &&.  Write these commands into your homework4.txt file, one per line.  In your file, write the command that will perform the task described for each numbered item; don't write the actual output that such a command would produce.  You should perform this section on your own Linux box (not attu).  CS support dislikes a surge of people needing admin intervention due to messed up permissions, and you might inadvertently open up your files for others to access :) 

1. (Self-Discovery) The tar command compresses/decompresses files in the Unix TAR (“tape archive”) file format.  TAR merges many files into a single archive; however, unlike ZIP, TAR does not compress the contents.  Therefore most .tar files are then subsequently also compressed with a separate compression algorithm called GNU ZIP (“gzip”), which yields a .tar.gz file.  (This format was used over ZIP because of patent issues.)   For this exercise, show a single command that will decompress file hw4.tar.gz from the current folder in “verbose” mode, so that it echoes each file that is coming out of the archive.  (Hint: You don't need |, &&, ;, etc.)  You can download hw4.tar.gz on the course web site and use it for testing the other problems below. 

Answer: tar -zxvf hw4.tar.gz

2. Set the file example1.txt in the current directory (only) so that its owner and its group can read the file. (You don't need to change any other permissions that might currently be set on the file.) 

Answer: chmod ug+r example1.txt

3. (two parts)  (a) Set all files with extensions .dat and .doc in the current directory (only) to be read/writable (but not executable) by their owner, and to have no access by any users other than the owner.  Do this using the standard letter code arguments for granting and removing permissions. (b) Write this same command but using the octal number code arguments to grant/remove the permissions. 

Answer: 

(a) chmod u+rw,u-x,go-rwx *.dat *.doc
 
(b) chmod 600 *.dat *.doc

4. Set all files in the current directory and all its subdirectories (and sub-sub-directories, etc.) recursively to have a group of admin (or another group). [If you can’t get this to work see the tips on the homework page.] 

Answer: chown -R zxy ./

5. Give a command that would launch a text editor of your choice to edit the file /etc/hosts (a system configuration file that contains a mapping from internet domain names to IP addresses) as the root super-user. [Note: depending on where you are trying to do this, the actual command may not work – on attu you do not have permissions to do this, on your VM you should.  Just give the command that you would use.] 

Answer: sudo gedit /etc/hosts

6. Set all .java files in the current directory and all its subdirectories (and sub-sub-directories, etc.) to have read permission to all users.  (You can't achieve this simply with a chmod command; you need to use other commands taught recently for finding a particular group of files and processing each one as a command-line argument.) 

Answer: find -name "*.java"|xargs chmod +r

7. Set all files of at least 1 kilobyte in size in the current directory and all subdirs (and sub-sub-directories, etc.) to have a group of admin (or another group) . 

Answer: chown -R zxy `find . -type f -size +10k`

8. (Self-Discovery)  The umask command is used to specify what permissions are given by default to newly created files. Check the slides from lecture to see a more detailed discussion of how to use umask. Note: its format might be the opposite of what you expect, that is it actually specifies what permissions will be “taken away”. 

 Use umask to set it such that when you create files, the files are given read/write (but not execute) permission to you (the owner), but no permissions to anyone else. 

Answer: umask 0066


Task 3: Login script, .bash_profile (on attu) As we have discussed in class, .bash_profile is a script that runs every time you log in to a Bash shell.  For this part of this assignment, you should modify the .bash_profile file in your home directory on attu that performs the following operations.  You also need to turn this file in. 

1. Create an alias so that when you type attu, you will connect to attu.cs.washington.edu with SSH.  (This alias isn't very useful if you're testing on attu itself, but set it up anyway.)   

2. Create an alias so that when you try to delete a file or overwrite a file during a move/copy operation, the user is prompted for confirmation first.  (Hint: Set each of the three operations described to run in “interactive mode”.) 

3. Make it so that when you log on to attu you get a personalized message or action (e.g. “Welcome to attu Ruth! Have a great day! ”) You can do this by just listing the command you would execute at the bottom of your  .bash_profile. Feel free to get creative with this – cat the contents of a particular file or run other commands. Anything will be fine here as long as it prints some output to the screen that would not have happened otherwise – but really, positive messages only please! 

4.  [OPTIONAL – no extra credit, just for fun!] (Self-Discovery)  List which of your friends are online and what they are doing. Before adding this command to your .bash_profile, define a file named buddylist.txt in your home directory whose contents are the CSE user names of two or more of your friends, one per line. The file can have as many lines as you like so long as it is at least two. If you would like, you may use rea and your own username. Now add a command to your .bash_profile script to display which of your buddies are logged in to the system and what they're doing, sorted by user name.  For example, if your buddylist.txt contains: gaetano  rea  beame Then a possible output from this command of your .bash_profile might be: beame   pts/5    21:09   2:20m    0.34s   0.28s   emacs hw5.tex gaetano pts/3    00:10   7:46     0.02s   0.04s   gdb bomb gaetano pts/10   00:20   8:34     0.02s   0.00s   vim bomb.c rea     pts/8    20:33   2:15m   11.23s   0.08s   javac WordCount.java (Hint: You can get this to work using commands we've already seen, but you will need to read the man pages.) 

 Note that your command should work regardless of the number of user names in buddylist.txt. You should not assume that buddylist.txt contains exactly 3 names as shown above, etc. In class we discussed several ways to get information about who is currently logged on.  Your first step should be to figure out which of these methods gives you the exact format shown above.  The lecture 3 slides will be useful. Once you have found the correct command to call, there are several ways to call the command using the contents of buddylist.txt.  If you use xargs in your solution you will want to give it an argument that sets the max number of arguments it sends to the command in question to be one (look at the man page for that command, it says it takes a single user as a parameter, not multiple users). You can also answer the problem without using xargs at all.  Don’t forget to sort your results by username. Overall you will find it useful to search through man pages for useful options.  Inside of man you can type /keyword and hit return to find the first occurrence of keyword in the man page.  Typing n will find the next occurrence. Q will quit out of a man page. Recall that you can test the functionality of your .bash_profile by writing the following command in the shell: source .bash_profile 

Answer:

.bash_profile文件如下:

#1
alias attu=ssh root@ attu.cs.washington.edu 
#2
alias oper=mv -i
#3
c2="$(tput bold)$(tput setaf 2)"
echo "$c2 Welcome to attu Ruth! Have a great day!"
echo "$(tput sgr0)"



 类似资料: