Functions of files in /etc/ and Basic Scripting Implementation (revision 130724-r05KI)

Return to Lab 01

To give a brief understanding of the functions of files that were located in /etc/, you can do the following tutorial:

  1. Exit Lab00 and enter Lab01 again.
    $ cd ..
    $ cd Lab01
  2. To see the information of users that is registered in a linux operating system, you can view the content of the file that is in /etc/passwd. Do this command:
    $ cat /etc/passwd
    
  3. The content of each /etc/passwd line is following the format that is in this page: http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/
  4. Now we will make a simple script to get the list of users from /etc/passwd. There are two ways:
    1. Using awk
      $ awk < /etc/passwd -F: '{ print $1 }'
      
    2. Using cut
      $ cut -d ":" -f 1 /etc/passwd
      
  5. To see whether a user has been registered, you can use this command:
    $ egrep -i "^username" /etc/passwd
    

    Change username with the username that you want to check.
  6. Let's play a little bit using scripting. Do these commands:
    $ vi testscript.sh
    i
    
  7. Type this script, replace your_token with your token before, NPM with your student ID.
    # ZCZC your_token NPM
    #!/bin/bash
    
    /bin/egrep  -i "^$1" /etc/passwd
    if [ $? -eq 0 ]; then
       echo "User $1 exists in /etc/passwd" 
    else
       echo "User $1 does not exists in /etc/passwd" 
    fi
    
  8. Exit and save the file that you've made:
    [ESC]
    :wq
    
  9. Change the file's permission to be executable (as the file is actually a script / program). Replace your_username with the username that you want to find.
    $ chmod 755 testscript.sh
    $ ./testscript.sh your_username
    
  10. If user isn't found, you can add a new user using this command:
    sudo adduser your_username
    

    Change your_username with the user that you want to add.
  11. To see the location of home directory of user "root", you can use this command:
    $ egrep -i "^root" /etc/passwd | cut -d ":" -f6
    
  12. The command above takes the line that contains "root" word. This line is then split based on ":", we take the sixth part of the split phrases.
  13. Create a file named groupuser.txt that contains the commands that should be run to get the group of your username. You can modify the previous commands to get the group of your username.
  14. /etc/group contains group information in your linux operating system. To see it you can do this command:
    $ cat /etc/group
    
  15. The format of the content in /etc/group is explained in this page: http://www.cyberciti.biz/faq/understanding-etcgroup-file/
  16. Create a file named listgroup.txt that contains commands that need to be run to get the list of groups that is in your operating system. Upi cam modify the previous command when you look for user list.
  17. To see the password encrypted information that is saved in your linux operating system, you can type this command:
    $ sudo cat /etc/shadow
    
  18. Format of /etc/shadow can be learnt in the following page: http://www.cyberciti.biz/faq/understanding-etcshadow-file/
  19. This marks the end of Initial Scripting Tutorial

Return to Lab 01
Return to Wiki