Linux基础命令详解
【摘要】 Linux操作系统的诞生、发展和成长过程始终依赖着五个重要支柱:Unix操作系统、MINIX操作系统、GNU计划、POSIX标准和Internet网络
管理本地用户和组
useradd:创建用户
root:x:0:0:root:/root:/bin/bash
root:用户名
x:密码占位符
0:uid
0:gid
root:描述信息
/root:家目录
/bin/bash:登录shell(/bin/bash允许用户登录,/sbin/nolgin不允许用户登录)
命令格式:
useradd [选项] 用户名
[root@localhost ~]# useradd user1
[root@localhost ~]# tail -n 1 /etc/passwd
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
-u 指定uid
localhost ~]# useradd -u 1010 user3
user3:x:1010:1010::/home/user3:/bin/bash
-d 指定家目录
[root@localhost ~]# useradd -d /home/qq user4
[root@localhost ~]# tail -n 3 /etc/
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1010:1010::/home/user3:/bin/bash
user4:x:1011:1011::/home/qq:/bin/bash
-s 指定登录shell
[root@localhost ~]# useradd -s /sbin/nologin user5
[root@localhost ~]# tail -n 1 /etc/passwd
user5:x:1012:1012::/home/user5:/sbin/nologin
验证:
[root@localhost ~]# su - user5
This account is currently not available.
-c 指定描述信息
[root@localhost ~]# useradd -c testuser user6
[root@localhost ~]# tail -n 1 /etc/passwd
user6:x:1013:1013:testuser:/home/user6:/bin/bash
-g 将用户加入到主组(1个)
id 查看用户信息
[root@localhost ~]# groupadd group1
[root@localhost ~]# groupadd group2
[root@localhost ~]# useradd -g group1 user7
[root@localhost ~]# id user7
uid=1014(user7) gid=1014(group1) 组=1014(group1)
[root@localhost ~]# id user6
uid=1013(user6) gid=1013(user6) 组=1013(user6)
[root@localhost ~]#
-G 将用户加入到附加组
[root@localhost ~]# useradd -G group2 user8
[root@localhost ~]# id user8
uid=1015(user8) gid=1016(user8) 组=1016(user8),1015(group2)
useradd里面的-u是指定uid,-g将用户加入到主组,-G将用户加入到附加
groupadd里面的-g才是指定gid
练习题目
创建组sysadm
创建用户harry,natasha,tom
要求harry,natasha的附加组为sysadm
要求tom用户的登陆shell为非交互式shell
三个用户的密码为redhat
创建用户user100
1.描述信息为ptyh
2.家目录为/home/wx
3.uid为6666
4.登录shell为/sbin/nologin
[root@localhost ~]# useradd -c ptyh -d /home/wx -u 6666 -s /sbin/nologin user100
[root@localhost ~]# tail -n 1 /etc/passwd
user100:x:6666:6666:ptyh:/home/wx:/sbin/nologin
usermod:修改用户
[root@localhost ~]# usermod -c testc -u 1515 -d /qwer c
[root@localhost ~]# tail -n 1 /etc/passwd
c:x:1515:1000:testc:/qwer:/bin/bash
[root@localhost ~]# id c
uid=1515(c) gid=1000(c) 组=1000(c),10(wheel)
[root@localhost ~]# groupadd group1
[root@localhost ~]# groupadd group2
[root@localhost ~]# usermod -g group1 -G group2 c
[root@localhost ~]# id c
uid=1515(c) gid=1001(group1) 组=1001(group1),1002(group2)
[root@localhost ~]#
-u
-c
-g
-G
-s
-d
useradd user1
echo “123” | passwd --stdin user1
-L:锁定
[root@localhost ~]# usermod -L user1
[root@localhost ~]#
-U:解锁
[root@localhost ~]# usermod -U user1
解锁之后可以登录
[root@localhost ~]# su - c
su: 警告:无法更改到 /qwer 目录: 没有那个文件或目录
[c@localhost root]$ su - user1
密码:
[user1@localhost ~]$
userdel:删除用户
-r 递归删除(从用户邮箱删除)
练习:
1.创建user2
2.分别检查3个文件中是否有user1和user2 (/etc/passwd /etc/shadow /etc/group cd /var/spool/mail ----- ls)
3.userdel user1 -----重复第2步 ----- 只能从3个文件中将用户删除,并不能删除邮箱(路径)---rmdir(空)---rm -rf 文件和目录
4.userdel -r user2 ----重复第2步 -----删除更加干净
groupadd:创建组
-g 指定gid
[root@localhost ~]# groupadd -g 6666 group4
[root@localhost ~]# tail -n 3 /etc/group
group2:x:1002:c
group3:x:1003:
group4:x:6666:
groupmod:修改组
-g 修改gid
[root@localhost ~] # groupmod -g 7777 group4
[root@localhost ~]# tail -n 3 /etc/group
group2:x:1002:c
group3:x:1003:
group4:x:7777:
-n 修改组名
[root@localhost ~]# groupmod -n testgroup4 group4
[root@localhost ~]# tail -n 3 /etc/group
group2:x:1002:c
group3:x:1003:
testgroup4:x:7777:
groupdel:删除组
[root@localhost ~]# groupdel testgroup4
补充:将用户加入到组
1.创建用户的时候将用户加入到组
[root@localhost ~]# groupadd group10
[root@localhost ~]# groupadd group20
[root@localhost ~]# groupadd group30
[root@localhost ~]# useradd -g group10 user10
[root@localhost ~]# usermod -g group20 user10
2.修改用户的组
3.直接将已存在的用户加入到组
[root@localhost ~]# gpasswd -a user10 group10
正在将用户“user10”加入到“group10”组中
[root@localhost ~]# tail -n 5 /etc/group
group2:x:1002:c
group3:x:1003:
group10:x:1004:user10
group20:x:1005:
group30:x:1006:
[root@localhost ~]# gpasswd -d user10 group10
正在将用户“user10”从“group10”组中删除
^[[A[root@localhost tail -n 5 /etc/groupup10
group2:x:1002:c
group3:x:1003:
group10:x:1004:
group20:x:1005:
group30:x:1006:
[root@localhost ~]#
Openssh
查看信息
1.查看地址
ip a = ip address show
ifconfig
2.查看网关
ip r = ip route
思路
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
1.将ONBOOT修改为yes
2.启用windows上面的vmnet8网卡
3.windows和Linux做互通
4.远程到Linux
ssh root@192.168.184.128
文件权限
[root@localhost ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1225 11月 18 2020 anaconda-ks.cfg
权限:
rw-------:一共9个字符,分3段
第一段(前3个字符):拥有人、所属者:user:u
第二段(中间3个字符):拥有组、所属组:group:g
第三段(后3个字符):其他人:other:o
r:read:读权限:4
w:write:写权限:2
x:execute:执行权限:1
-:无权限
所属关系
root:拥有人、所属者
root:拥有组、所属组
权限 | 对文件的影响 | 对目录的影响 |
---|---|---|
r (读) | 可以读取文件的内容 | 可以列出目录的内容(文件名) (列出目录下的文件名) |
w(写) | 可以更改文件的内容 | 可以创建或删除目录中的任一文件 |
x(执行) | 可以作为命令执行文件 | 可以访问目录的内容 (点进目录下目录) |
修改普通权限
rw-------:chmod---修改权限
root root:chown---修改所属
修改权限的两种方式
1.用字符修改
命令格式:chmod ugoa +-= rwx filename
原:rw-------
现:rwxrw-rw-
chmod u+x,g=rw,o+rw anac
练习:
原:rwx rw- rw-
现:rwx r-- ---
[root@localhost ~]# chmod g-w,o=- anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rwxr-----. 1 root root 1225 11月 18 2020 anaconda-ks.cfg
原:rwx r-- ---
现:r-- r-- r--
[root@localhost ~]# chmod a=r anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-r--r--r--. 1 root root 1225 11月 18 2020 anaconda-ks.cfg
2.用数字修改
命令格式:chmod nnn filename
原:r-- r-- r--
现:rwx rw- rw-
7 6 6
[root@localhost ~]# chmod 766 anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rwxrw-rw-. 1 root root 1225 11月 18 2020 anaconda-ks.cfg
原:rwx rw- rw-
现:rw- rw- rw-
chmod u-x anac
chmod 666 anac
修改所属关系
root root:chown---修改所属
命令格式:chown user|(.|:)group filename
修改用户命令格式:chown user filename
修改组令格式:chown 【.|:】group filename
修改所属者
[root@localhost ~]# ls -l anaconda-ks.cfg
-rwxrw-rw-. 1 root root 1225 11月 18 2020 anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rwxrw-rw-. 1 user4 root 1225 11月 18 2020 anaconda-ks.cfg
用.修改所属组
[root@localhost ~]# chown .c anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rwxrw-rw-. 1 user4 c 1225 11月 18 2020 anaconda-ks.
用:修改所属组
cfg[root@localhost ~]# chown :root anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rwxrw-rw-. 1 user4 root 1225 11月 18 2020 anaconda-ks.cfg
[root@localhost ~]# chown user10:user10 anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rwxrw-rw-. 1 user10 user10 1225 11月 18 2020 anaconda-ks.cfg
[root@localhost ~]#
练习:
1.创建用户user10和user20
useradd user10
useradd user20
2.创建文件1.txt,2.txt
touch {1,2}.txt
3.将1.txt和2.txt的所属者改为c
chown c 1.txt
chown c 2.txt
4.将1.txt所属组改为user10 (要求用.修改)
chown .user10 1.txt
5.将2.txt所属组改为user20 (要求用:修改)
chown :user20 2.txt
更改目录所属关系:
[root@localhost ~]# ls -ld /test1
drwxr-xr-x. 2 root root 6 11月 3 04:10 /test1
[root@localhost ~]# touch /test1/test.txt
[root@localhost ~]# ls -ld /test1/test.txt
-rw-rw-r--. 1 root root 0 11月 3 04:16 /test1/test.txt
[root@localhost ~]# chown c:user10 /test1
[root@localhost ~]# ls -ld /test1
drwxr-xr-x. 2 c user10 22 11月 3 04:16 /test1
[root@localhost ~]# ls -ld /test1/test.txt
-rw-rw-r--. 1 root root 0 11月 3 04:16 /test1/test.txt
-R 递归
[root@localhost ~]# chown -R user10:c /test1/
[root@localhost ~]# ls -ld /test1
drwxr-xr-x. 2 user10 c 22 11月 3 04:16 /test1
[root@localhost ~]# ls -ld /test1/test.txt
-rw-rw-r--. 1 user10 c 0 11月 3 04:16 /test1/test.txt
默认权限
文件夹:755=777-022
文件:644=666-022
[root@localhost ~]# umask
0022
[root@localhost ~]# umask 0002
[root@localhost ~]# touch 2.txt
[root@localhost ~]# ls -l 2.txt
-rw-rw-r--. 1 root root 0 11月 3 04:13 2.txt
[root@localhost ~]# mkdir /test2
[root@localhost ~]# ls -ld /test2
drwxrwxr-x. 2 root root 6 11月 3 04:14 /test2
ACL权限
setfacl:
getfacl:
命令格式
setfacl [选项] user:username:rwx |group:groupname:rwx filename
file:xinchuang20303-4
user:banzhang(rwx),tuanzhishu(rw),xuexiweiyuan(rw)
group:diyizu(rwx),dierzu(rw),disanzu(r)
[root@localhost ~]# touch xinchuang20303-4
[root@localhost ~]# useradd banzhang
[root@localhost ~]# useradd tuanzhishu
[root@localhost ~]# useradd xuexiweiyuan
[root@localhost ~]# groupadd diyizu
[root@localhost ~]# groupadd dierzu
[root@localhost ~]# groupadd disanzu
[root@localhost ~]#
[root@localhost ~]# setfacl -m u:banzhang:rwx,u:tuanzhishu:rw,
u:xuexiweiyuan:rw xinchuang20303-4
[root@localhost ~]# setfacl -m g:diyizu:rwx,g:dierzu:rw,g:disa
nzu:r xinchuang20303-4 [root@localhost ~]# getfacl xinchuang20303-4
# file: xinchuang20303-4
# owner: root
# group: root
user::rw-
user:banzhang:rwx
user:tuanzhishu:rw-
user:xuexiweiyuan:rw-
group::r--
group:diyizu:rwx
group:dierzu:rw-
group:disanzu:r--
mask::rwx
other::r--
-x 一步步移除
[root@localhost ~]# setfacl -x g:diyizu xinchuang20303-4
[root@localhost ~]# setfacl -x g:diyizu xinchuang20303-4
[root@localhost ~]# getfacl xinchuang20303-4
# file: xinchuang20303-4
# owner: root
# group: root
user::rw-
user:tuanzhishu:rw-
user:xuexiweiyuan:rw-
group::r--
group:dierzu:rw-
group:disanzu:r--
mask::rw-
other::r--
-b一次性移除
[root@localhost ~]# setfacl -b xinchuang20303-4
[root@localhost ~]# getfacl xinchuang20303-4
# file: xinchuang20303-4
# owner: root
# group: root
user::rw-
group::r--
other::r--
练习
1.创建文件1.txt
2.创建用户user1,user2
3.创建组group1、group2
4.给1.txt设置acl权限
user1(rwx)
user2(r)
group1(rw)
group2(r)
5.移除group2的权限
6.移除所有权限
特殊权限
suid
作用:文件的有效身份为文件的拥有者而非执行者
u+s
4
sgid
作用:文件的有效身份为文件的拥有者而非执行者
继承所属组
g+s
2
sticky(t位)
o+t
1
1.txt:默认文件权限:chmod 4644 1.txt
suid案例
作用:文件的有效身份为文件的拥有者而非执行者
字符表示法:u+s
数字表示法:4
1.用root身份:查看/etc/passwd的文件权限 ----644
[root@localhost ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 2650 11月 10 00:46 /etc/passwd
2.用户c身份:cat /etc/passwd -----能看
[root@localhost ~]# su - c
[c@localhost ~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
3.用root身份:将查看/etc/passwd的文件权限修改为640
[root@localhost ~]# chmod 640 /etc/passwd
[root@localhost ~]# ls -l /etc/passwd
-rw-r-----. 1 root root 2650 11月 10 00:46 /etc/passwd
4.用户c身份:cat /etc/passwd -----不能看
[c@localhost ~]$ cat /etc/passwd
cat: /etc/passwd: 权限不够
5.用root身份:查看cat的二进制文件路径
[root@localhost ~]# which cat
/usr/bin/cat
6.用root身份:查看/usr/bin/cat的文件权限
[root@localhost ~]# ls -l /usr/bin/cat
-rwxr-xr-x. 1 root root 51856 5月 11 2019 /usr/bin/cat
7.用root身份:将/usr/bin/cat的文件权限加晚上suid权限
[root@localhost ~]# chmod 4755 /usr/bin/cat
[root@localhost ~]# ls -l /usr/bin/cat
-rwsr-xr-x. 1 root root 51856 5月 11 2019 /usr/bin/cat
8.用户c身份:cat /etc/passwd -----能看(因为c在使用cat命令时调用了拥有人的权限)
[c@localhost ~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
sgid案例
作用:
1.文件的有效身份为文件的拥有组而非执行组
2.继承权限
字符表示法:g+s
数字表示法:2
1.用root身份:查看/etc/passwd的文件权限 ----644
[root@localhost ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 2650 11月 10 00:46 /etc/passwd
2.用户c身份:cat /etc/passwd -----能看
[root@localhost ~]# su - c
[c@localhost ~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
3.用root身份:将查看/etc/passwd的文件权限修改为640
[root@localhost ~]# chmod 640 /etc/passwd
[root@localhost ~]# ls -l /etc/passwd
-rw-r-----. 1 root root 2650 11月 10 00:46 /etc/passwd
4.用户c身份:cat /etc/passwd -----不能看
[c@localhost ~]$ cat /etc/passwd
cat: /etc/passwd: 权限不够
5.用root身份:查看cat的二进制文件路径
[root@localhost ~]# which cat
/usr/bin/cat
6.用root身份:查看/usr/bin/cat的文件权限
[root@localhost ~]# ls -l /usr/bin/cat
-rwxr-xr-x. 1 root root 51856 5月 11 2019 /usr/bin/cat
7.用root身份:将/usr/bin/cat的文件权限加晚上suid权限
[root@localhost ~]# chmod 2755 /usr/bin/cat
[root@localhost ~]# ls -l /usr/bin/cat
-rwsr-xr-x. 1 root root 51856 5月 11 2019 /usr/bin/cat
8.用户c身份:cat /etc/passwd -----能看(因为c在使用cat命令时调用了拥有人的权限)
[c@localhost ~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
案例2:继承权限
1.创建目录/test1
[root@localhost ~]# mkdir /test1
[root@localhost ~]# ls -ld /test1/
drwxr-xr-x. 2 root root 6 11月 10 02:36 /test1/
2.在目录下创建1.txt
[root@localhost ~]# touch /test1/1.txt
3.分别查看/test1和/test1/1.txt的权限和所属
[root@localhost ~]# ls -ld /test1/
drwxr-xr-x. 2 root root 6 11月 10 02:36 /test1/
[root@localhost ~]# ls -l /test1/1.txt
-rw-r--r--. 1 root root 0 11月 10 02:37 /test1/1.txt
4.修改目录的sgid权限
[root@localhost ~]# chmod g+s /test1/
[root@localhost ~]# ls -ld /test1/
drwxr-sr-x. 2 root root 19 11月 10 02:37 /test1/
5.修改目录的所属组为c
[root@localhost ~]# chown c:c /test1/
[root@localhost ~]# ls -ld /test1/
drwxr-sr-x. 2 c c 19 11月 10 02:37 /test1/
6.分别查看/test1和/test1/1.txt所属关系
[root@localhost ~]# ls -ld /test1/
drwxr-sr-x. 2 c c 19 11月 10 02:37 /test1/
[root@localhost ~]# ls -l /test1/1.txt
-rw-r--r--. 1 root root 0 11月 10 02:37 /test1/1.txt
7.在/test1/目录下创建新的文件
[root@localhost ~]# touch /test1/2.txt
8.查看的新文件的所属关系--------已经继承
[root@localhost ~]# ls -l /test1/2.txt
-rw-r--r--. 1 root c 0 11月 10 02:42 /test1/2.txt
sticky(t位)案例
作用:自己只能删除自己上传的文件,不能删除别人的文件
字符表示方式:o+t
数字表示方法:1
1.用root身份:创建用户b
[root@localhost ~]# useradd b
2.用b身份:在/tmp下创建b.txt
[root@localhost ~]# su - b
[b@localhost ~]$ touch /tmp/b.txt
3.用c身份:在/tmp下创建c.txt
[c@localhost ~]$ touch /tmp/c.txt
4.分成查看b.txt和c.txt所属关系是谁
[b@localhost tmp]$ ls -ld c.txt
-rw-rw-r--. 1 c c 0 11月 10 03:20 c.txt
[b@localhost tmp]$ ls -ld b.txt
-rw-rw-r--. 1 b b 0 11月 10 03:20 b.txt
5.用b身份删除c.txt文件
[b@localhost tmp]$ rm -rf c.txt
rm: 无法删除'c.txt': 不允许的操作
[b@localhost tmp]$
6.用c身份删除b.txt文件
[c@localhost ~]$ cd /tmp/
[c@localhost tmp]$ rm -rf b.txt
rm: 无法删除'b.txt': 不允许的操作
给目录设置t位
用数字法(建议)
[root@localhost ~]# chmod 1777 /test1/
[root@localhost ~]# ls -ld /test1/
drwxrwxrwt. 2 c c 32 11月 10 02:42 /test1/
用字符法
[root@localhost ~]# chmod o+t /test1
[root@localhost ~]# ls -ld /test1/
drwxrwxrwt. 2 c c 32 11月 10 02:42 /test1/
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
- 点赞
- 收藏
- 关注作者
评论(0)