15、xargs命令 将数据转换成命令行参数 -n 分组 一组几个 -i 以{}代替前面的结果 find . -type f -name "*.log"|xargs -i mv {} /home/oldboy/bbb/ #-i 可以让{}代替find查找的内容 -n 指定每行最大的参数量n 可也将标准输入的文本划分为多行每行n个参数 [root@mail ~]# cat tess.log asdfasdfasdfasdfasdf asdfasdfasdfasdf asdfasdfasf [root@mail ~]# xargs -n 1 < tess.log asdfasdfasdfasdfasdf asdfasdfasdfasdf asdfasdfasf [root@mail ~]# xargs -n 2 < tess.log asdfasdfasdfasdfasdf asdfasdfasdfasdf asdfasdfasf [root@mail ~]# xargs -n 3 < tess.log asdfasdfasdfasdfasdf asdfasdfasdfasdf asdfasdfasf 特殊案例:(文件名中有空格) [root@mail ~]# ll total 4 -rw-r--r-- 1 root root 50 Oct 25 14:56 tess.log -rw-r--r-- 1 root root 0 Oct 25 14:59 test aa.txt -rw-r--r-- 1 root root 0 Oct 25 14:59 test bb.txt [root@mail ~]# find . -type f -name "*.txt" |xargs rm rm: cannot remove ‘./test’: No such file or directory rm: cannot remove ‘aa.txt’: No such file or directory rm: cannot remove ‘./test’: No such file or directory rm: cannot remove ‘bb.txt’: No such file or directory [root@mail ~]# find . -type f -name "*.txt" -print0|xargs -0 rm -f [root@mail ~]# ll total 4 -rw-r--r-- 1 root root 50 Oct 25 14:56 tess.log 分行例子 [root@moban ~]# seq 10 1 2 3 4 5 6 7 8 9 10 [root@moban ~]# seq 10|xargs -n 2 1 2 3 4 5 6 7 8 9 10 [root@moban ~]# seq 10|xargs -n 5 1 2 3 4 5 6 7 8 9 10 [root@moban ~]# seq 10|xargs 1 2 3 4 5 6 7 8 9 10 [root@moban ~]# 17、chattr命令 -a 大多用于系统日志 -i 大多用于关键文件(passwd等) [root@mail ~]# chattr +i /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/inittab chattr +a test.log #设定文件只能增加内容,不能删除 追加的话 >不能用 >>可以 chattr +i test.log #锁定文件 chattr -a test.log 删除-a属性 chattr -i test.log 删除-i属性 18、lsattr命令 -R 递归查看目录属性 -a 显示所有文件包含隐藏文件 -d 查看目录的扩展属性 查看文件扩展属性 [root@mail ~]# lsattr -d 1111/ #查看目录属性 -----a---------- 1111/ [root@mail ~]# lsattr aaa1.html #查看文件属性 ----i----------- aaa1.html 总结:给目录(或者文件)加 i 或者e 属性,根据需求来定 ####################################################################################### 19、file命令 Linux文件类型属性 [-]普通文件 包括 纯文本文件(ASCII);二进制文件(binary);数据格式的文件(data);各种压缩文件 [d]目录文件 cd 命令进入的 [b]块设备文件 块设备文件 :就是存储数据以供系统存取的接口设备例如一号硬盘的代码是 /dev/hda1等文件。 [c] 字符设备 字符设备文件:即串行端口的接口设备,例如键盘、鼠标等等。第一个属性为 [s]套接字文件、这类文件通常用在网络数据连接。可以启动一个程序来监听客户端的要求,客户端就可以通过套接字来进行数据通信。最常在 /var/run目录中看到这种文件类型 [p] FIFO管道文件、也是一种特殊的文件类型,它主要的目的是,解决多个程序同时存取一个文件所造成的错误。FIFO是first-in-first-out(先进先出)的缩写。 [l]链接文件、类似Windows下面的快捷方式。第一个属性为 ,例如 [lrwxrwxrwx] Linx三种文件类型 数据格式文件 [root@moban ~]# file /var/log/wtmp /var/log/wtmp: data 二进制文件 [root@moban ~]# file /bin/ls /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=c5ad78cfc1de12b9bb6829207cececb990b3e987, stripped 普通文本格式 [root@moban ~]# file /etc/hosts /etc/hosts: ASCII text 显示文件类型 [root@localhost oneinstack]# file backup.sh backup.sh: Bourne-Again shell script, ASCII text executable [root@localhost oneinstack]# file * addons.sh: Bourne-Again shell script, ASCII text executable, with very long lines backup_setup.sh: Bourne-Again shell script, UTF-8 Unicode text executable backup.sh: Bourne-Again shell script, ASCII text executable config: directory include: directory init.d: directory install.log: ASCII text, with very long lines, with escape sequences install.sh: Bourne-Again shell script, ASCII text executable LICENSE: ASCII text options.conf: ASCII text pureftpd_vhost.sh: Bourne-Again shell script, ASCII text executable README.md: UTF-8 Unicode text, with very long lines reset_db_root_password.sh: Bourne-Again shell script, ASCII text executable src: directory ss.sh: Bourne-Again shell script, ASCII text executable tools: directory uninstall.sh: Bourne-Again shell script, ASCII text executable upgrade.sh: Bourne-Again shell script, ASCII text executable versions.txt: ASCII text vhost.sh: Bourne-Again shell script, ASCII text executable, with very long lines ####################################################################################### 20、md5sum命令 -c 从指定文件中读取md5校验值 进行校验 -t 文本模式读取,默认模式 --status 不输出任何信息,可以通过命令返回值来判断 [root@mail home]# md5sum p.sh 87aebcd4c64bcbc0efe29c410eb5d74b p.sh [root@mail home]# md5sum p.sh >md5.log [root@mail home]# cat md5.log 87aebcd4c64bcbc0efe29c410eb5d74b p.sh [root@mail home]# md5sum -c md5.log p.sh: OK [root@mail home]# echo test >> p.sh [root@mail home]# md5sum -c md5.log p.sh: FAILED md5sum: WARNING: 1 computed checksum did NOT match --status [root@mail home]# md5sum -c --status md5.log [root@mail home]# echo $? 1 用途: 当服务器备份完成后,顺便生成MD5文件,将备份的文件以及MD5文件传输到备份服务器,然后进行比对,来防止文件损坏 21、chown改变文件和目录的用户和用户组 22、chmod改变文件或目录的权限 r 4 w 2 x 1 [root@mail ~]# chmod 555 test.log -r-xr-xr-x 1 root root 8 Oct 25 15:25 test.log [root@mail ~]# chmod -x test.log [root@mail ~]# ls -l test.log -r--r--r-- 1 root root 8 Oct 25 15:25 test.log 没什么好记录的、理解就行 23 、muask命令 [root@mail ~]# umask -p umask 0022 [root@mail ~]# umask -S u=rwx,g=rx,o=rx 系统设定好的默认安全权限,基本上没啥用。 umask 033 放入到/etc/profile 将所有的用户umask设置为033 没什么鸟用 ####################################################################################### 24、cat命令 合并日志: [root@mail xuexi]# cat a.log b.log >ab.log cat 来实现一键主从同步 cat |mysql -u root -p'123456'<test.log < gawk NR表示行 分隔符为回车(RS) $ 表示取 引用 域(区域,列) $0表示一整行的内容 $1表示第一列 $2表示第二列 $3表示第三列 $NF表示最后一列 FS filed separator 区域分隔符 awk内置变量 awk -F NF number of fileds 列的数量,一行有多少列(区域) FS 支持正则表达式 [root@moban ~]# awk 'NR<=2{print $0}' passwd [root@moban ~]# awk -F ":" 'NR>=2&&NR<=5{print $1,$3}' passwd awk print里 $0 代表整行内容 取出ssh登陆失败的ip [root@web ~]#awk '$6~/Failed/{print $11}' /var/log/secure [root@web ~]#grep Failed /var/log/secure | awk '{print $(NF-3)}' | sort -rn | uniq -c | awk '{print $2 "=" $1}' 取出nginx访问日志里的url [root@localhost wwwlogs]# awk -F "GET|HTTP" '{print $2}' a391.shangtua.com.log 取出第4-8行 [root@localhost home]# awk 'NR==4,NR==8' passwd -F 指定分割字段 []多分隔符 awk -F '[ :]+' 代表取空格和: +代表取多次 [root@moban home]# cat 1.txt linyaohong 1 2 3 , 5 6 [root@moban home]# awk -F "[ ,]" '{print $7}' 1.txt 5 ,和空格之间也算一列 -v 定义或修改一个awk内部的变量 [root@localhost home]# awk 'NR==3,NR==6 {print NR,$0}' passwd 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync [root@localhost home]# NR表示行号 $0表示一整行的内容 取出第一列和最后一列: [root@localhost home]# awk -F ":" '{print $1 $NF}' passwd $1表示第一列 $2表示第二列 $3表示第三列 $NF表示最后一列 centos7取出ip [root@localhost home]# ifconfig ens32|awk -F " " 'NR==2{print $2}' 41、uname命令 [root@localhost ~]# uname -r 3.10.0-862.el7.x86_64 [root@localhost ~]# uname -a Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [root@localhost ~]# uname -a|awk '{print $1}' Linux 41、hostname命令 [root@test-web ~]# vim /etc/sysconfig/network centos 6 修改主机名 [root@test-web ~]# vim /etc/hostname centos 7 修改主机名 [root@test-web ~]# hostname A 将主机名称改为A 退出重新登录生效,重启服务器失效 [root@test-web ~]# hostname -i 查看IP fe80::5a18:6160:f059:1800%ens32 120.220.14.253 [root@test-web ~]# hostname -I 查看IP 120.220.14.253 41、du命令 [root@VM_0_12_centos home]# du -sh /home/ [root@VM_0_12_centos home]# du -h -d 1 /home/ [root@VM_0_12_centos home]# du -h -d 2 /home/ [root@centos7 home]# du -sh test.disk [root@centos7 home]# du -sh oneinstack [root@web home]# du -d 1 -h 8.0K ./tools 20K ./blockip 186M . [root@web home]# du -s 190256 . [root@web home]# du -sh 186M . [root@web home]# du -h -d 1 /usr/local/ centos 7 [root@web home]# du -h --max-depth=1 /usr/local/ centos 6.0 41、date命令 这里记住T和F 就行了 [root@test-web home]# echo $(date +%T) 16:11:44 [root@test-web home]# echo $(date +%F -d "yesterday") 2018-11-13 [root@test-web home]# echo $(date +%F) 2018-11-14 [root@test-web home]# echo $(date +%F -d "-1day") 2018-11-13 [root@test-web home]# echo $(date +%F -d "+1day") 2018-11-15 [root@test-web home]# date +%F\ %T \ 这里\空格 才可空格,具体不知道什么原理,反正就这么写 2018-11-14 16:16:06 [root@test-web home]# date +%Y-%m-%d\ %H:%M:%S 2018-11-14 16:20:56 41、echo命令 -n 不自动换行 -E 不解析转义字符 (默认参数) -e 支持转义字符 \t 一个tab键 [root@moban ~]# echo -ne "lin";echo "yao" linyao [root@moban ~]# echo -ne "lin\t";echo "yao" lin yao [root@test-web home]# echo 'abc!' abc! [root@test-web home]# echo "abc!" -bash: !": event not found " [root@test-web home]# echo 'abc\!' abc\! [root@test-web home]# echo "abc\!" abc\! 输出颜色 [root@test-web home]# echo -e "\033[31m 红色字体 test \033[0m" 红色字体 test [root@test-web home]# echo -e "\033[32m test \033[0m" test [root@test-web home]# echo -e "\033[33m test \033[0m" test [root@test-web home]# echo -e "\033[34m test \033[0m" test [root@test-web home]# echo -e "\033[35m test \033[0m" test [root@test-web home]# echo -e "\033[36m test \033[0m" test [root@test-web home]# echo -e "\033[37m test \033[0m" test 闪的字体31带表红色,5m不可调整 备注:这里调整31可以换字体颜色31-37 [root@test-web home]# echo -e "\E[31;5m必须输入1-4 \E[0m " 必须输入1-4 红字白底,白字闪烁 备注:这里调整41可以换背景颜色31-46 [root@test-web home]# echo -e "\E[41;5m必须输入1-4 \E[0m " 必须输入1-4 42、watch命令 可以 以全屏方式动态显示命令或程序的执行情况 -n 命令执行间隔 -d 高亮显示命令变动的地方 -t 关闭顶部显示的时间间隔,命令以及当前时间的输出 42、which命令 显示命令的全路径(用来查询使用) [root@backup ~]# which mysql /usr/bin/mysql [root@backup ~]# which sh /usr/bin/sh [root@moban ~]# pwd /root [root@moban ~]# echo "echo welcome " >>lin [root@moban ~]# chmod +x /root/lin [root@moban ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/leanote/go/bin:/usr/local/leanote/gopackage/bin:/root/bin [root@moban ~]# export PATH=/root/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/leanote/go/bin:/usr/local/leanote/gopackage/bin:/root/bin [root@moban ~]# echo $PATH /root/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/leanote/go/bin:/usr/local/leanote/gopackage/bin:/root/bin [root@moban ~]# lin welcome 如上lin就被假如了环境变量,当做命令来执行 42、whereis命令 显示命令以及相关文件全路径 44、scp命令 远程复制 -C 压缩传输 -l 指定占用带宽 -P 指定端口 -p(小写):保持文件属性 -r 递归复制整个目录 [root@backup ~]# scp -r -P 521 php.zip root@120.220.14.253:/home/ 复制到远程机器 [root@backup ~]# scp -P 521 -r root@120.220.14.253:/etc/services /root/ 从远程机器复制 45、useradd命令 Linux服务器结合动态密码访问认证 活动目录AD LADP服务集中管理账号密码 -c comment 给新用户添加备注 -d home_dir 为主目录指定一个名字(如果不想用登录名作为主目录名的话) -e expire_date 用YYYYY-MM-DD格式指定一个账户过期的日期 -f inactive_days 指定这个帐户密码过期后多少天这个账户被禁用;0表示密码一过期就立即禁 用,-1表示禁用这个功能 -g initial_group 指定用户登录组的GID或组名 -G group ... 指定用户除登录组之外所属的一个或多个附加组 -k 必须和-m一起使用,将/etc/skel目录的内容复制到用户的HOME目录 -m 创建用户的HOME目录 -M 不创建用户的HOME目录(当默认设置里指定创建时,才用到) -n 创建一个同用户登录名同名的新组 -r 创建系统账户 -p passwd 为用户账户指定默认密码 -s shell 指定默认登录shell -u uid 为账户指定一个唯一的UID 详细可查看例子: 当创建用户的时候会在这四个文件里分别写入(根据 /etc/login.defs) [root@moban ~]# useradd lin [root@moban ~]# grep -w lin /etc/passwd lin:x:1001:1001::/home/lin:/bin/bash [root@moban ~]# grep -w lin /etc/group lin:x:1001: [root@moban ~]# grep -w lin /etc/shadow lin:!!:17851:0:99999:7::: [root@moban ~]# grep -w lin /etc/gshadow lin:!:: 重要: [root@moban ~]# useradd -g www yao 添加用户yao并且加入wwww组 [root@moban home]# useradd -M -s /sbin/nologin hong 添加用户hong并且不创建家目录,而且禁止登陆 添加用户设置注释信息,UID指定806 用户归属为root sa成员 shell为/bin/sh设置家目录为tmp/test 过期时间为2018年11月18日 [root@moban home]# useradd -u 806 -s /bin/sh -c rsyncuser -G root,www -e "2018/08/18" -f 2 -d /tmp/test test [root@moban home]# grep -w test /etc/passwd test:x:806:1004:rsyncuser:/tmp/test:/bin/sh [root@moban home]# grep -w test /etc/group root:x:0:test www:x:1000:test test:x:1004: 解释: [root@moban home]# id test uid=806(test) gid=1004(test) groups=1004(test),0(root),1000(www) [root@moban home]# chage -l test Last password change : Nov 16, 2018 Password expires : never Password inactive : never Account expires : Aug 18, 2018 -e指定的用户过期时间 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7 [root@moban home]# tail -1 /etc/shadow test:!!:17851:0:99999:7:2:17761: 2就是用户过期停权的结果3 添加用户默认的参数(会有限读取这里面的内容) 如果useradd指定了参数,会覆盖下面的默认参数,不指定,根据配置文件来 [root@moban home]# cat /etc/default/useradd # useradd defaults file GROUP=100 依赖/etc/login.defs USERGROUPS_ENAB yes参数,如果为no则此处控制 HOME=/home 用户家目录 INACTIVE=-1 用户是否过期停权 -1表示不启用 EXPIRE= 用户终止日期,不设置表示不启用 设置成2018/11/18(创建账号的默认有效期) SHELL=/bin/bash 用户shell类型 SKEL=/etc/skel 创建用户从这里拷贝文件(默认只有相关环境变量) CREATE_MAIL_SPOOL=yes 创建mail文件 -D 参数 [root@moban etc]# useradd -D -e 2018/11/18 相当于编辑 /etc/default/useradd 的EXPIRE= 参数 用户登录的定义:包含创建用户,权限,用户家目录,等等 [root@test-web ~]# vim /etc/login.defs 46、usermod命令 修改用户信息 -d 修改用户家目录 参数和useradd一样,一个增加用户一个修改用户 47、userdel 删除用户 最好不删除,用usermod把用户改成 nologin -r 删除用户的同时,删除相关的所有文件 -f 强制删除,即使用户在线 48、groupadd命令 添加用户组 [root@moban etc]# groupadd aaa 48、groupdel命令 删除用户组 不能删除还有用户的组 49、passwd命令 -d 删除用户密码 [root@template ~]# passwd -d linyaohong Removing password for user linyaohong. -e 密码立即过期,下次登陆强制修改 [root@template ~]# passwd -e linyaohong Expiring password for user linyaohong. passwd: Success 设置用户7天内不能更改密码,60天以后必须修改密码,过期前10天通知用户,过期30天后禁止登陆 [root@template ~]# passwd -n 7 -x 60 -w 10 -i 30 linyaohong Adjusting aging data for user linyaohong. passwd: Success [root@template ~]# passwd -S linyaohong linyaohong PS 2018-11-19 7 60 10 30 (Password set, SHA512 crypt.) 一条命令设置密码: [root@template ~]# echo "111111"|passwd --stdin linyaohong 锁定用户不能登陆 [root@template ~]# passwd -l linyaohong Locking password for user linyaohong. passwd: Success 解锁用户准许登陆 [root@template ~]# passwd -u linyaohong Unlocking password for user linyaohong. passwd: Success 49、chage命令 修改用户密码有效期 -E 账号过期日 -l 显示账号有效信息 设置用户7天内不能更改密码,60天以后必须修改密码,过期前10天通知用户,过期30天后禁止登陆 [root@template ~]# chage -m 7 -M 60 -W 10 -I 30 linyaohong [root@template ~]# chage -l linyaohong Last password change : Nov 19, 2018 Password expires : Jan 18, 2019 Password inactive : Feb 17, 2019 Account expires : never Minimum number of days between password change : 7 Maximum number of days between password change : 60 Number of days of warning before password expires : 10 50、chpasswd命令 批量更新用户密码 添加10个用户,并且创建密码记录到pass.txt [root@moban manpages-zh-1.5.1]# echo $((RANDOM+10000000)) 10016804 [root@template ~]# echo www{01..10}|xargs -n 1 useradd [root@template ~]# echo www{01..10}:$((RANDOM+10000000))|tr " " "\n" >pass.txt [root@template ~]# cat pass.txt www01:10011291 www02:10013410 www03:10020148 www04:10014811 www05:10032692 www06:10011124 www07:10025920 www08:10031586 www09:10002815 www10:10006129 [root@template ~]# chpasswd < pass.txt 51、su命令 切换用户 su 用户 切换以后还是原来用户的环境变量 su -用户 切换以后使用切换后用户的环境变量【可取】 查看环境变量 [root@template ~]# env |grep -E "USER|MAIL|PWD|LOGNAME" USER=root MAIL=/var/spool/mail/root PWD=/root LOGNAME=root root直接在普通用户创建文件 常用在以普通用户来执行脚本(如果有需要) [root@moban ~]# whoami root [root@moban ~]# su - aaaaa -c 'touch a1' [root@moban ~]# ls -l /home/aaaaa/a1 -rw-rw-r-- 1 aaaaa aaaaa 0 12月 11 00:48 /home/aaaaa/a1 让普通用户执行脚本 su - aaaaa -c "/bin/sh /scripts/kill.sh" 52、visudo命令 [root@moban ~]# ls -l /etc/sudoers -r--r----- 1 root root 3971 12月 11 01:09 /etc/sudoers 注意权限一定为 440 相当于 [root@template ~]# vim /etc/sudoers [root@moban ~]# visudo -c 检查语法 visudo不需要检查语法 /etc/sudoers:ok [root@template ~]# visudo #Defaults requiretty #注释这个 root ALL=(ALL) ALL www ALL= (ALL) ALL 用户 用户管理的机器 临时拥有的用户角色 执行的命令 ## Allow root to run any commands anywhere root ALL=(ALL) ALL #linyoahong ALL=(ALL) /bin/ls, /sbin/useradd ## Allows members of the 'sys' group to run networking, software, #linyoahong ALL=(ALL) /bin/ls, /sbin/useradd ## Allows members of the 'sys' group to run networking, software, ## service management apps and more. # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS ## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL ## Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL 3种授权方式,第一个比较安全,可以给初级运维,第二个平时自己用 #########代表执行以下授权的命令不需要密码,而且没有权限执行其他命令########################## linyaohong ALL=(ALL) NOPASSWD: /bin/ls, /sbin/useradd, /bin/mkdir, /bin/rm #########代表执行所有的命令都不需要密码,而且拥有所有命令权限################################ linyaohong ALL=(ALL) NOPASSWD: ALL #########代表执行以下授权第一次需要输入用户密码,时间戳为5分钟,过期后仍然需要输入密码 用户自行清除时间戳 sudo -K #########################/var/db/sudo/lectured/lin linyaohong ALL=(ALL) /bin/ls, /sbin/useradd, /bin/mkdir, /bin/rm #########给组授权 ########第四种方式 %sa ALL=(ALL) NOPASSWD: /bin/ls, /sbin/useradd, /bin/mkdir, /bin/rm sudo -l 查看可以执行的命令 执行命令用 sudo 命令 总结: 如果用户比较多,可以用别名 1、第一种和第二种授权方式最常见 ALL大写 !取反 禁止 禁止的命令要放到最后,支持正则 \ 换行 visudo命令 在最后一行添加 Defaults logfile=/var/log/sudo.log 可以实现,记录普通用户使用sudo命令 php用户(比如www)即可使用sudo 来执行一些root权限的命令 开启日志审计 需要跳板机,用来记录登陆和记录命令 jumpserver 堡垒机 CrazyEye 堡垒机 53、id命令 [root@template ~]# id uid=0(root) gid=0(root) groups=0(root) [root@template ~]# id -g 0 [root@template ~]# id -G 0 [root@template ~]# id -gn root [root@template ~]# id -u 0 [root@template ~]# id -un root 54、w命令 -h 不显示前两行的标题 -u 忽略 -s 使用短格式输出 显示已登录用户信息 可以看到执行w命令及显示结果。 USER:显示登陆用户帐号名。用户重复登陆,该帐号也会重复出现。 TTY:用户登陆所用的终端。 FROM:显示用户在何处登陆系统。 LOGIN@:是LOGIN AT的意思,表示登陆进入系统的时间。 IDLE:用户空闲时间,从用户上一次任务结束后,开始记时。 JCPU:以终端代号来区分,表示在这段时间内,所有与该终端相关的进程任务所耗费的CPU时间。 PCPU:指WHAT域的任务执行后耗费的CPU时间。 WHAT:表示当前执行的任务。 55、who命令 -a 显示所有信息 -b 显示系统启动时间 -d 显示已退出的用户 -H 显示标题 -l 显示登陆进程 56、whoami命令 显示当前登陆的用户名 57、last命令 显示用户登陆列表 58、lastb命令 显示用户登录失败的记录 /var/log/btmp在这里面读取,文件是乱码 59、lastlog命令 显示所有用户的最近登录记录 fdisk /dev/vd* #根据上面命令显示的磁盘名称 //分区 mkfs.ext4 /dev/vdb1 #格式化 或者xfs格式 mount /dev/vdb1 /home #挂载 echo '/dev/vdb1 /www ext4 defaults 0 0' >> /etc/fstab 开机自挂载 61、fdisk命令 当硬盘大于2T用parted分区,并且需要将磁盘转换为GPT格式 小于2T使用fdisk命令 -l 查看分区情况 [root@template ~]# parted -l [root@template ~]# parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel gpt【输入部分】 Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? yes【输入部分】 (parted) mkpart primary 0 8000 (这里指定分区大小,可以自由比如分2个区,0 4096 下一次就是 4096 8200) 【输入部分】 Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? Ignore【输入部分】 (parted) p 【输入部分】 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 8590MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 8000MB 8000MB primary (parted) quit【输入部分】 Information: You may need to update /etc/fstab. 后面的挂载和fdisk一样 [root@template /]# ls /dev/sdb* /dev/sdb /dev/sdb1 [root@template ~]# mkfs.xfs /dev/sdb1 [root@template /]# mount /dev/sdb1 /parted/ [root@template /]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 8.0G 2.1G 6.0G 26% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 1014M 139M 876M 14% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/sdb1 7.3G 34M 6.8G 1% /parted fdisk命令非交互式分区 [root@template ~]# umount /dev/sdb1 先卸载刚才的挂载 [root@template ~]# parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p 【输入部分】 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 8590MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 8000MB 8000MB xfs primary (parted) rm 1 删除分区 (parted) p 查看 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 8590MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags (parted) quit 【输入部分】 Information: You may need to update /etc/fstab. 开始非交互式分区 parted /dev/sdb mklabel gpt yes #调整为GPT格式 parted /dev/sdb mkpart linux-swap 0 1024 Ignore #创建1G swap分区 不加单位为MB parted /dev/sdb mkpart primary xfs 1024 4096 Ignore #创建4G主分区 parted /dev/sdb mkpart primary xfs 4096 8192 Ignore #创建4G主分区 parted /dev/sdb mkpart logical xfs 8192 20G Ignore #创建逻辑分区 parted /dev/sdb mkpart logical xfs 20G 3000G Ignore #创建逻辑分区 parted /dev/sdb quit 查看分区 [root@template ~]# parted /dev/sdb p 创建文件系统 [root@template ~]# mkfs.xfs /dev/sdb1 [root@template ~]# mkfs.xfs /dev/sdb2 [root@template /]# mkdir sdb1 [root@template /]# mkdir sdb2 tune2fs命令 -l 查看文件系统信息 -c max-mount-counts 设置强制自检的挂载次数, 如 设置以后不检查 [root@moban ~]# tune2fs -c -1 /dev/sdb3 tune2fs 1.42.9 (28-Dec-2013) Setting maximal mount count to -1 查看磁盘文件系统 [root@moban ~]# dumpe2fs /dev/sdb3 [root@moban ~]# tune2fs /dev/sdb3 挂载 [root@template /]# mount /dev/sdb1 /sdb1/ [root@template /]# mount /dev/sdb2 /sdb2/ 查看挂载新信息 [root@moban ~]# cat /proc/mounts 加入开机自动挂载 [root@template ~]# tail -2 /etc/fstab /dev/sdb1 /sdb1 xfs defaults 0 0 /dev/sdb2 /sdb2 xfs defaults 0 0 检查 mount -a 查看系统超级快信息 [root@moban ~]# xfs_growfs /dev/sda1 meta-data=/dev/sda1 isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=262144, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 62、mkfs命令 创建linux文件系统 查看系统支持的文件系统 [root@moban ~]# ls /lib/modules/`uname -r`/kernel/fs binfmt_misc.ko.xz cifs ext4 gfs2 mbcache.ko.xz nls udf btrfs cramfs fat isofs nfs overlayfs xfs cachefiles dlm fscache jbd2 nfs_common pstore ceph exofs fuse lockd nfsd squashfs 上面分区的时候已经用过 创建文件系统 [root@template ~]# mkfs.xfs /dev/sdb1 [root@template ~]# mkfs.xfs /dev/sdb2 63、resize2fs命令 调整ext2/3/4文件系统大小 生产环境禁用(分区没规划好的时候可以调整),不学了,没什么卵用 64、fsck命令 检查并修复linux文件系统 Linux断电后或者重启故障,屏幕显示如下 ***AN error................ ***xxx ***xxx Give root password for maintenance (or type Control -D continue): 此时输入root用户密码,然后输入命令: (Repair filesystem) 1 # fssk -A 等待一段时间,带状态返回命令行,尝试重启系统 禁止正常开机的情况下使用fsck命令 65、dd命令 转换或复制文件(其他的暂不记了,学会生成文件这个) [root@template ~]# dd if=/dev/zero of=test.date bs=1M count=2 [root@template ~]# dd if=/dev/zero of=/root/swapfile bs=1M count=1024 2+0 records in 2+0 records out 2097152 bytes (2.1 MB) copied, 0.00143789 s, 1.5 GB/s [root@template ~]# du -h test.date 2.0M test.date 其他案例:光驱iso文件复制到linux硬盘 [root@template /]# dd if=/dev/cdrom of=centos7.iso 8554496+0 records in 8554496+0 records out 4379901952 bytes (4.4 GB) copied, 72.1711 s, 60.7 MB/s 此时iso文件就复制过来了! 案例: [root@template ~]# dd if=/dev/zero /dev/linyaohong bs=1M count=1024 [root@template ~]# mkfs.ext4 /dev/linyaohong [root@template ~]# mount -o loop /dev/linyaohong /root/hong/ [root@template ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 8.0G 2.5G 5.6G 31% / devtmpfs 478M 8.7M 469M 2% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 32M 457M 7% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 1014M 139M 876M 14% /boot tmpfs 98M 0 98M 0% /run/user/1000 /dev/sr0 4.1G 4.1G 0 100% /mnt tmpfs 98M 0 98M 0% /run/user/0 /dev/loop0 455M 2.3M 425M 1% /root/hong 上面的案例,生成一个硬盘并且挂载到hong目录 66、mount命令 挂载文件系统 [root@template /]# mount /dev/cdrom /mnt/ 没有指定it iso9660 但是mount命令会自动识别 mount: /dev/sr0 is write-protected, mounting read-only [root@template /]# ll -h /dev/cdrom lrwxrwxrwx 1 root root 3 Nov 20 13:52 /dev/cdrom -> sr0 [root@template /]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 8.0G 6.2G 1.8G 78% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sdb1 3.7G 16M 3.5G 1% /sdb1 /dev/sdb2 3.7G 16M 3.5G 1% /sdb2 /dev/sda1 1014M 139M 876M 14% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/sr0 4.1G 4.1G 0 100% /mnt 66、umount命令 卸载文件系统 -f 强制卸载 -l懒惰地卸载 [root@template mnt]# umount /mnt/ 因为在mnt目录底下,所以需要强制卸载,或者可以退出此目录,不加参数直接卸载 umount: /mnt: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) [root@template mnt]# umount -lf /mnt/ 67、df命令 显示文件系统inode [root@template mnt]# df -i -T 列出文件系统的类型 -h 格式显示磁盘使用情况,M、G 68、mkswap命令:创建分区 69、swapon命令:激活交换分区 79、swapoff命令:关闭交换分区 1、使用文件作为swap分区 [root@template ~]# dd if=/dev/zero of=/root/swapfile bs=1M count=2048 2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB) copied, 10.1536 s, 212 MB/s 创建swap分区 [root@template ~]# mkswap /root/swapfile Setting up swapspace version 1, size = 2097148 KiB no label, UUID=e5098508-aae6-424a-966f-cd8f782576c0 激活swap分区 [root@template ~]# swapon /root/swapfile swapon: /root/swapfile: insecure permissions 0644, 0600 suggested. [root@template ~]# free -h[没加这个之前是1G的swap分区] total used free shared buff/cache available Mem: 976M 82M 67M 6.7M 826M 718M Swap: 3.0G 0B 3.0G [root@template ~] echo "/root/swapfile swap swap defaults 0 0" >>/etc/fstab 关闭swap分区 [root@template ~]# swapoff /root/swapfile [root@template ~]# free -h total used free shared buff/cache available Mem: 976M 81M 68M 6.7M 827M 720M Swap: 1.0G 2、也可以新建磁盘分区作为swap分区 关闭所有swap分区 swapoff -a 查看swap分区 [root@template ~]# swapon -s Filename Type Size Used Priority /dev/dm-1 partition 1048572 0 -1 /root/swapfile 80、rsyn命令:刷新文件系统缓冲区 81:ps命令 82、pstree命令 -u 显示进程所属的用户 83、kill命令 kill -9 立即结束进程 84、pgrep命令 查找匹配条件的进程 [root@web402 ~]# ps -ef|grep rsync|grep -v grep|awk '{print $2}' 12140 [root@web402 ~]# pgrep rsync 12140 [root@web402 ~]# ps -ef|grep nginx|grep -v grep|awk '{print $2}' 15875 15876 15877 15878 15879 15880 [root@web402 ~]# pgrep nginx 15875 15876 15877 15878 15879 15880 显示指定用户的所有进程号 [root@web402 ~]# pgrep -u www 85、killall命令 killall 进程名来终止进程 多执行几次 -w 等等待所有被终止的进程死去 85、pkill命令 -u 杀死指定用户进程 -t 杀死指定终端的进程 [root@template ~]# w 16:58:31 up 3:05, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.1.195 13:52 7.00s 0.37s 0.00s w www01 pts/1 192.168.1.195 16:58 7.00s 0.01s 0.01s -bash [root@template ~]# pkill -u www01 86、nohup命令 用户退出系统进程继续工作 后台运行 [root@template ~]# nohup ping www.baidu & 在后台运行 [root@template ~]# tail -f nohup.out 会生成一个nohup.out PING www.baidu (123.129.254.12) 56(84) bytes of data. --- www.baidu ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data. 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=16.5 ms 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=17.0 ms 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.9 ms 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.7 ms 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=16.4 ms 87、strace命令(听说很重要,但是看不懂) 跟踪进程的系统调用 88、ltrace命令 跟踪进程调用库函数 [root@test-web web]# ltrace -p 6672 89、runlevel命令 输出当前运行级别 [root@template ~]# runlevel N 3 运行级别 0 关机 1 single user 单用户 2 multi-user.target 多用户没有NFS网络文件系统 3 文本模式 工作模式 4 unused 5 图形,桌面 X11 6 reboot 重启 90、init命令 [root@template ~]# init 0 # 关机 [root@template ~]# init 6 # 重启 91、service命令 [root@template ~]# service --status-all 显示所有服务状态(centos7好像有所改动,回头补记) 92、ifconfig命令 yum -y install net-tools [root@template ~]# ifconfig ens33|awk -F " " 'NR==2{print $2}' 192.168.1.10 [root@template ~]# ifconfig -a [root@template ~]# ifconfig ens33 94、ifup命令 [root@template ~]# ifup ens33 激活网卡 95、ifdown命令 禁用网络接口 [root@template ~]# ifdown ens33 (这个最好别用,会断远程连接) 96、route命令 显示或管理路由表 add 添加路由信息 del 删除路由信息 gw GW 为发往目标网络/主机的任何分组指定网关 [root@template ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 删除默认网关 [root@template ~]# route del default 查看route已经删除 [root@template ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 查看ping百度已经不通 [root@template ~]# ping www.baidu.com connect: Network is unreachable 添加默认网关 [root@template ~]# route add default gw 192.168.1.1 查看默认网关 [root@template ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 此时ping百度可以ping通 [root@template ~]# ping www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=17.0 ms 删除默认网关方法二: [root@template ~]# route del default gw 192.168.1.1 添加网关方法2,使用dev指明网络设备(适用于多快网络设备的主机) [root@template ~]# route add default gw 192.168.1.1 dev ens33 ############################################################################################ 添加路由(没成功,日后研究) [root@template ~]# route add -net 10.10.10.0/24 netmask 255.255.255.0 dev ens33 [root@template ~]# route del -net 10.10.10.0/24 dev ens33 ############################################################################################ 97、ip命令 暂时不学(回头做网络的时候学) 98、netstat命令 查看网络状态 l 列表 n以数字显示 u udp t tcp p显示进程名 常用一: [root@template ~]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 907/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1144/master tcp6 0 0 :::22 :::* LISTEN 907/sshd tcp6 0 0 ::1:25 :::* LISTEN 1144/master udp 0 0 127.0.0.1:323 0.0.0.0:* 650/chronyd udp6 0 0 ::1:323 :::* 650/chronyd 常用二: [root@template ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 常用三: [root@test-web ~]# netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens32 1500 6528273 0 213 0 12524598 0 0 0 BMRU lo 65536 42761902 0 0 0 42761902 0 0 0 LRU [root@test-web ~]# 正常情况下 456列都应该为0 否则的话网络质量有问题 ####需要做笔记,状态, [root@test-web ~]# man netstat ESTABLISHED The socket has an established connection. TIME_WAIT The socket is waiting after close to handle packets still in the network. 需要统计 看老男孩博客 优化 关键词 99、ss命令 查看网络状态 [root@test-web ~]# ss -lntu 显示socket统计 [root@test-web ~]# ss -s Total: 265 (kernel 590) TCP: 12 (estab 1, closed 1, orphaned 0, synrecv 0, timewait 1/0), ports 0 Transport Total IP IPv6 * 590 - - RAW 1 0 1 UDP 2 1 1 TCP 11 9 2 INET 14 10 4 FRAG 0 0 0 和netstat差不多吧,逐渐代替netstat命令 100、ping命令 -c 次数 -i 时间间隔 [root@test-web ~]# ping -c 5 -i 2 -s 1024 -t 255 www.baidu.com 101、traceroute命令 -n 直接使用ip不使用主机名 追踪数据传输路由状况 [root@template ~]# traceroute -I www.baidu.com traceroute to www.baidu.com (61.135.169.121), 30 hops max, 60 byte packets 1 gateway (192.168.1.1) 1.038 ms 0.964 ms 0.934 ms 2 112.233.112.1 (112.233.112.1) 2.740 ms 3.308 ms 3.283 ms 3 218.59.251.150 (218.59.251.150) 3.257 ms 3.231 ms 3.812 ms 4 61.156.156.141 (61.156.156.141) 18.053 ms 18.621 ms 18.610 ms 5 219.158.9.105 (219.158.9.105) 18.584 ms 19.192 ms 19.169 ms 6 * * * 7 124.65.63.146 (124.65.63.146) 17.722 ms 18.114 ms 17.987 ms 8 61.49.168.78 (61.49.168.78) 17.927 ms 17.854 ms 17.712 ms 9 * * * 10 61.135.169.121 (61.135.169.121) 16.902 ms 16.910 ms 16.727 ms 102、arping命令 [root@template ~]# arping -f 192.168.1.151 ARPING 192.168.1.151 from 192.168.1.10 ens33 Unicast reply from 192.168.1.151 [00:E0:70:81:03:13] 2.049ms Sent 1 probes (1 broadcast(s)) Received 1 response(s) 可以查看IP的mac地址 103、telnet命令 登陆远程主机(一般在windows下使用,查看端口是否开放) 104、nc命令 105、ssh命令 远程登陆主机 -p 端口 [root@template ~]# ssh -p 521 linyaohong@47.89.32.22 -t 远程sudo 执行命令 ssh -t hostname sudo 106、wget命令 命令行下载工具 -o 将执行的命令结果写入文件 -O 指定保存的文件名 后下载 -c 断点续传 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 监控url是否正常 [root@template ~]# wget -q -T 3 --tries=1 --spider www.baid1u.com [root@template ~]# echo $? 0 [root@template ~]# wget -q -T 3 --tries=1 --spider www.baid11123u.com [root@template ~]# echo $? 4 还可以模拟浏览器登陆 --user-agetn=************** 107、mailq命令 查看邮件队列 [root@template ~]# mailq Mail queue is empty #没有所以是空的 108、mail命令 发送和接受邮件 -s 指定邮件主题 -a 发送邮件附件 -b指定密件抄送的收信人地址 -c指定抄送收信人地址 [root@409web .mailqq]echo "邮件正文" | mail -s "邮件主题" -c "122123498@qq.com" "271537752@qq.com" [root@409web .mailqq]echo "邮件正文" | mail -s "邮件主题" -a 1.jpg -a 2.jpg -c "122123498@qq.com" "271537752@qq.com" 1) 无邮件正文 mail -s "主题" 收件地址 mail -s "测试" 1968089885@foxmail.com 2) 有邮件正文 mail -s "主题" 收件地址< 文件(邮件正文.txt) mail -s "邮件主题" 1968089885@foxmail.com < /data/findyou.txt echo "邮件正文" | mail -s 邮件主题 收件地址 echo "邮件正文内容" | mail -s "邮件主题" 1968089885@foxmail.com cat 邮件正文.txt | mail -s 邮件主题 收件地址 cat /data/findyou.txt | mail -s "邮件主题" 1968089885@foxmail.com 3) 带附件 mail -s "主题" 收件地址 -a 附件 < 文件(邮件正文.txt) mail -s "邮件主题" 1968089885@foxmail.com -a /data/findyou.tar.gz < /data/findyou.txt 108、dig命令 [root@moban ~]# yum install bind-utils 109、host命令 [root@moban ~]# host www.baidu.com www.baidu.com is an alias for www.a.shifen.com. www.a.shifen.com has address 61.135.169.121 www.a.shifen.com has address 61.135.169.125 [root@moban ~]# echo $? 0 110、nmap命令 [root@moban ~]# yum -y install nmap 默认扫描1000以内(书上写的是centos6/7做实验,都可以扫描出来) [root@moban ~]# nmap 10.10.10.101 Starting Nmap 6.40 ( http://nmap.org ) at 2018-11-22 15:45 CST Nmap scan report for 10.10.10.101 Host is up (0.00044s latency). Not shown: 994 filtered ports PORT STATE SERVICE 20/tcp closed ftp-data 21/tcp open ftp 22/tcp closed ssh 80/tcp open http 81/tcp open hosts2-ns 8888/tcp open sun-answerbook MAC Address: 00:0C:29:51:88:A9 (VMware) Nmap done: 1 IP address (1 host up) scanned in 5.13 seconds [root@moban ~]# nmap 10.10.10.101|grep -w 8888 8888/tcp open sun-answerbook 指定端口扫描是否开放 [root@moban ~]# nmap -p 1-30 10.10.10.101 扫描局域网内所有的ip [root@moban ~]# nmap -p 1-30 10.10.10.0/24 探测目标主机的服务器和操作系统版本 (所以针对服务器可以屏蔽版本号) [root@moban ~]# nmap -O -sV 10.10.10.101 Starting Nmap 6.40 ( http://nmap.org ) at 2018-11-22 15:49 CST Nmap scan report for 10.10.10.101 Host is up (0.0020s latency). Not shown: 994 filtered ports PORT STATE SERVICE VERSION 20/tcp closed ftp-data 21/tcp open ftp Pure-FTPd 22/tcp closed ssh 80/tcp open http nginx 81/tcp open http Apache httpd 8888/tcp open sun-answerbook? 1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at http://www.insecure.org/cgi-bin/servicefp-submit.cgi : SF-Port8888-TCP:V=6.40%I=7%D=11/22%Time=5BF65F7D%P=x86_64-redhat-linux-gnu SF:%r(GetRequest,119,"HTTP/1\.1\x20303\x20See\x20Other\r\nContent-Type:\x2 SF:0text/html\r\nLocation:\x20http://\[unknown\]/login\r\nContent-Type:\x2 SF:0text/html\r\nLocation:\x20http://\[unknown\]/login\r\nSet-Cookie:\x20B SF:T_PANEL=6a991f3c7a7af9514b8f049b6e0f81faab8fc3e6;\x20Path=/;\x20httponl SF:y\r\nDate:\x20Thu,\x2022\x20Nov\x202018\x2007:49:17\x20GMT\r\nServer:\x SF:20localhost\r\n\r\n")%r(HTTPOptions,D5,"HTTP/1\.1\x20405\x20Method\x20N SF:ot\x20Allowed\r\nContent-Type:\x20text/html\r\nAllow:\x20GET\r\nSet-Coo SF:kie:\x20BT_PANEL=76a21f5e2b6e1a176ad45eff7c88263220ae1e32;\x20Path=/;\x SF:20httponly\r\nDate:\x20Thu,\x2022\x20Nov\x202018\x2007:49:17\x20GMT\r\n SF:Server:\x20localhost\r\n\r\nNone")%r(FourOhFourRequest,208,"HTTP/1\.1\x SF:20404\x20Not\x20Found\r\nContent-Type:\x20text/html\r\nSet-Cookie:\x20B SF:T_PANEL=b9d76df77dab70b11228b7e0dfac55095529f9b4;\x20Path=/;\x20httponl SF:y\r\nDate:\x20Thu,\x2022\x20Nov\x202018\x2007:49:17\x20GMT\r\nServer:\x SF:20localhost\r\n\r\n\n\x20\x20\x20\x20\n\x20\ SF:x20\x20\x20404\x20Not\x20Found\n\x20\x20\x20\x20< SF:body>\n\x20\x20\x20\x20

\xe6\x8a\xb1\xe6\xad\x89,\xe9\xa1\xb5\xe9\x9 SF:d\xa2\xe4\xb8\x8d\xe5\xad\x98\xe5\x9c\xa8

\n\x20\x20\x20\x20\x20\x2 SF:0\x20\x20

\xe6\x82\xa8\xe8\xaf\xb7\xe6\xb1\x82\xe7\x9a\x84\xe9\xa1\xb SF:5\xe9\x9d\xa2\xe4\xb8\x8d\xe5\xad\x98\xe5\x9c\xa8,\xe8\xaf\xb7\xe6\xa3\ SF:x80\xe6\x9f\xa5URL\xe5\x9c\xb0\xe5\x9d\x80\xe6\x98\xaf\xe5\x90\xa6\xe6\ SF:xad\xa3\xe7\xa1\xae!

\n\x20\x20\x20\x20
\n\x20\x20\x20\x20\xe5\xae\x9d\xe5\xa1\x94Linux\xe9\x9d\xa2\xe6\x9d\xbf\x205\.x\x20\xe8\xaf\xb7\xe SF:6\xb1\x82\xe5\xb8\xae\xe5\x8a\xa9\n\x20\x20\x20\x20\n\x20\x20\x20\x20")%r(GenericLines,60,"HTTP/1\.1\x20400\x20Bad SF:\x20Request\r\nContent-Length:\x2022\r\nContent-Type:\x20text/plain\r\n SF:\r\nMalformed\x20Request-Line")%r(RTSPRequest,D5,"HTTP/1\.1\x20405\x20M SF:ethod\x20Not\x20Allowed\r\nContent-Type:\x20text/html\r\nAllow:\x20GET\ SF:r\nSet-Cookie:\x20BT_PANEL=401bc2ca8280145a6ca126f526403f5293ea6488;\x2 SF:0Path=/;\x20httponly\r\nDate:\x20Thu,\x2022\x20Nov\x202018\x2007:49:22\ SF:x20GMT\r\nServer:\x20localhost\r\n\r\nNone"); MAC Address: 00:0C:29:51:88:A9 (VMware) Aggressive OS guesses: Linux 3.0 - 3.9 (93%), Linux 2.6.22 - 2.6.36 (91%), Linux 2.6.39 (91%), Linux 2.6.32 - 3.9 (89%), Crestron XPanel control system (89%), Linux 2.6.26 - 2.6.35 (89%), Netgear DG834G WAP or Western Digital WD TV media player (89%), HP P2000 G3 NAS device (89%), Ubiquiti AirMax NanoStation WAP (Linux 2.6.32) (88%), Linux 3.0 (88%) No exact OS matches for host (test conditions non-ideal). Network Distance: 1 hop OS and Service detection performed. Please report any incorrect results at http://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 122.78 seconds 111、tcpdump命令 监听网络流量 -n 不进行DNS解析,加快显示速度 -nn 不将协议和端口数字等转换成名字 -q 以快速输出的方式运行, -i 指定网口 -c 指定数据包的数目后,退出命令 -A 以ASCII码的方式显示没一个数据包 以快速输出的方式运行 [root@moban ~]# tcpdump -q -i ens33 监听指定端口的数据包 [root@moban ~]# tcpdump -nn port 11 -i ens33 监听icmp数据包(可以用另外一个主机ping一下看到效果) [root@moban ~]# tcpdump -n icmp -i ens33 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 16:02:12.959002 IP 10.10.10.101 > 10.10.10.11: ICMP echo request, id 4973, seq 1, length 64 16:02:12.959312 IP 10.10.10.11 > 10.10.10.101: ICMP echo reply, id 4973, seq 1, length 64 ... .. . 监听指定协议的数据包 [root@moban ~]# tcpdump -n arp -i ens33 常见的协议关键字有 ip arp icmp tcp udp 多个过滤条件混合使用 [root@moban ~]# tcpdump -n ip host 10.10.10.101 and ! 10.10.10.1 -i ens33 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes 17:03:38.664108 IP 10.10.10.181.62837 > 10.10.10.101.ssh: Flags [P.], seq 3367889978:3367890030, ack 1170928080, win 16425, length 52 17:03:38.664273 IP 10.10.10.101.ssh > 10.10.10.181.62837: Flags [.], ack 52, win 251, length 0 17:03:42.406762 IP 112.233.117.29.58102 > 10.10.10.101.hostname: Flags [P.], seq 2572111893:2572111929, ack 3143152100, win 255, length 36 17:03:42.407075 IP 10.10.10.101.hostname > 112.233.117.29.58102: Flags [.], ack 36, win 266, length 0 ... .. . 利用tcdump抓包详解tcp/ip 连接和断开过程的案例 看不懂 Linux系统管理命令 112、lsof命令 查看进程打开的文件 [root@moban ~]# lsof /var/log/messages COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsyslogd 950 root 6w REG 253,0 169612 10063963 /var/log/messages 显示进程号打开的文件 [root@moban ~]# ps -ef|grep nginx root 1002 1 0 14:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf www 1005 1002 0 14:17 ? 00:00:00 nginx: worker process root 2387 1274 0 17:13 pts/0 00:00:00 grep --color nginx [root@moban ~]# lsof -p 1002 查看所有进程 [root@moban ~]# lsof -i 查看端口 [root@moban ~]# lsof -i:11 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1172 root 3u IPv4 23383 0t0 TCP *:systat (LISTEN) sshd 1172 root 4u IPv6 23385 0t0 TCP *:systat (LISTEN) sshd 1272 root 3u IPv4 24179 0t0 TCP moban:systat->112.233.117.29:58150 (ESTABLISHED) [root@moban ~]# lsof -i tcp 113、uptime命令 [root@moban ~]# uptime 17:18:31 up 3:01, 2 users, load average: 0.00, 0.01, 0.05 114、iftop命令 动态显示网络接口流量信息 -i 指定网卡 CentOS/RHEL 6 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm CentOS/RHEL 7 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm yum -y install iftop [root@340 ~]# iftop -nNBP TX:发送流量 RX:接受流量 TOTAL:总流量 Cum 运行iftop到目前时间的总流量 rates:过去2s 10s 40s 的平均流量 115、vmstat命令 虚拟内存统计 -p 指定磁盘分区统计信息 -S 使用指定单位 k K m M -t 统计带上时间戳 -a 显示活跃和非活跃内存 [root@moban ~]# vmstat 1 10 1秒1次 10次退出 [root@moban ~]# vmstat -S m procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 415 2 456 0 0 4 2 49 45 0 0 100 0 0 [root@moban ~]# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 2 0 0 405936 2108 445692 0 0 4 2 49 45 0 0 100 0 0 [root@moban ~]# free -h total used free shared buff/cache available Mem: 974M 142M 395M 7.7M 437M 644M Swap: 819M 0B 819M 116、mpstat命令 -P 指定cpu编号 0代表第一个 1代表第二个cpu [root@moban ~]# mpstat 1 10 (1秒1次 10次退出) [root@moban ~]# mpstat -P 0 1 Linux 3.10.0-862.11.6.el7.x86_64 (moban) 11/23/2018 _x86_64_ (1 CPU) 10:48:39 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 10:48:40 AM 0 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 99.00 10:48:41 AM 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 [root@moban ~]# mpstat 1 Linux 3.10.0-862.11.6.el7.x86_64 (moban) 11/23/2018 _x86_64_ (1 CPU) 02:29:16 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 02:29:17 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 02:29:18 PM all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00 CPU 为all代表所有cup 0表示第一个cpu %user 用户进程消耗的CPU时间百分比 %nice 改变过优先级进程消耗的cpu时间百分比 %sys 系统内存进程消耗的CPU时间百分比 %iowait IP等待所占用的CPU时间百分比 %irq 硬中断占用的cpu时间百分比 %soft 软中断占用的cpu时间百分比 %idle cpu处在空闲状态的时间百分比 116、iostat命令 I/O信息统计 包含mpstat里的部分信息 Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.41 0.00 0.11 0.02 0.00 99.46 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 2.94 7.59 56.06 11291687 83375759 dm-0 3.79 7.50 54.95 11146978 81718230 dm-1 0.00 0.00 0.00 2228 0 dm-2 0.04 0.01 1.11 16240 1655126 -d 显示磁盘使用情况 -c 显示cpu的使用情况 -k 每秒kb显示数据 -m 每秒mb显示数据 -p device 指定磁盘 -x 显示扩展统计 -p效果 [root@test-web ~]# iostat -p sda Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.41 0.00 0.11 0.02 0.00 99.46 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn sda 2.94 7.59 56.08 11291723 83406103 sda1 0.00 0.08 0.00 123828 2402 sda2 2.94 7.51 56.08 11165814 83403701 117、iotop命令 动态显示磁盘I/O统计信息 [root@test-web ~]# yum -y install iotop 118、sar命令 -A:所有报告的总和 -u:输出CPU使用情况的统计信息 -v:输出inode、文件和其他内核表的统计信息 -d:输出每一个块设备的活动信息 -r:输出内存和交换空间的统计信息 -b:显示I/O和传送速率的统计信息 -a:文件读写情况 -c:输出进程统计信息,每秒创建的进程数 -R:输出内存页面的统计信息 -y:终端设备活动情况 -w:输出系统交换活动信息 查看CPU的整体负载情况 [root@test-web ~]# sar -u 2 3 Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) 04:22:09 PM CPU %user %nice %system %iowait %steal %idle 04:22:11 PM all 0.00 0.00 0.00 0.00 0.00 100.00 04:22:13 PM all 1.38 0.00 0.25 0.00 0.00 98.37 04:22:15 PM all 2.00 0.00 0.38 0.00 0.00 97.62 Average: all 1.13 0.00 0.21 0.00 0.00 98.66 显示运行队列的大小 [root@test-web ~]# sar -q 2 3 Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) 04:22:49 PM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked 04:22:51 PM 0 267 0.06 0.03 0.05 0 04:22:53 PM 0 267 0.06 0.03 0.05 0 04:22:55 PM 0 267 0.05 0.03 0.05 0 Average: 显示系统内存的使用情况 [root@test-web ~]# sar -r 2 3 Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) 04:23:32 PM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty 04:23:34 PM 10684908 5191264 32.70 0 2299684 2812864 11.72 2467944 2153960 28 04:23:36 PM 10684908 5191264 32.70 0 2299684 2812864 11.72 2467952 2153960 28 04:23:38 PM 10684908 5191264 32.70 0 2299684 2812864 11.72 2467952 2153960 28 Average: 10684908 5191264 32.70 0 2299684 2812864 11.72 2467949 2153960 28 显示缓冲区的使用情况 [root@test-web ~]# sar -b 2 3 Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) 04:24:36 PM tps rtps wtps bread/s bwrtn/s 04:24:38 PM 1.50 1.00 0.50 20.00 4.00 04:24:40 PM 0.50 0.00 0.50 0.00 4.00 04:24:42 PM 27.00 0.00 27.00 0.00 414.00 Average: 9.67 0.33 9.33 6.67 140.67 显示网络错误的统计数据 [root@test-web ~]# sar -n EDEV 2 3 显示网络的运行状态 [root@test-web ~]# sar -n DEV 2 3 Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) 04:25:01 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 04:25:03 PM ens32 4.50 5.50 0.93 5.78 0.00 0.00 0.00 04:25:03 PM lo 61.00 61.00 24.68 24.68 0.00 0.00 0.00 04:25:03 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 04:25:05 PM ens32 13.00 11.50 5.14 7.58 0.00 0.00 0.00 04:25:05 PM lo 42.00 42.00 5.57 5.57 0.00 0.00 0.00 04:25:05 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 04:25:07 PM ens32 6.00 6.50 1.95 7.45 0.00 0.00 0.00 04:25:07 PM lo 75.00 75.00 10.89 10.89 0.00 0.00 0.00 Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s Average: ens32 7.83 7.83 2.67 6.94 0.00 0.00 0.00 Average: lo 59.33 59.33 13.71 13.71 0.00 0.00 0.00 查看系统磁盘的读写性能 [root@test-web ~]# sar -d 2 3 Linux 3.10.0-862.el7.x86_64 (test-web) 11/23/2018 _x86_64_ (4 CPU) 04:26:30 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 04:26:32 PM dev8-0 0.50 0.00 4.00 8.00 0.00 2.00 2.00 0.10 04:26:32 PM dev253-0 0.50 0.00 4.00 8.00 0.00 2.00 2.00 0.10 04:26:32 PM dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 04:26:32 PM dev253-2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 118、chkconfig命令 管理开机服务 centos6中常用 centos7中: systemctl管理 运行级文件: 每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。 第一行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用代替运行级 第二行对服务进行描述,可以用\ 跨行注释。例如,random.init包含三行: # chkconfig: 2345 20 80 # description: Saves and restores system entropy pool for \ # higher quality random number generation. 使用范例: chkconfig --list #列出所有的系统服务 chkconfig --add httpd #增加httpd服务 chkconfig --del httpd #删除httpd服务 chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态 chkconfig --list #列出系统所有的服务启动情况 chkconfig --list mysqld #列出mysqld服务设置情况 chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭 chkconfig mysqld on #设定mysqld在各等级为on,“各等级”包括2、3、4、5等级 如何增加一个服务: 1.服务脚本必须存放在/etc/ini.d/目录下; 2.chkconfig --add servicename 在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了; 3.chkconfig --level 35 mysqld on 修改服务的默认启动等级。 119、rpm命令 -q 查询软件包 - i 与qp配合使用,显示软件包的概要信息 info 安装软件包 此时i是install -v 显示详细信息 -h 用#显示安装进度条 -a 与q搭配查询所有的软件包 -e 卸载软件包 查询rpm包 [root@moban /~]# rpm -qa|grep lrzsz lrzsz-0.12.20-36.el7.x86_64 安装下载工具yumdownloader [root@moban /~]# yum -y install yum-utils 下载系统中已经存在rpm包 [root@moban /~]# yumdownloader lrzsz-0.12.20-36.el7.x86_64 安装rpm包 [root@moban /~]# rpm -ivh lrzsz-0.12.20-36.el7.x86_64.rpm 卸载rpm包 [root@moban /~]# rpm -e lrzsz-0.12.20-36.el7.x86_64.rpm 查看rpm包内容 [root@moban /~]# rpm -pql lrzsz-0.12.20-36.el7.x86_64.rpm 查看文件属于哪个rpm包 [root@web2 panel]# rpm -qf $(which ifconfig) net-tools-1.60-114.el6.x86_64 查看rpm包信息 [root@moban /~]# rpm -pqi lrzsz-0.12.20-36.el7.x86_64.rpm Name : lrzsz Version : 0.12.20 Release : 36.el7 Architecture: x86_64 Install Date: (not installed) Group : Applications/Communications Size : 184846 License : GPLv2+ Signature : RSA/SHA256, Fri 04 Jul 2014 11:35:32 AM CST, Key ID 24c6a8a7f4a80eb5 Source RPM : lrzsz-0.12.20-36.el7.src.rpm Build Date : Tue 10 Jun 2014 07:29:11 AM CST Build Host : worker1.bsys.centos.org Relocations : (not relocatable) Packager : CentOS BuildSystem Vendor : CentOS URL : http://www.ohse.de/uwe/software/lrzsz.html Summary : The lrz and lsz modem communications programs Description : Lrzsz (consisting of lrz and lsz) is a cosmetically modified zmodem/ymodem/xmodem package built from the public-domain version of the rzsz package. Lrzsz was created to provide a working GNU copylefted Zmodem solution for Linux systems. 查看安装包的依赖 [root@moban /~]# rpm -pqR lrzsz-0.12.20-36.el7.x86_64.rpm libc.so.6()(64bit) libc.so.6(GLIBC_2.11)(64bit) libc.so.6(GLIBC_2.14)(64bit) libc.so.6(GLIBC_2.15)(64bit) libc.so.6(GLIBC_2.2.5)(64bit) libc.so.6(GLIBC_2.3)(64bit) libc.so.6(GLIBC_2.3.4)(64bit) libc.so.6(GLIBC_2.4)(64bit) libc.so.6(GLIBC_2.7)(64bit) libnsl.so.1()(64bit) rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PartialHardlinkSets) <= 4.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rtld(GNU_HASH) rpmlib(PayloadIsXz) <= 5.2-13 查看rpm执行的脚本(不以文件形式存在rpm中) [root@yum home]# rpm -pql --scripts nginx-1.6.3-1.x86_64.rpm postinstall scriptlet (using /bin/sh): #!/bin/bash useradd www -M -s /sbin/nologin ln -s /application/nginx-1.6.3/ /application/nginx /server/application 如果目录下有很多rpm包,不知道顺序,可与用aid参数 [root@yum home]# rpm --aid *.rpm【没试验成功 --aid参数貌似不能用了】 chkconfig命令 1 创建启动脚本,对于apache、mysql、ssh这样的软件都是自己带的,我们只要稍微修改一下使之支持chkconfig就可以了 2 修改脚本 我们需要在脚本的前面加上2行,才能支持chkconfig命令 # chkconfig: 2345 08 92 (1-99) # description: Automates a packet filtering firewall with ipchains. [root@moban etc]# ls /etc/rc.d/rc4.d/ -l|awk '{print $9}' K50netconsole S10network 120、set命令 功能说明:设置shell。 语  法:set [+-abCdefhHklmnpPtuvx] 用set 命令可以设置各种shell选项或者列 出shell变量.单个选项设置常用的特性. 在某些选项之后-o参数将特殊特性打开.在某些选项之后使用+o参数将关闭某些特性, 不带任何参数的set命 令将显示shell的全部变量.除非遇到非法的选项,否则set总是返回ture. [root@template test]# set -o [root@template ~]# set +o history 关闭 [root@template ~]# set -o history 开启 [root@template test]# set -o allexport off braceexpand on emacs on errexit off errtrace off functrace off hashall on histexpand on history on ignoreeof off interactive-comments on keyword off monitor on noclobber off noexec off noglob off nolog off notify off nounset off onecmd off physical off pipefail off posix off privileged off verbose off vi off xtrace off [root@template test]# set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu") BASH_VERSION='4.2.46(2)-release' COLUMNS=109 DIRSTACK=() EUID=0 GREP_OPTIONS=--color=auto GROUPS=() HISTCONTROL=ignoredups HISTFILE=/root/.bash_history HISTFILESIZE=10000 HISTSIZE=10000 HISTTIMEFORMAT='%F %T root ' HOME=/root HOSTNAME=template HOSTTYPE=x86_64 ID=0 IFS=$' \t\n' LANG=en_US.UTF-8 LESSOPEN='||/usr/bin/lesspipe.sh %s' LINES=30 LOGNAME=root LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:' MACHTYPE=x86_64-redhat-linux-gnu MAIL=/var/spool/mail/root MAILCHECK=60 OLDPWD=/root OPTERR=1 OPTIND=1 OSTYPE=linux-gnu PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin PIPESTATUS=([0]="0") PPID=904 PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }' PS1='\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\$ ' PS2='> ' PS4='+ ' PWD=/root/test SHELL=/bin/bash SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor SHLVL=1 TERM=linux UID=0 USER=root XDG_SESSION_ID=543 _=-o colors=/root/.dircolors msg='2018-12-04 11:14:26 root set -o' << 重定向 > 或者 >0 输出重定向 >>或者>>1 追加输出重定向 < 或者 <0 输入重定向 <<或者<<0 追加输入重定向 2> 错误重定向,后跟字符串,用来表示 输入结果,也可以用ctrl+d 来结束输入 2>> 错误追加重定向.把错误信息追加到后面的文件中,不会删除文件原有内容 1 标准输出 stdin 0 标准输入 stdout 2 错误输出 stderr 案例: [root@moban ~]# echo 123 > test.log [root@moban ~]# cat a.log 2>b.log [root@template home]# cat a.log 123 [root@template home]# cat b.log [root@template home]# echoaaa 123 1>a.log 2>b.log [root@template home]# cat a.log [root@template home]# cat b.log -bash: echoaaa: command not found 在计划任务中经常可以看到。例如我们公司的计划任务举例: */2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testOne >/dev/null 2>&1 */2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testTwo >/dev/null 2>&1 [root@template home]# ls xxx 2>1 正确标准输出,错误不输出重定向到 1文件 [root@template home]# ls xxx 2>&1 正确标准输出,错误不会输出 ############################################################ [root@template home]# ls XXX >>/dev/nul 2>1 正确输出到/dev/null 错误输出到1文件中 会生成1文件 [root@template home]# ls XXX >>/dev/null 2>&1 正确输出到/dev/null 错误输出到&1(也是个空),此时不会生成1文件 << 输入给cat cat 又输出了 [root@template home]# cat <