第二週Linux重點內容

第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

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