Linux网络安全精要 William Bo Rothwell

2.使用命令行

  • 文件系统层次标准(FHS)是一个文件和目录在Unix和Linux操作系统上面应该如何存储的定义。
    • /bin 重要的二进制可执行程序
    • /boot 与系统启动有关的文件
    • /etc 系统配置文件
    • /home 普通用户家目录
    • /lib 重要的系统库
    • /media 可移动介质的挂载路径
    • /mnt 临时的挂载路径
    • /opt 可选择安装的软件包
    • /proc 与系统内核及进程有关的信息(虚拟的文件系统)
    • /root root用户家目录
    • /sbin 重要系统的二进制可执行程序
    • /tmp 临时文件的存放路径
    • /usr 一个有许多子目录的路径,里面包含了二进制可执行文件、库文件以及文档
    • /usr/bin 不太重要的二进制可执行文件
    • /usr/lib /usr/bin中可执行文件的库
    • /usr/sbin 不太重要的系统二进制可执行文件
    • /usr/share 与系统相对独立的一些数据
    • /var 经常变化的数据
    • /var/mail 邮件的日志
    • /var/log 缓存池数据(比如打印池)
    • /var/tmp 临时文件
  • file命令输出文件内容的类型
  • 查看文档命令 less more head tail
  • 当用户登录系统时,会启动一个login Shell。用户登录后启动的Shell,被称为non-login Shell
    • login Shell会执行初始化文件
      • /etc/profile,
      • ~/.bash_profile[.bash_login[.profile]]没有找到依次查找这三个,
      • ~/.bashrc,此文件是作为每个用户可以放置代码的地方
      • /etc/bash.bashrc,作为系统管理员放置代码的地方
    • non-login Shell启动
      • ~/.bashrc
      • /etc/bash.bashrc
  • 别名:alias 。取消别名:unalias
  • history
    • !84 : 表示执行history第84号执行历史
    • !-10:执行倒数第十条
  • nl 输出行号
  • 标准错误输出到file:cmd 2> file
    • 标准错误和标准输出同时输出到指定文件:cmd &> file(cmd >file 2>&1)
  • $() : echo “junno $(date)”
  • find [PATH] [option] [action]
    • -maxdepth n:限制每次搜索的目录的深度
    • -mount防止搜索作为挂载点使用的目录
    • -regextype type:当使用正则表达式时,此选项指定了使用的正则表达式的类型。默认emacs
    • 条件选项
      • -amin n:基于访问时间匹配文件;-amin -3将会匹配过去三分钟之内访问过的文件
      • -group name:匹配组为name的文件
      • -name pattern:匹配文件名为pattern的文件或者目录,可以是正则
      • -mmin n:基于修改时间匹配文件
      • -nogroup:匹配无有效属组的文件
      • -nouser:匹配无有效属主的文件
      • -size n:基于文件大小匹配。+表示大于 -表示小于,单位 c字节 k千字节 M兆字节 G千兆字节
      • -type fstype:d目录 p管道 f普通文件 l 链接文件
      • -user username:匹配文件属主是username的所有文件
      • -perm mode 权限查找
        • -perm 0744 查找权限刚好等于0744
        • -perm -0744 囊括权限744的文件
        • -perm /744 权限包含744中任意一个的的文件
      • -mtime[atime|ctime] 以mtime为例,列出指定的那天内修改的文件
        • -mtime 4 列出4天前5天内修改的文件[4,5]
        • -mtime +4 列出5天前修改的文件[,5]
        • -mtime -4 列出4天内修改的文件[4,]
      • newer file 列出比file还要新的文件
    • 操作选项:{}字符用来表示在find命令中匹配到的文件名
      • -delete删除所有匹配到的文件
      • -exec command 在每个匹配到的文件执行这个命令 。find / -maxdepth 3 -mount -nouser -type f -exec file {} ;
      • -ls 列出每个匹配到的文件的细节
      • -ok 在每个匹配到的文件上执行某个命令,但是在每次执行之前都会提醒用户。find / -maxdepth 3 -mount -nouser -type f -ok file {} ;
      • -print 输出每个匹配到的文件的文件名,默认操作
  • grep
    • c 显示匹配到行的数目
    • e 使用多模式 。grep -e pat1 -e pat2 file
    • i 忽略大小写
    • l 显示匹配到的文件名而不是显示文件能匹配到的每一行。grep -rl ‘root’ /etc/
    • r 递归地搜索一个目录结构
    • v 反向匹配
    • E 使用扩展正则表达式
  • tar:tar -czv -f etc.tar.gz /etc
    • c 创建一个.tar文件
    • t 列出一个.tar文件的内容
    • x 提取一个.tar文件的内容
    • f 指定.tar文件的名称
    • v 输出详细信息
    • A 追加新文件到已经存在的.tar文件中
    • d 比较.tar文件与目录文件的不同
    • u 更新,只向存在的.tar文件中追加新文件
    • z 使用gzip,gz
    • j 使用bzip2压缩或解压缩一个.tar文件,bz2
    • J 使用xz,xz
    • C 解压指定文件夹
    • p 保留备份文件原来的权限与属性,如备份/etc需要
    • P 使用绝对路径(此时会存在风险,如果解压缩,当内容发生变化时,会覆盖文件内容),默认为相对路径,当使用绝对路径会报错(tar: Removing leading/’ from member names)`
  • gzip
    • c 压缩 gzip -c juju > juju.gz
    • d 解压缩文件,相当于gunzip
    • r 递归压缩,不会合并文件,而是会创建多个压缩文件
    • v 显示压缩进度的百分比
  • bzip2:c d v
  • xz:c d v,l 列出已经存在的压缩文件的信息
  • 变量内容的删除、取代与替换
    • 从前往后删除:#:符合匹配【最短的】哪一个;## 符合匹配【最长的】哪一个
    • 从后往前删除:%:符合匹配【最短的】哪一个;%%:符合匹配【最长的】哪一个
    • 替换: /:替换第一个匹配,//:替换所有匹配
# 从前往后删除:#
[root@bogon ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
[root@bogon ~]# echo ${PATH#/*bin:}
/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
[root@bogon ~]# echo ${PATH##/*bin:}
/usr/bin

# 从后往前删除:%
[root@bogon ~]# echo ${PATH%:*bin}
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin
[root@bogon ~]# echo ${PATH%%:*bin}
/root/.local/bin

# 替换:/
[root@bogon ~]# echo ${PATH/bin/hello/}
/root/.local/hello/:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
[root@bogon ~]# echo ${PATH//bin/hello/}
/root/.local/hello/:/root/hello/:/usr/local/shello/:/usr/local/hello/:/usr/shello/:/usr/hello/

# 变量测试与内容替换:
username=${usernames-roots}$usernames 不存在,则赋值-后的roots
username=${usernames:-roots}$usernames 为空或者不存在,则赋值-后的roots
username=${usernames+roots}$usernames 为空或存在,则赋值+后的roots
username=${usernames:+roots}$usernames 为非空字符串,则赋值+后的roots
  • 字符转换命令
    • tr 替换和删除字符
      • last |tr [a-z] [A-Z] 小写替换为大写
      • last |tr -d ‘rot’ 删除rot三个字符
    • col 将tab键转换为空格
      • col -x
    • join 关联数据相同的行
      • join -t “:” -1 4 /etc/passwd -2 3 /etc/group | head -n 3
      • 以:分割数据 -1 4 -2 3(第一个文件以第四栏和第二个文件的第三栏比较 相同关联)
    • paste 将两行贴在一起,且中间以【Tab】键隔开
      • "-"表示标准输入 -d 分割符
      • cat /etc/group|paste /etc/passwd /etc/shadow - |head -n 3
    • expand 将[tab]换成空格键
      • t指定一个tab换几个空格键,但不是严格的换,会为了排版增加或减少
      • unexpand 转换空格为tab ;unexpand -t 6 -
  • spilt 文件分割
    • 文件分割,l以行分割,b以大小分,单位b k m
    • 分割文件abc.txt,以10行为一个文件 lxq为文件前缀
    split -l 10 abc.txt lxq
    
    • 分割文件001.sql,以10m为一个文件 lxq为文件前缀
    split -b 10m 001.sql lxq
    
    • 文件合并 cat lxq* >> 001.sql
  • 减号[-] 替代stdout或stdin。
    • tar -xvf - /home | tar -xvf - -C /tmp/homeback
  • bash
    • -n 不要执行脚本,仅查询语法问题
    • -v 执行脚本前,将脚本文件的内容输出到屏幕上
    • -x 将使用到的脚本内容显示到屏幕上

3.获取帮助

  • man
# 列出passwd在man中的所有分类
man -f passwd

# 查看passwd指定章节
man 1 passwd
man 5 passwd

# 搜索含有password的命令
man -k password
  • mandb:建立man查看的数据库
  • help -s:获取所有Shell内置命令的列表
  • info
    • n 移动到下一个节点
    • p 移动到上一个节点
    • u 移动到父节点
    • l 移动到最近的节点
    • b 移动到当前节点的开始位置
    • t 移动到所有节点的顶部
    • q 退出info命令

4.编辑文件

vi or vim
  • M 移动到屏幕中部,H 移动到屏幕顶部, L 移动到屏幕底部
  • $ 移动到尾 0 移动到行首
  • w 移动一个单词或下一个标点的开头,b 向前移动
  • W 移动到下一个单词开头,B 向前移动
  • e 移动到下一个单词或标点符号结尾,E 移动到下一个单词结尾
  • (: 向后移动一句,
  • ):向前移动一句
  • [[:文档开头,gg
  • ]]:文档结尾,G
  • xG:跳转第x行,相当于 :x
  • 次数修饰符,5i 插入5次,3w 向后移动三个单词
  • 撤销操作:u:撤销,Ctrl+r 重做撤销
  • y:复制,yw 复制单词,yy 复制当前行,y$ 复制当前字符到行尾
  • d: 删除,dw 删除单词,dd 删除当前行,x 删除光标当前字符,X 删除光标前一个字符
  • p: 粘贴,单词为粘贴在光标之后,行为粘贴在光标下一行;P 粘贴在之前
  • 文本查找:"/" 向下查找 "?" 向上查找,n 查找下一个匹配,N查找上一个匹配
  • 查找和替换:":x,ys/pattern/replace/[g|i]",x-y行,g为替换所有,i为不区分大小写,可以使用"%"代替"1,$"指代全文本
  • 可视区块
    • v 字符选择,会将光标经过的地方反白选择
    • V 行选择,会将光标经过的行反白选择
    • v 字符选择,会将光标经过的地方反白选择
    • [Ctrl]+v 可视区块,可以用矩形的方式选择数据
    • y 将反白的地方复制起来
    • d 将反白的地方删除掉
    • p 将反白的地方复制到光标处
  • 多文件打开
    • :n 下一个文件
    • :N 上一个文件
    • :files 列出当前打开的文件
  • 多窗口 :sp filename
    • 向下切换窗口 ctrl+w +j[向下方向符]。按下ctrl不放,按住w,放开所有键,再按j或向下方向符切换窗口
    • 向上切换窗口 ctrl+w +k[向上方向符]
  • 关键词补全
    • 以目前文件内容为补全关键字 ctrl+x -> ctrl+n
    • 以当前目录文件名为补全关键字 ctrl+x -> ctrl+f
    • 以扩展名为补全关键字 ctrl+x -> ctrl+o
  • 环境设置 /.vimrc、/viminfo
    • :set nu|nonu 行号
    • :set hlsearch|nohlsearch 高亮
    • :set autoindent|noautoindent 自动缩进
    • :syntax on|off 依据程序相关语法显示颜色
other
  • dos2unix和unix2dos
    • k 保留文件原始的mtime
    • n 保留原来的文件 dos2unix -n old new
  • 语系编码转换 iconv -f 原来编码 -t 新编码 filename [-o newfile]
    • iconv -f big5 -t utf8 vi.big5 -o vi.utf8

5.故障处理

  • 故障处理步骤建议
    • 1.收集与问题相关的所有信息
    • 2.确定哪些是最可能导致故障的原因
    • 3.在采取行动之前,把计划用来解决问题的操作记录下来
    • 4.仅仅执行被记录下来的操作来解决问题
    • 5.确认问题是否被解决
    • 6.如果问题未被解决,使用步骤3的思路来回退系统状态,再从2~5开始解决问题
    • 7.如果解决了,再确认做完操作之后是否还有其他问题
    • 8.使用一种在将来很容易查询的技术把创建的文档保存下来。
    • 9.考虑一下还能做什么来防止这个问题再次发生
  • 通知用户
    • /etc/issue:本地登录前的欢迎信息
    • /etc/issue.net:网络telnet登录前时显示
    • /etc/motd:登录后的信息。可以把服务器变动加在里面
  • 广播消息:wall
  • 给指定用户发送消息,CTRL+D结束
# w
 23:09:06 up 12 days,  5:56,  4 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    1.1.1.1  15:33    1.00s  0.40s  0.00s w
root     pts/1    1.1.1.1  16:15    5:23m  0.11s  0.01s tail -f messages
root     pts/2    1.1.1.1  19:03    2:08   0.05s  0.05s -bash
root     pts/3    1.1.1.2  22:18    8.00s  0.08s  0.06s bash
[root@server-1 logrotate.d]# write root pts/1
hello
[root@server-1 logrotate.d]# write root pts/1
hello world
  • 关机:
    • shutdown -h +5 “System shutdown”
    • shutdown -c 取消关机
    • shutdown -r 重启
    • systemctl reboot|poweroff|halt

6.管理用户组账户

  • 显示用户信息:id work
  • 查看用户组:groups work
  • 修改文件用户组:chgrp groupname filename
  • 修改用户主组:newgrp groupname;newgrp会创建一个子shell,执行完成exit退出之后主组还原
    • 主组决定了创建的文件的属组
  • 创建用户组:groupadd -g 5000 payroll
  • 修改用户组payroll名:groupmod -n payables payroll
  • 删除用户组payables:groupdel payables
  • 将用户student添加到组adm:usermod -a -G adm student
    • -g 设置初始用户组
    • -G 设置次用户组,与-a配合为添加次用户组
  • 组管理员设置:gpasswd -A student games。组管理员只可以使用gpasswd来管理那些他们是组管理员的组
    • 添加用户bo到组:gpasswd -a bo games
    • 从组移除用户bo:gpasswd -d bo games

7.管理用户账户

  • 创建用户:useradd
    • c 设置/etc/passwd文件中的注释字段
    • d 设置用户家目录
    • e 设置账户过期时间,使用格式YYYY-MM-DD
    • f 设置过期多少天禁用此用户
    • g 设置用户主属组
    • G 设置用户附属组,多个用逗号隔开
    • p 指定用户密码,不安全,不建议使用
    • s 指定用户登录shell,如 -s /bin/tcsh
    • u 指定用户id
    • D 显示或者设定默认值。可以在/etc/default/useradd中修改
    • k 指定框架目录Skel,当使用Skel目录时,该目录的文件将会复制到新创建用户的家目录中
useradd -c "Hello lmm" -d /home/cslmm -e 2022-12-12 -f 33 -g share -G share1,share2,share3 -s /bin/false -u 3333 share4
  • 修改用户:usermod
usermod -c "Hello csq" -d /home/csq  share4
  • 管理GECOS
chgn share4

# grep share4 /etc/passwd
share4:x:3333:1001:csq,bj,1234444,345322:/home/csq:/bin/bash
  • 删除账户:userdel
    • r 会删除账户、家目录及邮箱。
  • 切换到用户:su - work
  • sudo ,使用visudo修改,它会检查基本的语法规则
    • /etc/sudoers
  • 限制用户账户
    • PAM

8.指定账户安全策略

  • Kali Linux

9.文件权限

  • 权限rwx
    • 对于目录:r表示可以列出目录文件;w表示可以在目录中增加或删除文件(需要x权限);x表示可以进入此目录或者在路径名中使用它
  • umask:用于指定创建新文件或目录需要屏蔽那些权限。新文件最大权限为666,不会赋予执行权限,目录最大为777
  • 特殊权限
    • SUID:4,u+s|u-s,设置之后运行程序使用可执行文件属主的权限访问文件
    • SGID:2,g+s|g-s,当设置在可执行文件上时,SGID允许程序使用可执行文件的属组的权限访问文件。在目录上设置时,目录的所有新文件都继承该目录的所属组的权限
    • SBIT粘滞位:1,o+t|o-t,当在目录上设置粘滞位时,目录中的文件只能由文件的属主、目录的所有者或者root用户才能删除
  • ACL:
    • mask :设置此掩码指定除文件属主和“其他人“之外的任何人对文件的最大权限,可以临时取消文件的除上述用户之外的不符合限制的权限
    • default:默认ACL,会继承,文件的最大为rw-
getfacl sales_report

setfacl -m mask:0 sales_report
setfacl -m default:g:games:6 sales_report
setfacl -m default:u:nobody:7 sales_report

setfacl -m u:work:rw sales_report
setfacl -m g:work:rw sales_report
  • 变更所有权
    • chown user:group xxxx
    • chgrp group xxxx
    • chmod
    chmod [u|g|o|a][+|-][r|w|x] file or dir
    chmod u+s file #SUID
    chmod g+s file or dir #SGID
    chmod o+t dir #SBIT
    
  • 文件属性:chattr lsattr
    • a 追加模式,只允许在文件尾部追加新数据
    • A 禁止修改访问时间戳atime
    • i 不可变。不能修改和删除文件
    • u 不可删除,但可以修改
    • e 区段格式,和SELinux有关
    • chattr +i xxx
    • chattr -i xxx
  • SELinux:可以在进程访问文件时提供一层额外的安全设置。可以使用SELinux安全策略要求进程必须属于某个SELinux安全上下文才能访问到指定的文件和目录
  • 文件时间
    • mtime修改时间:当文件内容数据变更时,就会更新这个时间 ls -l file
    • ctime状态时间:当文件状态改变时,就会更新这个时间 ls -l --time=ctime file
    • atime访问时间:当文件内容被读取时,就会更新这个时间 ls -l --time=atime file
    # touch 修改文件时间
    touch -a file # 仅修改文件atime
    touch -m file # 修改文件mtime
    touch -c file # 修改文件时间,包含atime和mtime
    touch -a -t 201406150202 aaa # 自定义修改时间,否则更新为当前时间
    

10.管理本地存储:基础

  • 分区管理:
    • MBR分区第一个扇区的512字节,存储如下两种数据:fdisk
      • 主引导记录MBR:可以安装启动引导程序的地方,有446字节
      • 分区表:记录整块硬盘分区的状态,有64字节。由于分区表仅有64字节,最多仅能有四组记录去,每组记录区记录了该区段的起始和结束的柱面号码。
        • 主要分区与扩展分区最多可以有4个(硬盘限制)
        • 扩展分区最多只有一个(操作系统限制)
        • 逻辑分区是由扩展分区持续划分出来的分区
        • 能够被格式化后作为数据存取的分区是主要分区与逻辑分区,扩展分区无法格式化
        • 逻辑分区的数量依操作系统而不同,在Linux系统上一般为63,SATA硬盘可超过63
    • GPT gdisk 或 parted
      • 没有扩展和逻辑分区的概念,可以看做全是主要分区,搭配UFFI
  • 创建文件系统:mkfs.*
    • mkfs -t ext4 /dev/sdb1
  • ext文件系统:分为多个区块群组(block group)每个区块群组都有独立的inode、数据区块、超级区块系统
    • 数据区块(data block):每个区块最多只能放置一个文件的数据,常用大小为4K
    • inode table:记录文件属性和区块地址,每个inode大小固定(128B或256B),每个文件仅占用一个inode。inode有12个直接区块,一个间接区块(每条区块记录为4B,以1kB data block为例,2561KB)、一个双间接区块(2562561KB)和一个三间接区块(2562562561KB)
    • 超级区块(Superblock):记录整个文件系统相关的信息:数据区块和inode用量、剩余、区块大小等。在区块群组中不一定都有超级区块,第一个区块群组里有,后面的区块群组里即使存在也只是备份
    • 文件系统描述说明(Filesystem Description):这个区段可以描述每个区块群组的开始和结束区块,以及每个区段(superblock、data block等)分别介于哪一个区块之间,使用dumpe2fs可查看
    • 区块对照表(block bitmap):记录文件区块是否为空
    • inode对照表(inode bitmap):记录inode表的使用与否
  • 目录:Linux在建立一个目录时,文件系统会分配一个inode与至少一块区块给该目录。其中,inode记录该目录的相关属性与权限,并可记录分配到的那块区块号码,而区块则是记录在这个目录下的文件名与该文件名占用的inode号码数据。
  • lsblk 列出系统磁盘列表
    • f 列出文件系统名称
    • d 仅列出磁盘本身,不列出分区数据
    • m 输出权限信息
    • p 输出设备完整名称,默认只展示最后的名字
    • t 列出磁盘的详细数据
  • blkid 列出设备的UUID等参数
  • parted 列出磁盘分区表与分区信息,parted /dev/sda print
  • du:对某个目录结构下磁盘空间使用率进行统计
  • df:显示分区和逻辑设备的使用情况
  • dd if=“input file” of=“output file” bs=“block size” count=“number”
# 列出文件系统类型
df -hT
# 列出文件下的总量
du -sh /usr/* 
# 列出系统磁盘列表
lsblk -pd
blkid  /dev/sda

dd if=/dev/zero of=/home/lx/test.img bs=4MB count=100
dd if=/etc/passwd of=/tmp/passwd.bak
  • ext系统相关工具
    • 修复文件系统错误:fsck.*
    • 显示ext文件系统信息:dumpe2fs
    • 修改文件系统配置:tune2fs
    • 允许在交互环境中执行操作:debugfs
  • xfs系列文件系统工具
    • xfsdump 备份
    • xfsrestore 从备份恢复
    • xfs_info 查看xfs系统的基础信息
    • xfs_check 检查文件系统问题
    • xfs_repair 修复文件系统问题
# 全量备份/boot。M:备份的session标头 ;L 存储媒介标头;l 备份等级 0 为完整备份。
xfsdump -l 0 -L boot_all -M boot_all -f /srv/boot.dump /boot
# 备份成功会有记录,使用xfsdump  -I读取
ll /var/lib/xfsdump/inventory/
# 查看备份结果
xfsdump  -I
# 新增测试文件
dd if=/dev/zero of=/boot/testing.img bs=1M count=10
# 增量备份
xfsdump -l 1 -L boot_2 -M boot_2 -f /srv/boot.dump1 /boot

# 还原备份
xfsrestore -f /srv/boot.dump -L boot_all /boot
# 还原备份到/tmp/boot
xfsrestore -f /srv/boot.dump -L boot_all /tmp/boot
# -s 指定还原文件或文件夹
xfsrestore -f /srv/boot.dump -L boot_all -s grub2 /tmp/boot

diff -r /boot /tmp/boot
  • 挂载 /etc/fstab
    • mount
    • umount
    • blkid:查看设备UUID
  • 创建交换设备
swapon -s
# 格式化交换分区
mkswap /dev/sdb1
# 启动交换分区
swapon /dev/sdb1
# 关闭交换分区
swapoff /dev/sdb1

# 创建交换文件
dd if=/dev/zero of=/var/test_swap bs=1M count=200
mkswap /var/test_swap
swapon /var/test_swap

11.管理本地存储:高级特性

  • 加密文件系统
# 创建物理分区 /dev/sdb2
fdisk /dev/sdb
# 创建一个LUKS格式密码
cryptsetup --verbose --verify-passphrase luksFormat /dev/sdb2
# 创建一个加密设备文件/dev/mapper/data,需要输入的密码和上述设置密码相同
cryptsetup luksOpen /dev/sdb2 data
 
fdisk -l
# 创建文件系统
mkfs.ext4 /dev/mapper/data
# 创建挂载点目录,可以把none更改为密码自动执行挂在过程,但是这样违背了文件系统加密的初衷
echo "data /dev/sdb2 none" >>/etc/crypttab 
echo "/dev/mapper/data /mnt/cslx ext4 defaults 1 2">> /etc/fstab 
# 重启系统,自动挂载加密文件,需要手动输入密码
  • autofs:当某个用户(或者任意进程)访问文件系统挂载点时自动挂载文件系统
  • lvm2
  • 磁盘配额:需要以usrquota挂在选项挂载文件系统
# 挂载
mount -o usrquota /dev/mapper/cslx-lv2 /mnt/lv2
# 初始化配额数据库
quotacheck -cugm /dev/mapper/cslx-lv2 
# 开启磁盘配额 关闭为 quotaoff
quotaon /dev/mapper/cslx-lv2 
# 创建或编辑磁盘配额
edquota junmo
# 查看指定用户的配额,无数据的话可去挂载目录下随意新建一个文件
quota -u junmo 
# 查看所用用户指定文件下的配额
repquota /mnt/lv2
*** Report for user quotas on device /dev/mapper/cslx-lv2
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
junmo     --      20    1000    2000              3  1000  2000    

12.管理网络存储

  • SAMBA
# 查看配置
sed '{/#/d}' /etc/samba/smb.conf
# 自定义共享目录
vim  /etc/samba/smb.conf
[doc]
	comment = System documentation
	path = /usr/share/doc
	guest ok = no
	browseable = yes
	writeable = no

# testparm检查配置错误
# 添加账户
smbpasswd -a student
# 查看共享
subclient -U student -L 192.168.3.104
# 连接服务器,如果连接失败(NT_STATUS_CONNECTION_REFUSED),可能是防火墙的问题,centos默认启动firewalld,关闭即可
subclient -U student  //192.168.3.104/doc
# 挂载共享,如挂载失败,显示cifs不存在,安装 apt install cifs-utils
mount -t cifs -o user=student //192.168.3.104/doc /mnt/doc
# /etc/fstab
## 所有用户都能查看/etc/fstab,使用第二种密码文件更安全
//192.168.3.104/doc /mnt/doc cifs user=student,password=123456 0 0
//192.168.3.104/doc /mnt/doc cifs credentials=/etc/samba/student 0 0
# /etc/samba/student 密码文件
user=student
password=123456
  • 网络文件系统NFS
  • ISCSI服务

13.指定存储安全策略

  • 备份
  • 备份工具
    • dd
    • tar
    • rsync

14.crontab和at

  • crontab系统任务,/etc/crontab和/etc/cron.d/*配置生效
    • /etc/cron.hourly
# 每分钟执行一次
* * * * * /bin/bash xxxx.sh

# 每5分钟执行一次
*/5 * * * * /bin/bash xxxx.sh

# 每小时第3分钟和第6分钟各执行执行一次
3,6 * * * * /bin/bash xxxx.sh

# 每小时第12分钟和第18分钟之间每分钟执行一次
12-18 * * * * /bin/bash xxxx.sh
  • anacron:对于非24小时运行的系统执行的crontab,以及因为某些原因导致的超时时间而没有被执行的任务anacron会控制其执行。anacron是由crond每小时执行一次的。
$ cat /etc/cron.d/0hourly
01 * * * * root run-parts /etc/cron.hourly

$ ls /etc/cron.hourly
0anacron # 每小时执行一次

$  cat /etc/anacrontab
...
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

# 表示此任务会在每天的3点-22点之间执行,延迟5分钟,如果系统繁忙,最多可延迟45分钟,
# 判断时间是否过了一天以文件/var/spool/anacron/cron.daily为准
1	5	cron.daily		nice run-parts /etc/cron.daily 

7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly
#period in days   delay in minutes   job-identifier   command
  • at 一次性任务
    • atq
    • atrm
$ systemctl status atd
# 时间格式:(04:00) (04:00 2015-07-22) (04:00 + 3 days|minutes|hours)
$ at 04:00
at> /usr/bin/sleep
at> <EOT> # Ctrl+D
job 2 at Sun Mar  6 04:00:00 2022

$ at -l
2	Sun Mar  6 04:00:00 2022 a root
  • batch 系统有空时才执行任务,它没有时间参数,使用at -l依然可查询任务
$ # batch
warning: commands will be executed using /bin/sh
at> sleep 300
at> <EOT>
job 7 at Sat Mar  5 19:32:00 2022

15-17 bash

  • https://blog.csdn.net/qq_31220203/article/details/122746486?spm=1001.2014.3001.5501

18-23 网络

  • DNS https://blog.csdn.net/qq_31220203/article/details/118058002?spm=1001.2014.3001.5502
  • 网络基础
    • https://blog.csdn.net/qq_31220203/article/details/122891756?spm=1001.2014.3001.5502
    • https://blog.csdn.net/qq_31220203/article/details/117638530?spm=1001.2014.3001.5502

24.进程控制

  • ps 无参数为列出当前shell所有子进程和shell本身
    • ps -e 列出系统上运行的所有进程
    • ps -ef 详细列出
  • pgrep:过滤指定进程
    • -G name 按组名称匹配进程
    • -n 优先显示最近启动进程
    • -l 显示进程名称和PID
    • -u name 按用户名称匹配进程
  • top
  • uptime:进程负载统计
  • free:内存统计
    • -s sec 每sec秒更新一次
  • 暂停和启动进程
    • jobs
    • bg %1
    • fg %1
  • kill %2 杀死任务2
  • pkill
    • -u name 按用户名杀死所有进程
    • -G name 按组杀死所有进程
  • killall firefox
    • -s signal
  • nohup
  • nice [-20,19]指定nice值启动任务 nice -n 5 nginx
  • renice 更改已存在任务的nice值 renice -n 10 -p 28235

25.系统日志

  • rsyslogd守护进程负责记录应用程序和系统事件。
    • -r 允许rsyslogd接受远程系统发送过来的日志
  • 主配置文件/etc/rsyslog.conf用于指定要创建那些日志条目,在/etc/sysconfig[|default]/rsyslog指定syslogd的选项
  • systemctl restart[start|stop] rsyslog.service
  • /var/log目录是rsyslogd守护进程保存日志文件的标准路径
  • 规则:/etc/rsyslog.conf
    • 日志来源,*
    • 日志等级:debug info notice warn err crit alert emerg *
    • 指定tcp或udp接收远程日志,module(load="imtcp") input(type="imtcp" port="514")
    • 指定日志输出:action(type="omfwd" Target="172.16.130.55" Port="514" Protocol="tcp")
module(load="imuxsock" 	  # provides support for local system logging (e.g. via logger command)
       SysSock.Use="off") # Turn off message reception via local log socket;
			  # local messages are retrieved through imjournal now.
module(load="imjournal" 	    # provides access to the systemd journal
       StateFile="imjournal.state") # File to store the position in the journal
module(load="imtcp") # needs to be done just once
input(type="imtcp" port="514")
global(workDirectory="/var/lib/rsyslog")
module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")
include(file="/etc/rsyslog.d/*.conf" mode="optional")
*.info;mail.none;authpriv.none;cron.none                /var/log/messages
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog # 有短横线-会批量写入
cron.*                                                  /var/log/cron
*.emerg                                                 :omusrmsg:*
uucp,news.crit                                          /var/log/spooler
local7.*                                                /var/log/boot.log

# action(type="omfwd" Target="172.16.130.55" Port="514" Protocol="tcp")
  • 使用logger发送日志给rsyslogd守护进程。etc : logger -T -n 172.16.130.55 -P 514 -p local7.warn -t lxq “hello cslxq”

    • T tcp
    • n 远程主机
    • P 端口
    • p 日志来源和等级
    • t 指定tag
  • logrotate 日志备份及定期删除,/etc/logrotate.conf

# cat /etc/logrotate.d/nginx
/var/log/nginx/*log {
    create 0664 work work
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}
  • journalctl处理日志记录过程。使用journalctl可查看systemd日志条目

26.基于Red Hat的软件包管理

  • rpm软件包查询
    • rpm -qa 查看所有已安装的软件包
    • rpm -qi nginx 查看nginx软件包的基本信息
    • rpm -qc nginx 查看nginx软件包的配置文件
    • rpm -qd nginx 查看nginx软件包提供的文档
    • rpm -qd nginx 查看nginx软件包提供的文档
    • rpm -qf /etc/nginx/uwsgi_params.default 查看文件来源
    • rpm -qR nginx 查看软件包依赖什么
    • rpm -q --whatprovides “/bin/bash” 查看某个依赖是由那个软件提供的
    • rpm -q --whatprovides “/bin/bash” 查看某个依赖是由那个软件包提供的
    • rpm -q --whatrequires “/bin/bash” 查看某个依赖是有哪些软件包在使用
    • rpm -q --provides bash-4.4.20-3.el8.x86_64 查看软件包提供什么依赖
    • rpm -qa --last 按安装日期的顺序显示已安装的包
    • rpm -qip package.rpm 查看未安装软件包的信息
    • rpm -qa --queryformat ‘%{name} %{size}\n’|sort -n -k 2 -r|awk ‘{print $1,$2/1024/1024}’|head -10 查看安装包最大的10个
  • rpm 安装包
    • rpm -i package.rpm :如果没有安装上一个版本的包,则安装该软件包,如果安装了,则安装第二个版本的包(可能冲突)
    • rpm -U package.rpm :如果没有安装上一个版本的包,则安装该软件包,如果安装了,则软件包更新到新版本
    • rpm -F package.rpm :如果没有安装上一个版本的包,则无任何操作,如果安装了,则软件包更新到新版本
    • rpm -ih package.rpm:h选项会展示安装进度
    • 签名验证:
      • rpm --import /location/key-file 将签名引入RPM数据库里
      • rpm -K xxx.rpm 校验签名
      • rpm -qa gpg-pubkey* 列出当前RPM数据库里的所有签名
  • rpm 删除
    • rpm -e -v pkgname
    • rpm -e --test -vv pkgname : 测试删除软件包的影响,并没有删除
  • 恢复包被误删或修改的文件:rpm2cpio xxxx.rpm | cpio -idum
  • yum
    • yum list [installed|available|all]
    • yum group list
    • yum group info “System Tools”

27.基于Debian的软件包管理

  • dpkg
    • 查看当前已安装的所有软件包 dpkg -l
    • 查看某个包的详细信息 dpkg -s nginx
    • 查看包的文件列表 dpkg -L nginx
    • 查看某个文件是那个包提供的 dpkg -S /xxx/file
    • 查看未安装的软件包信息 dpkg -I xxxx.deb
    • 查看未安装的软件包内容(文件列表) dpkg -c xxxx.deb
    • 安装软件包 dpkg -i xxx.deb
    • 从软件包提取文件 dpkg -x xxx.deb
    • 删除软件包,保留其配置文件 dpkg -r pkgname
    • 删除软件包,包括所有的配置文件 dpkg -P pkgname
  • apt /etc/apt/sources.list
    • apt list --installed|–upgradable
    • apt show nginx
    • apt-get download nginx 只下载,不安装
    • apt remove nginx 删除软件包,保留配置文件
    • apt-get purge nginx 删除软件包,包含配置文件
    • apt-get check 检查所有软件包的依赖关系是否损坏
    • apt-get clean 清除源(仓库)缓存
    • apt-get autoclean 删除旧的软件包

28.系统引导

  • BIOS/UEFI阶段:计算机执行的第一个程序,加载bootloader程序
  • bootloader阶段:操作系统提供的一个软件程序,用于访问硬盘上的文件(特别是内核)并启动操作系统。通常存储在MBR(主引导记录)
    • GRUB 2,用于安装bootloader
    • 每个硬盘分区都有自己的启动扇区boot sector
    • 实际可启动的内核文件是放置到各分区的
    • 启动引导程序(bootloader):一个可读取内核文件来执行的软件,并将控制权传递给内核。
    • 启动引导程序只会认识自己的系统分区内的可启动的内核文件,以及其他启动引导程序
    • 启动引导程序可直接指向或是间接将管理权转交给另一个管理程序,多系统引导
  • 内核阶段:加载内核并将引导过程的控制权传递给内核
    • 内核由一个核心程序和一组较小的程序组成,这些较小的程序称为内核模块或LKM(可加载内核模块)
    • 内核模块LKM:/lib/modules
    • 内核镜像:将内核与模块合并为一个内核镜像文件,/boot/vmlinux-xxxx
    • 修改内核参数 sysctl -a;临时生效:sysctl -w net.ipv4.ip_forward=1 ; 永久生效:vim /etc/sysctl.conf , sysctl -p
  • 内核后阶段:systemd

30.踩点

  • nmap
    • nmap 192.168.3.103 扫描TCP端口 ,默认只扫描常用端口,大约2000个
    • nmap -sU 192.168.3.103 扫描UDP端口
    • nmap -p 1-65535 192.168.3.103 扫描所有TCP端口 ,默认只扫描常用端口,大约2000个
    • nmap -sV 192.168.3.105 查看服务版本
    • nmap -O 192.168.3.105 探测操作系统类型
    • nmap -Pn 192.168.3.0/24 探测一个网络
  • lsof
    • lsof -i
    • lsof -i:80
  • nc
    • -w 用于客户端在指定的超时时间后自动关闭
    • -6 启用IPv6
    • -k 保持服务器进程处于活动状态,即使在客户机断开连接之后也是如此,默认行为是客户机断开时停止服务器进程
    • -u 使用UDP连接,而不是默认的TCP
    • 交互:nc -l 33333 ;nc server 33333
    • 本地/home传输到服务器:nc -l 33333 | tar xvf - ; tar cvf - /home | nc server 33333

31.防火墙

  • iptables
# 展示规则 filter
iptables -nvL --line-numbers
iptables -I INPUT -s 192.168.3.106 -j DROP
iptables -t filter -F INPUT
# -p 指定协议
iptables -A INPUT -p icmp -j DROP
# -m 和 --dport --sport一起使用
iptables -A INPUT -m tcp -p tcp --dport 80 -j REJECT
iptables -A INPUT -p icmp -s 192.168.3.0/24 -j DROP

32.入侵检测

  • 网络工具
    • netstat 查看异常访问 异常监听端口
    • nmap 扫描服务,查看是否启动异常服务
    • tcpdump 抓包
  • 文件工具
    • 修改/etc/passwd 和/etc/shadow来创建后门。由于在shadow里没有设置密码,所以可以无密码登录,远程不能无密码登录,但是可以远程登录一个普通账户,再su - nncp 不需要密码。
    # /etc/passwd
    nncp:x:0:0:root:/root:/bin/bash
    # /etc/shadow
    nncp:::0:99999:7:::
    
    • 创建一个有SUID权限的程序来创建后门。例如 chmod u+s /usr/bin/vim,这样就可以使用vim来修改passwd和shadow文件获得root权限了
    • 校验文件变化,cmp比较文件和二进制文件,diff比较文本文件
    # 创建文件md5值,也可以使用变体sha1sum sha256sum sha512sum
    md5sum /etc/passwd > passwd.md5
    # 校验文件是否改变
    md5sum -c passwd.md5
    

33.其他安全任务

  • gpg 加密数据传输
# [host1]centos 
yum install pinentry
# [host1]生成秘钥对 csq csq@1234.com 
gpg --gen-key
# [host1]创建一个公钥文件
gpg --output pub_key_file --export "csq"
scp pub_key_file root@192.168.3.100:/root/
# [host2]在另一台主机上 导入公钥到GPG数据库
gpg --import pub_key_file
# [host2]加密数据
gpg --encrypt --recipient csq@1234.com data.txt
scp data.txt.gpg root@192.168.3.100:/root/
# [host1]解密数据
gpg --decrypt data.txt.gpg
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Linux for Developers: Jumpstart Your Linux Programming Skills (Developer's Library) by William "Bo" Rothwell English | 17 Apr. 2017 | ASIN: B06ZY85CPJ | 224 Pages | AZW3 | 5.37 MB Linux for Developers shows you how to start writing great code for Linux, whether you’re a Linux user with little or no coding experience, or an experienced Windows programmer. Leading IT trainer/author WilliamBoRothwell begins with a clear and up-to-date review of modern open source software, including the licensing arrangements and tradeoffs all developers need to understand. He presents essential skills for both Linux command line and GUI environments, introducing text editors and other tools for efficient coding. Building on this knowledge, Rothwell introduces scripting tools such as Bash, Python, and Perl, as well as traditional object-oriented programming languages such as Java, C++, and C. Finally, he presents a full section on the powerful Git version control system, teaching skills you can use in Linux and many other environments. Access Linux systems, use GUIs, and work at the command line Learn how Linux organizes files and navigate its filesystem Use basic developer commands such as gzip and grep Edit programs with vi and vim, and explore alternative editors Perform basic sysadmin tasks that developers often need to handle Compare Linux languages to choose the best one for each task Write Bash scripts that interact with users or other shell features Program with Python and Perl: flow control, variables, and more Understand Linux features related to building C, C++, and Java programs Stay on top of complex projects with GIT revision control Work in GIT: staging, committing, branches, diffs, merges, and patches Manage local and remote GIT repositories This guide’s modular coverage helps you quickly access whatever information you need right now.
Linux for Developers shows you how to start writing great code for Linux, whether you’re a Linux user with little or no coding experience, or an experienced Windows programmer. Leading IT trainer/author WilliamBoRothwell begins with a clear and up-to-date review of modern open source software, including the licensing arrangements and tradeoffs all developers need to understand. He presents essential skills for both Linux command line and GUI environments, introducing text editors and other tools for efficient coding. Building on this knowledge, Rothwell introduces scripting tools such as Bash, Python, and Perl, as well as traditional object-oriented programming languages such as Java, C++, and C. Finally, he presents a full section on the powerful Git version control system, teaching skills you can use in Linux and many other environments. Access Linux systems, use GUIs, and work at the command line Learn how Linux organizes files and navigate its filesystem Use basic developer commands such as gzip and grep Edit programs with vi and vim, and explore alternative editors Perform basic sysadmin tasks that developers often need to handle Compare Linux languages to choose the best one for each task Write Bash scripts that interact with users or other shell features Program with Python and Perl: flow control, variables, and more Understand Linux features related to building C, C++, and Java programs Stay on top of complex projects with GIT revision control Work in GIT: staging, committing, branches, diffs, merges, and patches Manage local and remote GIT repositories This guide’s modular coverage helps you quickly access whatever information you need right now.
ALL YOU NEED TO KNOW TO SECURE LINUX SYSTEMS, NETWORKS, APPLICATIONS, AND DATA–IN ONE BOOK From the basics to advanced techniques: no Linux security experience necessary Realistic examples & step-by-step activities: practice hands-on without costly equipment The perfect introduction to Linux-based security for all students and IT professionals Linux distributions are widely used to support mission-critical applications and manage crucial data. But safeguarding modern Linux systems is complex, and many Linux books have inadequate or outdated security coverage. Linux Essentials for Cybersecurity is your complete solution. Leading Linux certification and security experts WilliamBoRothwell and Dr. Denise Kinsey introduce Linux with the primary goal of enforcing and troubleshooting security. Their practical approach will help you protect systems, even if one or more layers are penetrated. First, you’ll learn how to install Linux to achieve optimal security upfront, even if you have no Linux experience. Next, you’ll master best practices for securely administering accounts, devices, services, processes, data, and networks. Then, you’ll master powerful tools and automated scripting techniques for footprinting, penetration testing, threat detection, logging, auditing, software management, and more. To help you earn certification and demonstrate skills, this guide covers many key topics on CompTIA Linux+ and LPIC-1 exams. Everything is organized clearly and logically for easy understanding, effective classroom use, and rapid on-the-job training.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值