码迷,mamicode.com
首页 > 系统相关 > 详细

第二周Linux重点内容

时间:2018-05-27 22:47:21      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:第二周重点内容

第4天
3A
认证 授权 审计
authentication authorization accouting aduition

root :管理员
Linux 组: groupname gid
管理员组 root
other

Linux安全上下文
进程(process) 进程访问资源的权限取决于运行者的身份
管理员 (root) /bin/cat (所有用户都能使用) /root/.bashrc
判断:1使用工具 2 使用文件
[root@centos ~]#ll /bin/cat
-rwxr-xr-x. 1 root root 48568 Mar 23 2017 /bin/cat
[root@centos ~]#ll /root/.bashrc
-rw-r--r--. 1 root root 206 May 16 16:52 /root/.bashrc
zhangsan (1000 uid ,2000gid,3000gid) /bin/cat (rootuid=0,admin=2000) /app/passwd (root,root)
组:admin :2000 rw-r--r-- (other) 既不是所有者也不属于组,但是属于other
------rwx (zhangsan ,root )/app/passwd 所有者/所有有组
---rwxrwx (1000,3000) 所有者没有权限,owner权限生效,组权限没有用!
inode 65535 存的inode 对应的id 号
Linux组 :有且只能有一个
附加组可以有多个
/etc/passwd man 5 passwd
/etc/shadow
/etc/group
/etc/gshadow
passwd :
[root@centos7 ~]#openssl rand -base64 30 |head -c30
j+6PERTSQf/tpkuGiHUQS2+5BLgMG3

加密算法: sha512位
root uid=0
张三 uid=0 通过 /etc/passwd 查看uid

root
[root@centos ~]#useradd -s /sbin/nologin user2 创建系统 /sbin/nologin
useradd: user ‘user2‘ already exists
[root@centos ~]#usermod -s /sbin/nologin user2
[root@centos ~]#su - user2
This account is currently not available.

[root@centos ~]#ls -I "[^.]*" -a 去掉点的文件忽略不带点的文件
. .bash_profile .dbus .gnupg .lesshst .rnd
.. .bashrc .esd_auth .gtk-bookmarks .local .ssh
.abrt .cache .gconf .gvfs .nautilus .tcshrc
.bash_history .config .gnome2 .ICEauthority .pulse .viminfo
.bash_logout .cshrc

[root@centos ~]#ll /etc/shadow
----------. 1 root root 2046 May 22 10:13 /etc/shadow 修改密码为啥没有读写权限
cat /etc/ shadow
1 date +%s 1970 _2018年秒
vim /etc/shadow

[root@centos ~]#chage -l sun
Last password change : May 15, 2018
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 2
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
date -s 设定时间
[root@centos ~]#echo centos |passwd --stdin sun
history 取消

minix mum date :

[root@centos ~]#date -s ‘2018-5-25‘ 2天时间内不能修改密码
Fri May 25 00:00:00 CST 2018
[root@centos ~]#su - sun
[sun@centos ~]$passwd 普通用户不能添加user名 超级用户切换密码,不要验证,普通用户切换密码需要验证

maximum date :
date -s 设定时间
[root@centos ~]#echo centos |passwd --stdin sun
history 取消
HISTCONTROL=ignorespace 取消历史和空格文件
echo $HISTCONTROL
nano ~/.bash_profile 配置文件 history
echo centos |passwd --stdin sun
[root@centos ~]#

取消时间:
warning 时间
[cao@centos ~]$su - sun warning 时间
Password:
Warning: your password will expire in 6 days

expires time 过期时间 过了会让你修改密码
[sun@centos ~]$su - sun
Password:
You are required to change your password immediately (password aged)
Changing password for sun.
(current) UNIX password:
账户过期时间:
Password:
Your account has expired; please contact your system administrator
su: incorrect password
例如:公司外来一个员工,如果2018年月sun底离职
date 显示当前时间:

date -s ‘2018-12-29‘ 距离1970年时间 -2018-12-29多少天
clock -s 硬件时间设置软件时间
date +%s 2018-12-29时间 S秒 转换成天 《天》
[root@centos ~]#chage -l sun
Last password change : Dec 30, 2018
Password expires : Jan 19, 2019
Password inactive : Jan 26, 2019
Account expires 账户过期时间 : Dec 29, 2018
Minimum number of days between password change : 2
Maximum number of days between password change : 20
Number of days of warning before password expires : 7

Your account has expired; please contact your system administra
passwd sun
passwd: all authentication tokens updated successfully.

chage 时间
查找用户:
[root@centos ~]#chage sun
Changing the aging information for sun
Enter the new value, or press ENTER for the default

    Minimum Password Age [0]: 
    Maximum Password Age [99999]: 
    Last Password Change (YYYY-MM-DD) [2018-05-22]: 
    Password Expiration Warning [7]: 
    Password Inactive [-1]: 
    Account Expiration Date (YYYY-MM-DD) [-1]: 

man 5 chage
pwconv
pwunconv
grpconv
grpunconv

群组信息非常重要
主组:放在/etc/passwd 例如cao 501 主组编号:uid cao :cao 用户主组是:
查看主组501,/etc/group cao:x 501:辅助组成员,谁是cao的辅助组,辅助组成员,谁把cao 当成辅助组
group
cao:x:501:root,sun (把cao 当作辅助组)
[root@centos ~]#id cao
uid=501(cao) gid=501(cao) groups=501(cao)
[root@centos ~]#id sun
uid=500(sun) gid=500(sun) groups=500(sun),501(cao)
[root@centos ~]#
[root@centos ~]#id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
原因:添加到附加组,必须重新登陆否则,就是上面的结果
重新登陆获取最新的消息
[root@centos ~]#id root
uid=0(root) gid=0(root) groups=0(root),501(cao)

gpasswd :修改组mima
passwd :修改密码 uid
添加附加组成员: usermod -G mage root (把马哥当作附加组)
usermod -G mage sun
添加附加组:
[root@centos ~]#usermod -aG cao sun 追加
[root@centos ~]#usermod -aG root sun
[root@centos ~]#groups sun
sun : sun root cao
newgroup 切换主组
gpasswd opts 设置组mima

[sun@centos ~]$newgrp opts

/etc/gshadow 添加组管理员:man gpasswd
gpasswd -A
[root@centos ~]#gpasswd -A sun opts 把sun 当作opts 的管理员

[root@centos ~]#gpasswd
Usage: gpasswd [option] GROUP
外部命令
Options:
-a, --add USER add USER to GROUP
-d, --delete USER remove USER from GROUP
-h, --help display this help message and exit
-Q, --root CHROOT_DIR directory to chroot into
-r, --delete-password remove the GROUP‘s password
-R, --restrict restrict access to GROUP to its members
-M, --members USER,... set the list of members of GROUP
-A, --administrators ADMIN,...

[root@centos ~]#usermod -G opts mage root 管理员
[sun @centos ~]# gpasswd -a mage opts 针对/etc/gshadow admin 管理员
用户
useradd
usermod
userdel
[root@centos ~]#groupadd admingroup

[root@centos ~]#useradd -u 2000 -g admingroup mage3
[root@centos ~]#id mage3
[root@centos /home]#useradd -g cao mage50 主组cao 用户mage50
[root@centos /home]#id mage50
uid=2006(mage50) gid=501(cao) groups=501(cao)
[root@centos ~]#cat /etc/default/useradd

useradd defaults file

GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
不创建家目录:useradd -M magetest 相当于删除家目录
[root@centos ~]#useradd -N a
[root@centos ~]#getent passwd a
a:x:2007:100::/home/a:/bin/bash

不创建家目录:useradd -M magetest 相当于删除家目录
[root@centos /home/magetest]#cp -a /etc/skel/.[^.]* .()当前工作目录 拷贝目录注意
王老师: cp -r /etc/skel /home/haha 把skel 目录拷贝病命名成haha目录
[root@centos /home]#cd /etc/skel/ 创建用户的配置文件全部在这里
[root@centos /etc/skel]#ls
welcometomage
[root@centos /etc/skel]#ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 .mozilla welcometomage

[root@centos /home]#chown -R magetest.magetest magetest/ 这是所有者
[root@centos /home]# chmod -R 700 magetest/ 这是权限

[root@centos /home]#cd /var/spool/mail root 邮箱
vim /etc/default/useradd
[root@centos /var/spool/mail]#useradd -D -s /bin/sch
[root@centos /var/spool/mail]#user
创建批量用户
newusers passwd 格式文件 userdel -r 删除 创建 userlist.tst> newusers userlist.tst
chpasswd 批量修改用户命令
chpasswd < piliang,txt vim piliang,txt mage1:cnetos mage2:rehat (用户不创建密码),批量创建不同的密码


groupadd
groupmod
groupdel
密码批量修改mima :

没有整理

重点::切忌:端口号
禁止root远程登陆
工作不能root登陆
提高实力 :

客户端的配置文件是/etc/ssh/ssh_config
服务端的配置文件是/etc/ssh/sshd_config
[root@centos ~]#vim /etc/ssh/ssh_config

第五天:整理重点

文件目录加执行权限
X:目录代表文件可以进入
x:代表文件可以执行
[root@centos /app]# chmod a-x * 去掉目录下所有的权限

[root@centos /app]# chmod a+X * 只给目录添加权限

[root@centos /app]#ll f1
-rw-r--r--. 1 root root 0 May 25 18:10 f1
[root@centos /app]#chmod g+wx f1
-rw-rwxr--. 1 root root 0 May 25 18:10 f1

[root@centos /app]#chmod --reference f1 f2
[root@centos /app]#ll f1 f2
-rwxr-xr-x. 1 root root 0 May 25 18:10 f1
-rwxr-xr-x. 1 root root 0 May 25 19:50 f2
经典案例:

1@@@@@@@@@@@@
[liubei@centos /app]$ll
total 4
drwxr-x---. 3 liubei shuguo 4096 May 25 20:15 house
[liubei@centos /app]$ chmod 000 house/ 所有者可以更改自己文件的权限
[liubei@centos /app]$ll
total 4
d---------. 3 liubei shuguo 4096 May 25 20:15 house
**文件的权限只有所有人和root 可以更改
wx
当目录有权限w 时候,文件不可读,但是可以删(文件只有读权限)
解释,一个笔记本,上了一把锁,你不能用,但是可以删掉

3@@@@@@@@@@@@@@@@
guanyu@centos /app/house]$cp /etc/passwd letter.txt
cp -f
[guanyu@centos /app/house]$cp /etc/passwd letter.txt 覆盖文件要有写权限,所以才可以覆盖
cp: cannot create regular file `letter.txt‘: Permission denied
cp -f 就是当文件没有写 权限, CP -f 可以删除也可以创建
inode 不变
数据删除并重命名替换
删除释放 inode c重新释放,并填充
当 目录只有X 权限时 ,可以进去,可以访问目录下的文件
~/.bashrc 将umask保存起来

特殊权限:
[root@centos ~]#stat /usr/bin/passwd
File: `/usr/bin/passwd‘
Size: 30768 Blocks: 64 IO Block: 4096 regular file
Device: 802h/2050d Inode: 794284 Links: 1
Access: (4755/-rwsr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-05-17 04:31:10.011107221 +0800
Modify: 2015-11-24 00:30:08.000000000 +0800
Change: 2018-05-17 04:26:19.014106977 +0800

suid 特殊权限:
[root@centos /app]#cp /bin/touch .
[root@centos /app]#chown liubei touch
[root@centos /app]# mv touch liubeituch
[root@centos /app]#su - guanyu
[guanyu@centos ~]$cd /app
[guanyu@centos /app]$touch gyfile1
-rw-rw-r--. 1 guanyu guanyu 0 May 25 22:31 gyfile1
[guanyu@centos /app]$./liubeitouch gyfile2
-rw-rw-r--. 1 guanyu guanyu 0 May 25 22:33 gyfile2
[root@centos /app]#chmod u+s /app/liubeituch
[root@centos /app]# su - guanyu
[guanyu@centos ~]$cd /app
[guanyu@centos /app]$./liubeituch gyfile3
-rwsr-xr-x. 1 liubei root 52560 May 25 22:29 liubeituch
suid : 对于可执行的二进制文件作用了suid权限之后,任何人在执行该文件时,可临时拥有其所属人的权限

-rwsr-xr-x. 1 liubei root 52560 May 25 22:29 liubeituch

为什么脚本不能suid
脚本随意改
脚本是root 里面是rm -rf / 删除跟了漏洞bug
[root@centos /app]#vim txt.sh
-rwxrwxrwx. 1 root root 47 May 25 22:55 txt.sh
[root@centos /app]#chmod u+s txt.sh
-rwsrwxrwx. 1 liubei root 47 May 25 22:55 txt.sh
[root@centos /app]# su - guanyu
[guanyu@centos ~]$cd /app
[guanyu@centos /app]$./txt.sh
hello world
[guanyu@centos /app]$ll file10
-rw-rw-r--. 1 guanyu guanyu 0 May 25 22:58 file10

对于二进制没有效果:
sgid
对于二进制程序可以正常使用
[guanyu@centos /app]$which mkdir
/bin/mkdir
[guanyu@centos /app]$cp /bin/mkdir /app/liubeimkdir

[guanyu@centos /app]$ll liubeimkdir
-rwxr-xr-x. 1 guanyu guanyu 50056 May 25 23:14 liubeimkdir

[root@centos /app]#chgrp liubei /app/liubeimkdir
[root@centos /app]#ll liubeimkdir
-rwxr-xr-x. 1 guanyu liubei 50056 May 25 23:14 liubeimkdir
[root@centos /app]#chmod g+s /app/liubeimkdir
[root@centos /app]#su - guanyu
[guanyu@centos ~]$mkdir guanyundir
drwxrwxr-x. 2 guanyu guanyu 4096 May 25 23:17 guanyundir

[guanyu@centos /app]$./liubeimkdir guanyudir2

[guanyu@centos /app]$ll
total 56
drwxrwxr-x. 2 guanyu liubei 4096 May 25 23:18 guanyudir2

sgid 目录
对与目录作用了sgid权限后,任何人在该目录下创建的文件的所属继承目录的所属组

1、创建目录所属组
2、g+s 该目录下所有文件继承了所属组的权限

所有人只能改权限
不能改所有人 刘备是所有者 chown cao house/
mkdir house
chmod 770 house/ 所有者的权限只能root和所有者可以改
chgrp shuguo wc
chmos g+s wc /
cd /wc
touch f1 属于所属组

sticky
对于目录作用了sticky 之后,该目录下的文件及子目录,仅其所属人和目录
的所属人及root才能删除
[root@centos /app]#chmod o+t house/
[root@centos /app]#ll
total 4
drwxrwx--T. 6 liubei shuguo 4096 May 26 08:49 house
[root@centos /app]#su guanyu
[guanyu@centos /app/house]$ touch guanyufiel
[zhangfei@centos /app/house]$rm zhangfeifile 不能删除:只有木有所有人可以删除

[liubei@centos /app/house]$rm -f liubeifile
[liubei@centos /app/house]$touch liubeifile

[liubei@centos /app]$ll
total 4
drwxrwx--T. 6 liubei shuguo 4096 May 26 08:57 house
[liubei@centos /app]$mkdir a
[liubei@centos /app]$mkdir b
[liubei@centos /app]$ ll
total 12
drwxrwxr-x. 2 liubei liubei 4096 May 26 10:34 a
drwxrwxr-x. 2 liubei liubei 4096 May 26 10:34 b
drwxrwx--T. 6 liubei shuguo 4096 May 26 08:57 house
[liubei@centos /app]$chmod a-x a
[liubei@centos /app]$chmod u+s a b
[liubei@centos /app]$chmod g+s a b
[liubei@centos /app]$chmod o+t a b
[liubei@centos /app]$ll
total 12
drwSrwSr-T. 2 liubei liubei 4096 May 26 10:34 a
drwsrwsr-t. 2 liubei liubei 4096 May 26 10:34 b
drwxrwx--T. 6 liubei shuguo 4096 May 26 08:57 house
chattr +a 不可修改,可以追加不能删除
chattr +i 什么也不能做,只能读

acl 生效顺序:所有者,自定义用户,自定义组,其他人
查看ACL 是否挂载
[root@centos /app]# tune2fs -l /dev/sda3 |grep acl
Default mount options: user_xattr acl

[root@centos /app]#fdisk /dev/sda 创建分区
[root@centos /app]#partx -a /dev/sda 更新,让内核重新读取
[root@centos /app]#ls /dev/sda6
/dev/sda6
[root@centos /app]# tune2fs -l /dev/sda6 |grep acl

[root@centos /app]#tune2fs -o acl /dev/sda6
[root@centos /app]#tune2fs -l /dev/sda6 |grep option

acl 挂载
[root@centos /app]# mount /dev/sda6 /mnt/
卸载
[root@centos /app]#umount /mnt/
即使root有權限:
app cd house/

ACl
未來創建:

[root@centos /app]#setfacl -m d:u:liubei:rwx house/
當前子目錄
[root@centos /app]#setfacl -Rm u:liubei:rwx house/
acl 壓縮
[root@centos /app]#tar cvf house.tar house/

-rw-r--r--. 1 root root 10240 May 27 14:48 house.tar
ACL 備份:
tar cvf house.tar house 備份數據
getfacl -R house >acl.txt ACL 备份
[root@centos /app]#tar -xvf house.tar -C /var/tmp 解压
[root@centos /var/tmp]#setfacl --restore acl.txt 恢复数据
cp acl.txt /var/tmp f复制
[root@centos /app]#cp acl.txt /var/tmp/ 复制

[root@centos /app]#mkdir -pv testdir/dir
[root@centos /app]#setfacl -Rm u:liubei:rwx /testdir/dir/
[root@centos /app]#setfacl -m d:u:liubei:rwx /testdir/dir/
[root@centos /app]#tar -cvf dir.house testdir/dir/ 压缩
[root@centos /app]#tar -xvf dir.house testdir/dir/ 解压
[root@centos /app]#getfacl -R testdir/dir/ > acl.txt
[root@centos /app]#getfacl -b testdir/dir/
[root@centos /app]#setfacl --restore dir.house

第六天

vim 编辑器 整理重点
vim 没有正常关掉怎么回复
[root@centos /app]#vim fstab 打开

watch -n 0.1 ls -a 监控

[root@centos /app]#vim -r fstab 恢复 数据或者 R 恢复数据之后删除
[root@centos /app]#rm -f.fstab.s
[root@centos /app]#watch -n 0.1 ls -Al
Every 0.1s: ls -Al Sat May 26 20:53:17 2018

egrep 正则练习
1、[root@centos ~]#cut -d: -f1,7 < /etc/passwd |egrep "\<root|mage|wang\>"
2、[root@centos ~]#cut -d: -f1,7 < /etc/passwd |egrep -w "\<root|mage|wang\>"
2、[root@centos /app]#cat functions | grep -o "^[[:alpha:]+_].()"
3、[root@centos /app]#echo "/rc.d/init.d/functions" |egrep -o "[^/]{1,}/?$"
functions
[root@centos /app]#echo "/rc.d/init.d/functions" |egrep -o "[^/]{1,}/?"
rc.d/
init.d/
functions
4、[root@centos /app]#echo "/rc.d/init.d/functions" |egrep -o ‘/.
/‘

6、0-9 [1-9][0-9] 1[0-9]{2} 2[0-4][0-9] 25[0-5]
7、[root@centos ~]#ifconfig |egrep -o "\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-9])\>"
vim

yy
1,3y p 粘贴复制

可视化模式
vim -b /app/binary 打开二进制
:%!xxd打开16进制
%!xxd -r
v 选中
V行 y d 结合使用
ctrl +v

first
next
打开多个vim
vim -o f1 f2 上下
vim -O f1 f2 左右
wqall 全盘退出
ctrl +w +方向键 可以选择
vim ~/.vimrc 设置set nu 加行数
set nu
set nonu
vim 搜索 / h jfskjriwj
:set ic 大小写
:set ai 上行一样
:set list ^i $ 显示符号
:set fileformat=unxi

:set fileformat=dos
file win.txt

vim 作业:br/>1、@^[[:space:]]\+i@@
2、:%s@(^[[:space:]].*)@#\1@
4、:%s@^#[[:space:]]{1,}@
3、:%s@/etc/sysconfig/init@/etc/sysconfig/init@

脚本
[root@centos /app]#vim py.sh 解释器
#!/usr/bin/python
print "hello world"
sha #bang !
头文件执行自己:
#!/bin/rm -f t.sh
echo “hello world ”
[root@centos /app]# ./t.sh 注释 相当于执行/bin/rm -f t.sh
echo $SHELL
默认就是shell

脚本centos 6 默认脚本 :
[root@centos /app]#vim /etc/rc.d/rc.sysinit

[root@centos /app]#cat t.sh
#!/bin/rm -f
echo "hello world"

[root@centos /app]#./t.sh

[root@centos /app/scripts]#vim txt.sh
#!/bin/bash

for n in {1..10};do
if [$n -1e 5];then
useradd user$n
else
useradd -s /sbin/nologin user$n
fi
done

bash -n 语法
bash -x 调试

环境变量

[root@centos /app]#export q=1
[root@centos /app]#env |grep -w ^q
q=1
set :局部变量 env:全局变量
[root@centos /app]#r=1
[root@centos /app]#export r=3
[root@centos /app]#env |grep -w ^r
r=3

df 导出磁盘利用率:

[root@centos ~]#df |grep "/dev/sda" |egrep -o "[0-9]{1,3}%"|egrep -o "[0-9]{1,3}"|sort -nr
23
15
1

批量处理文件
file-f f1
newsgrp passwd (格式)
echo u:wang > delacl.txt
[root@centos /app]#echo g:opts >>delacl.txt
[root@centos /app]#setfacl -X delacl.txt d2

[root@centos /app]#echo u:wang:rwx >wang.acl
[root@centos /app]#cat wang.acl
u:wang:rwx
[root@centos /app]#setfacl -R -M wang.acl d2

第二周Linux重点内容

标签:第二周重点内容

原文地址:http://blog.51cto.com/13718453/2120898

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!