Lab 15 Switching Users and Setting a Umask

Goal: Become familiar with the use of several essential commands in user
identification and account switching.

System Setup: A working, installed Red Hat Enterprise Linux system with an unprivileged
user account named student with the password student and an
unprivileged user account named visitor with a password password.

Sequence 1: Switching user accounts

Instructions:

1. Switch to virtual terminal 1 (tty1) by pressing: Ctrl-Alt-F1 and log in as visitor with
the password password.

2. Record the output of the following commands:
id
uid=503(visitor) gid=508(visitor) groups=508(visitor)

pwd
/home/visitor

3. Switch to the user student by running su - student and run the commands again:
id
uid=502(student) gid=507(student) groups=507(student)

pwd
/home/student

4. Run exit to terminate student's login and return to your original visitor login.

5. Switch to the student account again, but this time run su student (without the hyphen).
Run id and pwd again:
id
uid=502(student) gid=507(student) groups=507(student)

pwd
/home/visitor

Why do these results differ from those you recorded in the previous step?

The hyphen option to su initiates a new login shell, which includes changing the CWD to
the new user's home directory and running the user's startup scripts (~/.bash_profile,
etc). When su is run without the hyphen, your UID is changed, but all other details of the
login session, including CWD and environment variables, remain the same.

6. Log out of all the shells that you used during this sequence.

Sequence 2: Using umask to set default permissions on newlycreated files

Instructions:

1. Log in to your workstation as student.

2. View your current umask.

[student@stationX ~]$ umask
0002

3. Create a couple of files and a directory (do not examine the permissions yet).

[student@stationX ~]$ touch world_readable_file1
[student@stationX ~]$ touch world_readable_file2
[student@stationX ~]$ mkdir world_readable_dir1

4. Change your umask to a more paranoid (okay, maybe you prefer the word "secure") setting.Create new files and a directory.

[student@stationX ~]$ umask 027
[student@stationX ~]$ touch restricted_file1
[student@stationX ~]$ touch restricted_file2
[student@stationX ~]$ mkdir restricted_dir1

List the files to see if you are correct:

[student@stationX ~]$ ls -ld world_readable* restricted*

drwxr-x--- 2 student student 4096 Dec 10 15:23 restricted_dir1
-rw-r----- 1 student student 0 Dec 10 15:23 restricted_file1
-rw-r----- 1 student student 0 Dec 10 15:23 restricted_file2

drwxrwxr-x 2 student student 4096 Dec 10 15:22 world_readable_dir1
-rw-rw-r-- 1 student student 0 Dec 10 15:22 world_readable_file1
-rw-rw-r-- 1 student student 0 Dec 10 15:22 world_readable_file2

5. What is the advantage of setting umask over creating files then using the chmod command?

Using umask, you leave no window of vulnerability. In fact, if you restrict enough your
umask, you can avoid any world-readable file by default.

Sequence 3: Setting a Umask

Instructions:

1. Switch to virtual terminal 1 (tty1) by pressing: Ctrl-Alt-F1

2. Log in as the user visitor with a password of password.

3. Display your current umask:

[visitor@stationX ~]$ umask

4. Below is a table of umasks. Fill in the table with the permissions of files and directories given the umask.
Umask Directory Permissions File Permissions

5. Decide on a reasonable umask for the visitor account and add the appropriate umask command to visitor's .bashrc file. Log out of the visitor account, log in again, and create a file and a directory. View the permissions. Did the directory and file permissions match your expectation? If not, revisit the table in step 4, above, and retry with a new umask.

Sequence 4: Using the Graphical User-Management Tools

Scenario: A new contractor at your office needs an account on one of the Linux systems. The username and initial password should be contractor. The account should also be a member of the web group (without changing the user's primary group!). Finally, the account should automatically expire 7 days from today.

Instructions:

1. Run System->Administration->Users and Groups and enter the root password if prompted

2. Click Add Group and enter web in the Group Name field.

3. Click Ok and return to the main Users and Groups interface

4. Click Add User, enter contractor as the username and password in the appropriate fields. Leave all other fields at their default values

5. Click Ok to return to the main Users and Groups interface

6. Select the contractor user and click Properties

7. Go to the Account Info tab and check Enable Account Expiration. Calculate the date one week from now and enter it into the Account Expires fields.

8. Go to the Groups tab, scroll to the web group and check the checkbox next to it.

9. Click Ok to return to the main Users and Groups interface.

10. Close the Users and Groups window.

Challenge Sequence 5: Automating User Creation

Scenario: You have been asked to create a large number of user accounts. Since performing repetitive tasks by hand is for chumps, you have decided to write a shell script that uses a for loop on a list of users to create the accounts, generate a random password (different for each user) and send an email notifying users of their account information.

Instructions:

1. First of all, create a file called ~/userlist, which contains the usernames you are about to create. For example, your file might look like this:

sara
harry
marion
tyrone
tappy

2. Devise a command that could be used to create a user whose name is provided by a shell variable called $NAME
/usr/sbin/useradd $NAME

3. Devise a command that generates a 10 byte long, base64-encoded, random value and a command line that stores its output in a variable called PASSWORD

HINT: This will require a command you have not seen before. Start by looking at openssl rand --help

PASSWORD=$(openssl rand -base64 10)

4. Devise a command that non-interactively changes the password of $NAME to $PASSWORD.

HINT: passwd --stdin causes passwd to accept a password on STDIN, meaning you can pass values to it over a pipe.

echo $PASSWORD | passwd --stdin $NAME

5. Devise a command line that uses the mail command to email the values of $NAME and $PASSWORD to root@example.com. The email should have the subject "Account Info".

echo "username: $NAME, password: $PASSWORD" | mail -s "Account Info"
root@example.com

6. Finally, gather all your commands within a for loop that sets the NAME variable to each line of the userlist in turn.

Your finished script should look something like this:

#!/bin/bash
# Script for creating all users defined in
# a file called ~/userlist.
for NAME in $(cat ~/userlist)
do
  /usr/sbin/useradd $NAME
  PASSWORD=$(openssl rand -base64 10)
  echo PASSWORD | passwd --stdin $NAME
  echo "username: $NAME, password: $PASSWORD" | mail -s "Account Info" root@example.com
done

Note that there are still several things that could be improved about this script. For example, passwords are being sent in unencrypted emails. Although anything more complex than what we have here would be outside the scope of this course, you are encouraged to experiment with ways to add your own improvements!

7. Test your script (be sure to use sudo or log in as root first!). Try adding -x to your shbang
if you have problems.

RH033读书笔记(14)-Lab 15 Switching Users and Setting a Umask的更多相关文章

  1. RH033读书笔记(13)-Lab 14 Network Clients

    Goal: Practice using a variety of tools to transfer files between your system and a remote system. S ...

  2. RH033读书笔记(16)-Lab 17 Installation and Administration Tools

    Lab 17 Installation and Administration Tools Goal: Become familiar with system configuration tools a ...

  3. RH033读书笔记(8)-Lab 9 Using vim

    Lab 9 Using vim Sequence 1: Navigating with vim 1. Log in as user student 2. [student@stationX ~]$ c ...

  4. RH033读书笔记(5)-Lab 6 Exploring the Bash Shell

    Lab 6 Exploring the Bash Shell Sequence 1: Directory and file organization 1. Log in as user student ...

  5. RH033读书笔记(2)-Lab 3 Getting Help with Commands

    Lab 3 Getting Help with Commands Sequence 1: Using the Help Tools 1. man -f keyword whatis keyword l ...

  6. RH033读书笔记(4)-Lab 5 File Permissions

    Lab 5 File Permissions Sequence 1: Determining File Permissions 1. What is the symbolic representati ...

  7. RH033读书笔记(3)-Lab 4 Browsing the Filesystem

    Lab 4 Browsing the Filesystem Sequence 1: Directory and File Organization 1. Log in as user student ...

  8. RH033读书笔记(7)-Lab 8 Introduction to String Processing

    Lab 8 Introduction to String Processing Sequence 1: Exercises in string processing 1. Other than the ...

  9. RH033读书笔记(6)-Lab 7 Standard I/O and Pipes

    Lab 7 Standard I/O and Pipes 1. [student@stationX ~]$ cat /proc/cpuinfo /proc/meminfo 2. [student@st ...

随机推荐

  1. poj1860(spfa判正环)

    题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rat ...

  2. aMule代码分析(2)——CFileDataIO类和CFile类

    aMule中的类很多,Maixee今天选择了两个比较基础的类,均是跟文件操作有关的,分别是CFileDataIO类和CFile类.其中,前者是基类,后者由它派生而出的.具体的继承关系,可以参考这张图: ...

  3. hosts文件导致无法访问网站

    前段时间有人反映无论怎么样都无法在自己的电脑上访问法兰克官网,那台电脑的DNS也无法解析,通过查看hosts文件后发现,原来该电脑的hosts文件木马修改过了,屏蔽了相关的域名,删除新增的或者用其他机 ...

  4. poj1094Sorting It All Out

    主题链接: 啊哈哈,选我 题目: Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 268 ...

  5. python基础课程_2学习笔记3:图形用户界面

    图形用户界面 丰富的平台 写作Python GUI程序前,须要决定使用哪个GUI平台. 简单来说,平台是图形组件的一个特定集合.能够通过叫做GUI工具包的给定Python模块进行訪问. 工具包 描写叙 ...

  6. 牛逼的验证码,printf返回值

    牛逼的验证码,如下图, 结果是4321,为什么呢,主要是printf返回值问题?那么printf到底返回什么? 经查阅,printf的返回值是打印的字符个数,因此结果是4321就很明显了.

  7. Knockout获取数组元素索引的2种方法,在MVC中实现

    原文:Knockout获取数组元素索引的2种方法,在MVC中实现 在遍历数组.集合的时候,通常要获取元素的索引,本篇体验使用Knockout获取索引的2种方法. 假设有这样的一个模型: namespa ...

  8. 2012 PHP热门资料64个+经典源码50个——下载目录 :

    完整附件0豆下载:http://down.51cto.com/data/419216 附件部分预览: PHP精彩应用实例程序源码集锦 http://down.51cto.com/zt/39 无师自通: ...

  9. SVN的revert和update命令的区别

    svn中的revert和update 今天有人问到revert和update的问题. 刚开始还真被问住了. 因为感觉revert和update都可以将本地的copy更新到以前的一个版本,会有什么不同呢 ...

  10. 学习笔记 Android.mk 搜索自己主动

    最近一直Android.mk这是什么一个令人沮丧的夜晚,点击此处记录. ios你担心更多.不管那么多.xcode自己解决. 文本工具:MACVIM(文本编辑工具 很有用 你可以清楚地分辨tab 和Sp ...