Linux简介
Linux 发展历史
Linux系统诞生于1991年,由芬兰大学李纳斯(Linus Torvalds)和后来陆续加入的众多爱好者共同开发完成,是UNIX的分支。
Linux是开源软件,源代码开发的UNIX
Linux Logo
Linux 内核版本
Linux内核官网:www.kernel.org
内核版本说明
2.6.8(2 主版本 6 次版本 8 末版本)
内核版和开发版区别
内核版(几十M)是基础,开发版是在内核的基础上加入自己的桌面、应用程序,也就是说开发版每个公司都能开发,但是他们用的内核都是一样的。
Linux主要的发行版本
主要的区别 软件安装,其他的基本一致
开源软件
开源软件特点,绝大多数开源软件免费,可以获得软件源代码(安全),可以自由传播、改良甚至销售
支撑互联网的开源技术
LAMP
Linux 操作系统
Apache Web服务器
MySQL 数据库
PHP 编程语言
Linux应用
基于Linux的企业服务器
www.netcraft.com (通过该网站我们可以查询各大网站服务器所使用的系统,嗅探、踩点)
嵌入式应用(手机、平板)
电影娱乐行业A
用户和组
Linux操作系统中的用户分类
普通用户 权限比管理员低 也可以登录系统
root超级管理员
用户的分类和组
/etc/passwd 保存了操作系统中所有用户的信息
root : x : 0 : 0 : root : /root : /bin/bash
HSP : x : 500 : 500 : : /home/HSP : /bin/bash
字段1:用户名称
字段2:密码占位符
字段3:用户的uid (0表示超级用户,500-60000表示普通用户,1-499表示程序用户)程序用户不允许登录系统
字段4:基本组的gid(先有组才有用户,先建立组再建立用户)
字段5:用户信息记录字段(这个字段基本上废弃了)
字段6:用户的家目录
字段7:用户登录系统后使用的命令解释器
/etc/shadow 保存了用户的密码信息
root:$6$Lx3szebY4fAW2wFq$nI19XWtQZosCU0yoCyD05Qw7AHSwfJi0uh27mrwVhmtWo1IcspLDIHaSDp1FLqxuwCvV27mE6wZqyha2q4JeC1:18673:0:99999:7:::
HSP:$6$N8BBW6c7uOPtmjZI$7o0ycutUSP/n7XgAPZ8WFdDrV8yRmTu6htR.qbk20z5AXWPXKq20XXCR4uGJPmkVkIzJNSyt0vXSYgnDVl0S31:18673:0:99999:7:::
字段1:用户名
字段2:用户密码加密后的字符串(sha加密)
字段3:距离1970年1月1日密码最近一次的修改时间(UNIX诞生时间)
字段4:密码的最短有效期(如果写3表示用户三天内不可以修改密码,0就是不限制)
字段5:密码的最长有效期(200多年,不建议这么长,建议设置为90天)
字段6:密码过期前7天警告
字段7:密码的不活跃期
字段8:用户的失效时间
/etc/group 系统中所有组信息
建立及调整用户属性
注:我们这里通过几个场景的配置,学习和了解用户属性的建立及调整
linux操作系统里面是先有组才有用户的,所以要先建立组
1、建立一个名为class1的组,组id为1000,class2的组id为2000
[root@Waffle Desktop]# groupadd class1 建立组
[root@Waffle Desktop]# cat /etc/group
class1:x:501:
[root@Waffle Desktop]# groupmod -g 1000 class1 对class1重新修改组id
[root@Waffle Desktop]# cat /etc/group
class1:x:1000:
[root@Waffle Desktop]# groupadd -g 2000 class2 新建class2,指定组id
class2:x:2000:
2、建立tom用户要求其基本组是class1组,附加组为class2组,tom用户的uid为600
注:建立用户时如果不指定基本组,系统会直接指定出一个tom组
[root@Waffle Desktop]# useradd -g class1 tom 新建tom用户 这里写class1也行写1000也行
[root@Waffle Desktop]# id tom 观察tom用户当前信息
uid=501(tom) gid=1000(class1) groups=1000(class1)
[root@Waffle Desktop]# user 按tab后可以查看user开头的命令
useradd userdel userformat userhelper userinfo usermod usermount usernetctl userpasswd users
[root@Waffle Desktop]# usermod -G 2000 -u 600 tom 这里2000写class2也行 指定基本组是-g,-G是指定附加组 -u指定uid
[root@Waffle Desktop]# id tom
uid=600(tom) gid=1000(class1) groups=1000(class1),2000(class2)
3、建立一个程序用户uid为250用户名为testuser没有家目录
程序用户特点:不能登录操作系统、没有家目录
[root@Waffle Desktop]# useradd -u 250 -M -s /sbin/nologin testuser
-M表示没有家目录
程序用户不能登录系统:-s /sbin/nologin(/sbin/nologin代表这个用户登录时使用的命令解释器,这个就是不允许登录的命令)
[root@Waffle Desktop]# id testuser
uid=250(testuser) gid=2001(testuser) groups=2001(testuser)
[root@Waffle Desktop]# su - testuser 验证不能登录系统
su: warning: cannot change directory to /home/testuser: No such file or directory
This account is currently not available.
[root@Waffle Desktop]# cd /home/ 验证没有家目录
[root@Waffle home]# ls
HSP tom
4、为tom用户设定密码为123,并设定密码最长有效期为90天,将用户密码进行锁定使其无法登陆系统
注:只有超级管理员才能为指定用户设置密码
[root@Waffle home]# passwd tom
Changing password for user tom.
New password:
BAD PASSWORD: it is WAY too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@Waffle home]# cat /etc/shadow 验证
testuser:!!:18683:0:99999:7:::
[root@Waffle home]# man chage
-M, --maxdays MAX_DAYS
Set the maximum number of days during which a password is valid. When MAX_DAYS plus LAST_DAY is less than the
current day, the user will be required to change his/her password before being able to use his/her account.
This occurrence can be planned for in advance by use of the -W option, which provides the user with advance
warning.
Passing the number -1 as MAX_DAYS will remove checking a password´s validity.
[root@Waffle home]# chage -M 90 tom -M后跟密码最长有效期
[root@Waffle home]# id tom
uid=600(tom) gid=1000(class1) groups=1000(class1),2000(class2)
[root@Waffle home]# passwd -S tom -S查看状态
tom PS 2021-02-25 0 90 7 -1 (Password set, SHA512 crypt.)
[root@Waffle Desktop]# passwd -l tom -l锁定tom用户
Locking password for user tom.
passwd: Success
[root@Waffle Desktop]# passwd -S tom
tom LK 2021-02-25 0 90 7 -1 (Password locked.)
[root@Waffle Desktop]# passwd -u tom -u表示unlock 解锁
Unlocking password for user tom.
passwd: Success
5、删除tom用户和testuser用户,删除class1组和class2组
-r的目的是删除这个用户信息的时候连同它的家目录一块删,否则他不会删除它的家目录和它的文件
[root@Waffle Desktop]# userdel -r testuser
userdel: testuser home directory (/home/testuser) not found testuser本来就没有家目录
[root@Waffle Desktop]# user -r tom
bash: user: command not found
[root@Waffle Desktop]# userdel -r tom
[root@Waffle Desktop]# id tom
id: tom: No such user
[root@Waffle Desktop]# groupdel class1
[root@Waffle Desktop]# groupdel class2
调整文件及目录权限
权限 文件或者目录属于谁,属于哪个组,不同的用户能对该文件进行什么样的操作。
现在在tmp目录下建立两个文件(一个是文件一个是目录)
[root@Waffle Desktop]# vim /tmp/test.txt
[root@Waffle Desktop]# mkdir /tmp/testdir
[root@Waffle Desktop]# cd /tmp
[root@Waffle tmp]# ls
keyring-L37Uzg virtual-root.IpFBks vmware-fonts-2554.0
orbit-gdm virtual-root.qVMQEf vmware-root
orbit-root vmware-config-11898.0 vmware-root_1301-4248680502
pulse-BULfnWe2TdkQ vmware-config-22916.0 vmware-root_1646-591958445
pulse-M80xbZuu61w5 vmware-config-2554.0 vmware-root_22728-735758538
pulse-ponS681MlrXk VMwareDnD yum.log
testdir vmware-file-mod-11995.0
test.txt vmware-file-mod-1578.0
[root@Waffle tmp]# ls -l test.txt
-rw-r--r--. 1 root root 36 Feb 25 11:44 test.txt
[root@Waffle tmp]# ls -ld testdir 看目录要加d,因为目录没办法直接看它的权限,如果不加d是看它里面内容
drwxr-xr-x. 2 root root 4096 Feb 25 11:44 testdir
[root@Waffle tmp]#
文件:-rw-r--r--. 1(节点) root(所属者) root(所属组) test.txt(前面那个root是用户名,表示这个文件属于哪个用户,后面那个root是组名,表示这个文件属于哪个组)
目录:drwxr-xr-x. 2(文件中的子目录数) root root testdir
.表示这个文件只受到selinux文件程序管理
- rw- r-- r--
d rwx r-x r-x
字段1:文件类型 -表示普通文件 d表示目录 l表示符号链接 b表示块设备(比如硬盘)
字段2:文件所属者对该文件的权限
字段3:文件所属组的权限
字段4:其他用户的权限(既不是文件的所有者也不是文件所属组中的用户)
r | w | x | |
---|---|---|---|
文件 | read读取文件 | write写入文件 | 可执行权限 |
目录 | 可以查看目录文件 | 可以增删文件 | 可以进入目录 |
chmod 修改权限
chmod 对象 算数运算符 权限 文件
对象:u(所属者) g(所属组) o(其他用户的权限) a(all)
算数运算符:- + =
权限:r w x(读 写 执行)
例:chmod o-r /tmp/test.txt
改变文件所属者为tom,所属组为tom组
chown chgrp
chown 用户 文件
chgrp 组 文件
[root@Waffle tmp]# chmod o-r /tmp/test.txt
[root@Waffle tmp]# ll /tmp/test.txt
-rw-r-----. 1 root root 36 Feb 25 11:44 /tmp/test.txt
[root@Waffle tmp]# chown tom /tmp/test.txt
[root@Waffle tmp]# chgrp tom /tmp/test.txt
[root@Waffle tmp]# ll test.txt
-rw-r-----. 1 tom tom 36 Feb 25 11:44 test.txt
8进制赋权法:
rwx rw- r-- 764
rwxr----- 740
用法:
chmod 740 文件名
注:只需记三个,其他的用这三个相加即可。
0 | 000 | - - - |
1 | 001 | - - x |
2 | 010 | - w - |
3 | 011 | - w x |
4 | 100 | r - - |
5 | 101 | r - x |
6 | 110 | r w - |
7 | 111 | r w x |
改变文件的所属者,所属组(chown,chgrp)
chown 用户 文件
chgrp 组 文件
粘滞位、sgid权限、suid权限
粘滞位针对目录进行赋权,目录中创建的文件只有建立者可以删除
创建tom和Jerry两个用户:
[root@Waffle Desktop]# useradd tom
[root@Waffle Desktop]# useradd jerry
创建文件夹并赋权:
[root@Waffle Desktop]# mkdir /tmp/test
[root@Waffle Desktop]# chmod 777 /tmp/test
[root@Waffle Desktop]# ll -d /tmp/test
drwxrwxrwx. 2 root root 4096 Feb 25 16:10 /tmp/test
切换到tom用户 在tmp目录的test文件夹下创建tom.txt文件:
[root@Waffle Desktop]# su tom
[tom@Waffle Desktop]$ cd /tmp/test
[tom@Waffle test]$ touch tom.txt
[tom@Waffle test]$ ll
total 0
-rw-rw-r--. 1 tom tom 0 Feb 25 16:11 tom.txt
[tom@Waffle test]$
登录Jerry用户,问这个文件Jerry能不能删?可以
[tom@Waffle test]$ exit
exit
[root@Waffle Desktop]# su jerry
[jerry@Waffle Desktop]$ cd /tmp/test
[jerry@Waffle test]$ ls
tom.txt
[jerry@Waffle test]$ touch jerry.txt
[jerry@Waffle test]$ ls
jerry.txt tom.txt
[jerry@Waffle test]$ rm -rf jerry.txt
[jerry@Waffle test]$ rm -rf tom.txt
[jerry@Waffle test]$ ls
赋粘滞位:针对目录赋权,目录中创建的文件只有创建者可以删除
[root@Waffle Desktop]# cd /tmp/
[root@Waffle tmp]# chmod o+t test
[root@Waffle tmp]# ll -d test
drwxrwxrwt. 2 root root 4096 Feb 25 16:16 test
[root@Waffle tmp]# su tom
[tom@Waffle tmp]$ cd test
[tom@Waffle test]$ touch tom.txt
[tom@Waffle test]$ exit
exit
[root@Waffle tmp]# su jerry
[jerry@Waffle tmp]$ cd test
[jerry@Waffle test]$ ls
tom.txt
[jerry@Waffle test]$ rm -rf tom.txt
rm: cannot remove `tom.txt': Operation not permitted
[jerry@Waffle test]$ touch jerry.txt
[jerry@Waffle test]$ ls
jerry.txt tom.txt
[jerry@Waffle test]$ rm -rf jerry.txt
[jerry@Waffle test]$
sgid针对目录建立的权限,在该目录建立的文件所属组继承父目录的属组
[root@Waffle tmp]# chmod g+s test
[root@Waffle tmp]# ll -d test
drwxrwsrwt. 2 root root 4096 Feb 25 16:20 test
[root@Waffle tmp]# su tom
[tom@Waffle tmp]$ cd test
[tom@Waffle test]$ ls
tom.txt
[tom@Waffle test]$ rm -rf tom.txt
[tom@Waffle test]$ touch tom
[tom@Waffle test]$ ll
total 0
-rw-rw-r--. 1 tom root 0 Feb 25 16:26 tom
可以看到这个文件的所属组变成root(所属组会继承父目录的所属组)
suid 对可执行文件建立。如果建立了suid,运行这个进程或者运行这个程序的用户就会继承这个文件所属者的权限
谁运行改文件就具有该文件所属者的权限
[root@Waffle tmp]# su tom
[tom@Waffle tmp]$ cd /etc
[tom@Waffle etc]$ ll /etc/passwd
-rw-r--r--. 1 root root 1685 Feb 25 16:09 /etc/passwd (TOM能看passwd文件)
[tom@Waffle etc]$ ll /etc/shadow
----------. 1 root root 1092 Feb 25 16:09 /etc/shadow (什么权限都没有)
权限 限制不了超级管理员
[root@Waffle tmp]# which vim
/usr/bin/vim
[root@Waffle tmp]# ll /usr/bin/vim
-rwxr-xr-x. 1 root root 1847752 Apr 5 2012 /usr/bin/vim
[root@Waffle tmp]# chmod u+s /usr/bin/vim
[root@Waffle tmp]# ll /usr/bin/vim
-rwsr-xr-x. 1 root root 1847752 Apr 5 2012 /usr/bin/vim
[root@Waffle tmp]# su tom
[tom@Waffle tmp]$ vim /etc/shadow 查看后把waffle的密码删了,:wq!退出(虽然还是没什么权限,但他依靠root的权限完成了删除)
[tom@Waffle tmp]$ ll /etc/shadow
----------. 1 root root 986 Feb 25 16:42 /etc/shadow
[root@Waffle tmp]# ll -d test
drwxrwsrwt. 2 root root 4096 Feb 25 16:26 test
[root@Waffle tmp]# chmod g-s,o-t test 撤销粘滞位,suid,sgid的方法
[root@Waffle tmp]# ll -d test
drwxrwxrwx. 2 root root 4096 Feb 25 16:26 test
安全权限
1、不再允许添加新用户的请求
/etc/group
/etc/passwd
/etc/shadow
/home/xxxx
不允许再变动---锁上
chattr +i 文件(解锁只要把+i变成-i即可)
2、umask
0022
目录的最高权限 0777-0022=0755
文件的最高权限 666-002=644(x一般不赋予,所以是666)
为什么umask里目录是022,文件是002,原因在下列文件里,里面有一个if语句控制着
/etc/profile /etc/bashrc
3、修改默认的密码最长有效期
vim /etc/login.defs