首页 > 代码库 > linux 命令

linux 命令

1)	新建用户natasha,uid为1000,gid为555,备注信息为“master”
useradd natasha -u 1000 -g 555 -c "master" 
2)	修改natasha用户的家目录为/Natasha
usermod -d /Natasha natasha
su - natasha
-bash-4.1$ pwd
/home/Natasha
-bash-4.1$ 
3)	查看用户信息配置文件的最后一行
tail -1 /etc/passwd
4)	为natasha用户设置密码“123”
echo ‘123‘|passwd --stdin natasha 
5)	查看用户密码配置文件的最后一行
tail -1 /etc/shadow
6)	将natasha用户账户锁定
usermod -s /sbin/nologin  natasha
7)	将natasha用户账户解锁
usermod -s /bin/bash  natasha
8)	新建组police,gid为999
groupadd police -g 999
9)	查看组配置文件的最后一行
tail -1 /etc/group
10)	将natasha用户加入police组
usermod -a -G police natasha
11)	修改police组的组名为jingcha
groupmod -n jingcha police
12)	删除natasha用户,连家目录和邮箱一起删除
userdel natasha -r
13)	删除jingcha组
groupdel jingcha

作业二:
1)	在用户的主目录下创建目录test,进入test创建空文件file1
cd /home/ && mkdir test && touch test/file1
2)	以长格式形式显示文件信息,注意文件的权限和所属用户和组
ls -l test/
[natasha@iZ25j36rr97Z ~]$ ls -l test/
total 0
-rw-rw-r-- 1 natasha natasha 0 May 24 17:00 file1
3)	为文件file1设置权限,使其他用户可以对此文件进行写操作。
chmod o=w test/file1
4)	查看设置结果,
-bash-4.1$ ll test/
-rw-rw--w- 1 natasha natasha 0 May 24 17:14 file1
5)	取消同组用户对文件file1的读取权限,并查看设置结果。
chmod g-r file1 
-rw--w--w- 1 natasha natasha 0 May 24 16:54 file1
6)	用数字表示法为文件file设置权限,所有者可读、可写、可执行,所属组用户和其他用户只具有读和执行的权限。设置完成后查看设置结果。
chmod 755 file1
[natasha@iZ25j36rr97Z ~]$ ll test/file1 
-rwxr-xr-x 1 natasha natasha 0 May 24 16:54 test/file1
7)	用数字形式更改文件file1的权限,使所有者只能读取此文件。其他任何用户都没有权限。查看设置结果。
chmod 500 test/ 
chmod 400 file1
8)	回到上层目录,查看test的权限
cd .. && ll test
dr-x------ 2 natasha natasha 4096 May 24 15:13 test
9)	为其他用户添加对此目录的写权限
chmod o+wx test/ -R

作业三:
以操作文件的方式,新建一个用户alex
echo ‘alex:x:1004:1004:master:/home/alex:/bin/bash‘ >>/etc/passwd
echo ‘alex:!!:17310:0:99999:7:::‘ >>  /etc/shadow
echo ‘alex:x:666:‘ >>group 
echo ‘alex:!::‘ >> /etc/gshadow
touch  /var/spool/mail/alex
uid=1004(alex) gid=1004 groups=1004

作业四:
1) 新建目录/test/dir,属主为tom,数组为group1,/test目录的权限为777
mkdir /test/dir -p && chown tom.group1 /test/dir -R
chmod 777 /test
2) 新建用户jack,切换到jack用户下,验证jack用户对dir目录的rwx权限(开启另外一个终端,依次修改dir目录的others权限)
useradd jack
su - jack
[jack@iZ25j36rr97Z test]$ ll dir
total 0
[jack@iZ25j36rr97Z test]$ touch dir/1
touch: cannot touch `dir/1‘: Permission denied
[jack@iZ25j36rr97Z test]$ cd dir
[jack@iZ25j36rr97Z dir]$ 

[root@iZ25j36rr97Z test]# chmod o=- dir
[root@iZ25j36rr97Z test]# ll
total 4
drwxr-x--- 2 tom group1 4096 May 24 17:22 dir
[rose@iZ25j36rr97Z test]$ ll dir
ls: cannot open directory dir: Permission denied
[rose@iZ25j36rr97Z test]$ touch dir/1
touch: cannot touch `dir/1‘: Permission denied
[rose@iZ25j36rr97Z test]$ echo ‘11‘ >dir/1
-bash: dir/1: Permission denied
[rose@iZ25j36rr97Z test]$ cd dir/
-bash: cd: dir/: Permission denied
 

[root@iZ25j36rr97Z test]# chmod o=r dir
[rose@iZ25j36rr97Z test]$ ll dir
total 0
[rose@iZ25j36rr97Z test]$ echo ‘11‘ >dir/1
-bash: dir/1: Permission denied
[rose@iZ25j36rr97Z test]$ cd dir/
-bash: cd: dir/: Permission denied
[rose@iZ25j36rr97Z test]$  

[root@iZ25j36rr97Z test]# chmod o=w dir 

drwxr-x-w- 2 tom group1 4096 May 24 17:22 dir
[rose@iZ25j36rr97Z test]$ ll dir
ls: cannot open directory dir: Permission denied
[rose@iZ25j36rr97Z test]$ echo ‘11‘ >dir/1
-bash: dir/1: Permission denied
[rose@iZ25j36rr97Z test]$ cd dir
-bash: cd: dir: Permission denied
[rose@iZ25j36rr97Z test]$ 

[root@iZ25j36rr97Z test]# chmod o=x dir
[rose@iZ25j36rr97Z test]$ echo ‘11‘ >dir/1
-bash: dir/1: Permission denied
[rose@iZ25j36rr97Z test]$ ll dir/
ls: cannot open directory dir/: Permission denied
[rose@iZ25j36rr97Z test]$ cd dir/
[rose@iZ25j36rr97Z dir]$

[root@iZ25j36rr97Z test]# chmod o=wx dir
[rose@iZ25j36rr97Z test]$ echo ‘11‘ >dir/1
[rose@iZ25j36rr97Z test]$ ll dir/         
ls: cannot open directory dir/: Permission denied
[rose@iZ25j36rr97Z test]$ cd dir/

[root@iZ25j36rr97Z test]# chmod o=rx dir 
[rose@iZ25j36rr97Z test]$ cd dir
[rose@iZ25j36rr97Z dir]$ cd ..
[rose@iZ25j36rr97Z test]$ echo ‘11‘ >dir/2
-bash: dir/2: Permission denied

[root@iZ25j36rr97Z test]# chmod o=rw dir
[rose@iZ25j36rr97Z test]$ ll dir
ls: cannot access dir/1: Permission denied
total 0
-????????? ? ? ? ?            ? 1
[rose@iZ25j36rr97Z test]$ cd dir
-bash: cd: dir: Permission denied
[rose@iZ25j36rr97Z test]$ echo ‘11‘ >dir/2
-bash: dir/2: Permission denied
[rose@iZ25j36rr97Z test]$ 


3)将jack加入group1组,验证jack用户对dir目录的rwx权限(开启另外一个终端,依次修改dir目录的group权限)
usermod -a -G group1 jack
[jack@iZ25j36rr97Z test]$ ll
total 4
drwxr-xrw- 2 tom group1 4096 May 24 17:39 dir
[jack@iZ25j36rr97Z test]$ echo ‘11‘ >dir/1
-bash: dir/1: Permission denied
[jack@iZ25j36rr97Z test]$ ll dir/
total 4
-rw-rw-r-- 1 rose rose 3 May 24 17:39 1
[jack@iZ25j36rr97Z test]$ cd dir
[jack@iZ25j36rr97Z dir]$ 
[jack@iZ25j36rr97Z test]$ 
4)切换到tom用户,验证tom用户对dir目录的rwx权限(开启另外一个终端,依次修改dir目录的user权限)
[tom@iZ25j36rr97Z test]$ ll dir
total 4
-rw-rw-r-- 1 rose rose 3 May 24 17:39 1
[tom@iZ25j36rr97Z test]$ cd dir
[tom@iZ25j36rr97Z dir]$ cd ..
[tom@iZ25j36rr97Z test]$ echo ‘333‘ >dir/2
[tom@iZ25j36rr97Z test]$ 
5)在dir目录内新建文件tom.txt,属主为tom,属组为group1,/test目录的权限为777
chown tom.group1 tom.txt
chmod 777 /test
6)新建用户rose,切换到rose用户下,验证rose用户对tom.txt的rwx权限(开启另外一个终端,依次修改tom.txt的others权限来配合验证过程)
[rose@iZ25j36rr97Z test]$ ll dir
total 0
-rw-r--r-- 1 tom group1 0 May 24 17:58 tom.txt
[rose@iZ25j36rr97Z test]$ echo ‘1‘ >dir/tom.txt
-bash: dir/tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
[rose@iZ25j36rr97Z dir]$ sh tom.txt  
5

[root@iZ25j36rr97Z dir]# chmod o=- tom.txt
[rose@iZ25j36rr97Z dir]$ ll
total 0
-rw-r----- 1 tom group1 0 May 24 17:58 tom.txt
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
cat: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ echo ‘11‘ >tom.txt 
-bash: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ sh tom.txt 
sh: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ 

[root@iZ25j36rr97Z dir]# chmod o=w tom.txt
[rose@iZ25j36rr97Z dir]$ ll tom.txt 
-rw-r---w- 1 tom group1 3 May 24 18:02 tom.txt
[rose@iZ25j36rr97Z dir]$ echo ‘11‘ >tom.txt 
[rose@iZ25j36rr97Z dir]$ sh tom.txt 
sh: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
cat: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ 


[root@iZ25j36rr97Z dir]# chmod o=x tom.txt 
[rose@iZ25j36rr97Z dir]$ ll
total 4
-rw-r----x 1 tom group1 3 May 24 18:02 tom.txt
[rose@iZ25j36rr97Z dir]$ echo ‘11‘ >tom.txt 
-bash: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
cat: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ sh tom.txt 
sh: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ 

[root@iZ25j36rr97Z dir]#  chmod o=rw tom.txt 
[root@iZ25j36rr97Z dir]# ll
total 4
-rw-r--rw- 1 tom group1 3 May 24 18:02 tom.txt
[rose@iZ25j36rr97Z dir]$ echo ‘3‘ >tom.txt
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
echo ‘3‘
[rose@iZ25j36rr97Z dir]$ sh tom.txt 
3


[root@iZ25j36rr97Z dir]# chmod o=rx tom.txt
[rose@iZ25j36rr97Z dir]$ ll
total 4
-rw-r--r-x 1 tom group1 9 May 24 18:11 tom.txt
[rose@iZ25j36rr97Z dir]$ echo "3" >>tom.txt 
-bash: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
echo ‘1‘
[rose@iZ25j36rr97Z dir]$ ./tom.txt
-bash: ./tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ 


[root@iZ25j36rr97Z dir]#  chmod o=wx tom.txt 
[rose@iZ25j36rr97Z dir]$ ll
total 4
-rw-r---wx 1 tom group1 20 May 24 18:33 tom.txt
[rose@iZ25j36rr97Z dir]$ echo "echo \‘3\‘" >>tom.txt 
[rose@iZ25j36rr97Z dir]$  ./tom.txt
bash: ./tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
cat: tom.txt: Permission denied

[root@iZ25j36rr97Z dir]# chmod o=rwx tom.txt 
[rose@iZ25j36rr97Z dir]$ ll
total 4
-rw-r---wx 1 tom group1 20 May 24 18:33 tom.txt
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
cat: tom.txt: Permission denied
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
echo ‘3‘
echo \‘3\‘
[rose@iZ25j36rr97Z dir]$ ./tom.txt
3
‘3‘
[rose@iZ25j36rr97Z dir]$ echo "echo \‘3\‘" >>tom.txt








7)将rose加入group1组,在rose用户下,验证rose用户对tom.txt的rwx权限(开启另外一个终端,依次修改tom.txt的group1权限来配合验证过程)
[rose@iZ25j36rr97Z dir]$ cat tom.txt 
[rose@iZ25j36rr97Z dir]$ echo ‘111‘ >tom.txt 
[rose@iZ25j36rr97Z test]$ ll dir/
total 8
-rwxrwxrwx 1 tom tom    3 May 24 16:33 1
-rw-rw-r-- 1 tom group1 4 May 24 16:42 tom.txt
8)切换到tom用户,验证tom用户对tom.txt的rwx权限(开启另外一个终端,依次修改tom.txt的user权限来配合验证过程)
[root@iZ25j36rr97Z dir]# chmod u-rw tom.txt 
[root@iZ25j36rr97Z dir]# ll
total 8
-rwxrwxrwx 1 tom tom      3 May 24 16:33 1
----rw-r-- 1 tom group1 670 May 24 16:43 tom.txt
[tom@iZ25j36rr97Z dir]$ cat tom.txt 
cat: tom.txt: Permission denied
[tom@iZ25j36rr97Z dir]$ echo ‘11‘ > tom.txt 
-bash: tom.txt: Permission denied

[root@iZ25j36rr97Z dir]# chmod u+r tom.txt   
[tom@iZ25j36rr97Z dir]$ cat tom.txt 
222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222111
[root@iZ25j36rr97Z dir]# chmod u-r tom.txt   
[root@iZ25j36rr97Z dir]# chmod u+x tom.txt
[tom@iZ25j36rr97Z dir]$ cat tom.txt 
cat: tom.txt: Permission denied
[tom@iZ25j36rr97Z dir]$ echo ‘3‘ >tom.txt 
-bash: tom.txt: Permission denied
[tom@iZ25j36rr97Z dir]$ sh tom.txt 
sh: tom.txt: Permission denied
[tom@iZ25j36rr97Z dir]$ 
[root@iZ25j36rr97Z dir]# chmod u+rw tom.txt
[tom@iZ25j36rr97Z dir]$ cat tom.txt 
222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222111
[tom@iZ25j36rr97Z dir]$ echo ‘3‘ >tom

  

linux 命令