基本命令练习及用户管理练习

1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

[root@liang /]# cat /etc/issue | tr 'a-z' 'A-Z' > /tmp/issue.out
[root@liang /]# cat /tmp/issue.out 
CENTOS RELEASE 6.8 (FINAL)
KERNEL \R ON AN \M

2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中

[root@liang /]# who | tr 'a-z' 'A-Z' > /tmp/who.out
[root@liang /]# cat /tmp/who.out 
ROOT     PTS/0        2016-08-02 17:54 (192.168.99.1)

3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名,the system version is here,please help me to check it ,thanks!
操作系统版本信息

[liang@liang ~]$  echo -e "Hello,I am `whoami`,the system version is here,please help me to check it,thanks! \n`cat /etc/centos-release`" | mail -s help root

4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开

[root@liang /]# ls -a /root/ | tr '\n' ' '
. .. aa.log anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc file1 install.log install.log.syslog .lesshst mail .tcshrc .viminfo .Xauthority


5、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和

[root@liang ~]# echo $[`cat file1 | tr -t ' ' '+'`]
55
[root@liang ~]# cat file1 | tr -t ' ' '+' | bc
55

6、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格

[root@liang ~]# echo "xt.,l 1 jr#bcmn2 c*/fe3 uz4" | tr -cd '[:digit:] '
 1 2 3 4

7、将PATH变量每个目录显示在独立的一行

[root@liang ~]# echo $PATH | tr ':' '\n'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/root/bin

8、删除指定文件多余的空行

[root@liang ~]# cat aa.log 


hello ,i am root ,

 
my os is

 
CentOS     release 6.8 


     (Final)
[root@liang ~]# cat aa.log | tr -s '[:space:]'
hello ,i am root , 
my os is 
CentOS release 6.8 
 (Final)

9、将文件中每个单词(字母)显示在独立的一行,并无空行

[root@liang ~]# cat aa.log | tr -cs '[:alpha:]' '\n'
hello
i
am
root
my
os
is
CentOS
release
Final

10、创建testuser用户,要求 uid 1234,主组:bin,辅助组:root,ftp,shell:/bin/csh home:/testdir/testuser

[root@liang testuser]# useradd -u 1234 -g bin -G root,ftp -s /bin/csh -d /testdir/testuser testuser
[root@liang testuser]# id testuser
uid=1234(testuser) gid=1(bin) 组=1(bin),0(root),50(ftp)
[root@liang testuser]# getent passwd testuser
testuser:x:1234:1::/testdir/testuser:/bin/csh

11、修改testuser用户,要求 uid:4321,主组:root,辅组:nobody,登录用户名:test,家目录:/home/test 且家数据迁移

[root@liang testuser]# usermod -u 4321 -g root -G nobody -l test -d /home/test -m testuser
[root@liang test]# getent passwd test
test:x:4321:0::/home/test:/bin/csh
[root@liang test]# id test
uid=4321(test) gid=0(root) 组=0(root),99(nobody)

12、批量创建帐号:user1...user10,要求uid:3000-3009,shell:/bin/csh,家目录:/testdir/username
用户密码:usernamepass

(1)创建用户批量文件

[root@liang /]# vim userlist
[root@liang /]# cat userlist 
user1:x:3000:3000::/home/user1:/bin/csh
user2:x:3001:3001::/home/user2:/bin/csh
user3:x:3002:3002::/home/user3:/bin/csh
user4:x:3003:3003::/home/user4:/bin/csh
user5:x:3004:3004::/home/user5:/bin/csh
user6:x:3005:3005::/home/user6:/bin/csh、
user7:x:3006:3006::/home/user7:/bin/csh
user8:x:3007:3007::/home/user8:/bin/csh
user9:x:3008:3008::/home/user9:/bin/csh
user10:x:3009:3009::/home/user10:/bin/csh

(2)使用newusers命令调用用户批量文件

[root@liang /]# newusers userlist 
[root@liang /]# getent passwd
...
user1:x:3000:3000::/home/user1:/bin/csh
user2:x:3001:3001::/home/user2:/bin/csh
user3:x:3002:3002::/home/user3:/bin/csh
user4:x:3003:3003::/home/user4:/bin/csh
user5:x:3004:3004::/home/user5:/bin/csh
user6:x:3005:3005::/home/user6:/bin/csh
user7:x:3006:3006::/home/user7:/bin/csh
user8:x:3007:3007::/home/user8:/bin/csh
user9:x:3008:3008::/home/user9:/bin/csh
user10:x:3009:3009::/home/user10:/bin/csh

(3)将用户的家目录复制过来,家目录模板文件在/etc/skel目录下

[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user1
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user2
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user3
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user4
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user5
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user6
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user7
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user8
[root@liang etc]# cp -r /etc/skel/.[^.]* /home/user9
[root@liang  etc]#  cp  -r  /etc/skel/.[^.]*  /home/user10
[root@liang user1]# ls -a /home/user1
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2

(4)修改各个用户家目录所有者及权限

[root@liang home]# chown -R user1:user1 user1
[root@liang home]# chown -R user1:user1 user2
[root@liang home]# chown -R user1:user1 user3
[root@liang home]# chown -R user1:user1 user4
[root@liang home]# chown -R user1:user1 user5
[root@liang home]# chown -R user1:user1 user6
[root@liang home]# chown -R user1:user1 user7
[root@liang home]# chown -R user1:user1 user8
[root@liang home]# chown -R user1:user1 user9
[root@liang home]# chown -R user1:user1 user10

(5)创建用户密码列表

[root@liang /]# vim passwdlist
[root@liang /]# cat passwdlist 
user1:user1
user2:user2
user3:user3
user4:user4
user5:user5
user6:user6
user7:user7
user8:user8
user9:user9
user10:user10

(6)使用chpasswd命令调用用户米密码列表

[root@liang /]# cat passwdlist | chpasswd    #调用密码文件批量设置用户密码
[root@liang /]# getent shadow user1      #查看user1的密码
user1:$6$9C8gM/W7$ECVWuxrsOMNehJQn8UvM/2aBvlRExF3hizdxVdIIYFki.5Jl68GQh.O8.h3KgAwJMQ.lD3yti.I4l4PiEy7qr.:17020:0:99999:7:::


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章