工作中遇到的一些linux常用命令总结

零、查看历史命令,linux中可按“↑” “↓”查找之前输入的命令,亦可用 history 命令查看之前的输入,linux中的亦有“Tab”键可联想输入

一、root权限: 1、su 之后输入root密码进入root模式

       2、root执行相关shell    sudo  sh  demo.sh

二、文件及文件夹相关

  1、查看当前工作路径:  pwd

  2、查看当前文件夹下的文件:ls

  3、进入文件夹:  cd  文件夹名

  4、仅仅查看文件内容:cat 文件名

  5、编辑文件:  vim 文件名

    5.1退出vim(vi): esc   后    输入“:q”不保存退出;

               esc   后    输入“:wq”保存退出;

               esc   后    输入“:x”保存退出,若没有修改内容,则不会改变日志更新时间

    (有些Arm开发板上没有vi命令时,可以尝试下nano命令,和sed命令)

    编辑文件: nano  文件名

  6、解压.tar后缀的压缩包:tar –xvf file.tar           (其他后缀压缩包解压各有不同)

       将文件夹下所有包压缩成zip格式:zip  -r  myfile.zip  ./*

    解压zip:unzip -o -d  /home/workspace myfile.zip

  7、将本服务器文件传到另一服务器:   scp 文件名 用户名@计算机IP或者计算机名称:远程路径 

  8、查看服务器空间:df -h

  9、查看当前目录下一级子文件和子目录占用的磁盘容量:du  --max-depth=1 -h

  

三、测试端口及ip相关

  1、ping相应ip:  ping 192.168.110.110

  2、telnet  ip  port: telnet 192.168.110.111 22              (22为ssh端口,一般情况均会开启)

       退出telnet :ctrl + ]      之后输入quit  

  3、traceroute ip:port     上述命令也许需要安装

  4、netstat -ntul           查看正在监听的端口(t tcp u udp)

  5、抓包!:tcpdump    (此命令具体看情境使用,可直接抓网卡或筛选后抓包)

 

四、进程相关命令

  1、ss命令可以便捷的统计socket信息 (效率高)

    -t       tcp

    -l        listening

    -a       all

    -m       memory  显示内存情况

    -n  不解析服务器名称

    例子:ss -nl |grep 9999       显示9999端口是否开启

  2、top 动态显示进程

  3、ps :静态显示正在运行的进程

    例1、ps   -aux  以列表形式显示进程

    例2、ps -elf  以长格式显示系统进程,包含更丰富内容

    例3、详解ps -ef|grep:

           ps命令将某个进程显示出来 grep命令为查找命令 “|”表示管道命令(ps.grep)两个命令同时进行
         eg:检查java进程是否存在: ps -ef|grep java, 输出字段如下:
            UID PID PPID C STIME TTY TIME CMD
            admin 1223 1111 0 00:32 pts/0 00:00:07 grep --color=auto dae
            UID:程序被此uid所有
            PID:程序id
            PPID:上级父程序id
            C: CPU所用的资源百分比
            STIME:系统启动时间
            TTY:登入者的终端机位置
            TIME:使用掉的CPU时间
            CMD:所下达的是什么命令

  4、pstree

    eg : pstree -p 1110|wc -l

      (可显示1110进程下的线程数量,去掉wc-l  可显示具体线程号)

  5、pstack 进程号,显示堆栈

  (安装的pstack有问题时,自行实现下述shell,可传入进程号)

#!/bin/sh

if test $# -ne 1; then
    echo "Usage: `basename $0 .sh` <process-id>" 1>&2
    exit 1
fi

if test ! -r /proc/$1; then
    echo "Process $1 not found." 1>&2
    exit 1
fi

# GDB doesn‘t allow "thread apply all bt" when the process isn‘t
# threaded; need to peek at the process to determine if that or the
# simpler "bt" should be used.

backtrace="bt"
if test -d /proc/$1/task ; then
    # Newer kernel; has a task/ directory.
    if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
    backtrace="thread apply all bt"
    fi
elif test -f /proc/$1/maps ; then
    # Older kernel; go by it loading libpthread.
    if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
    backtrace="thread apply all bt"
    fi
fi

GDB=${GDB:-/usr/bin/gdb}

if $GDB -nx --quiet --batch --readnever > /dev/null 2>&1; then
    readnever=--readnever
else
    readnever=
fi

# Run GDB, strip out unwanted noise.
$GDB --quiet $readnever -nx /proc/$1/exe $1 <<EOF 2>&1 | 
set width 0
set height 0
set pagination no
$backtrace
EOF
/bin/sed -n     -e 's/^\((gdb) \)*//'     -e '/^#/p'     -e '/^Thread/p'
#end

  

四、firewall防火墙相关

  1、启动防火墙: systemctl start firewalld

  2、关闭防防火墙:systemctl stop firewalld

  3、开机自启防火墙:systemctl enable firewalld

  4、显示防火墙状态:firewall-cmd --list-all     此命令在防火墙未启动时不可显示

    注意:下面命令在防火墙开启时才可设置,且设置成功后,均需要重新载入防火墙配置(即执行)firewall-cmd --reload才会生效

  5、添加ip白名单:firewall-cmd --permanent --zone=public --add-source=192.168.100.0/24       ,/24为保留最后一位,/16为保留最后两位

  6、添加端口: firewall-cmd --zone=public --add-port=22/tcp --permanent

  7、删除白名单和端口,只需将上面两条命令中的 “add” 改成 “remove”

 

五、iptables防火墙相关

  1、安装iptables:yum install -y iptables

          yum install iptables-services

  2、查看iptables状态:service iptables status

  3、查看iptables规则:iptables -L

            iptables -nvL –-line-number   可看到行数等稍具体的信息

  4、清除iptables规则:iptables -F

  5、规则相关:

    添加iptables规则禁止ip访问:
      iptables -I INPUT -s 192.168.101.196/24 -j DROP
      iptables -I INPUT 3 -s 192.168.101.196/24 -j DROP (将规则写入第三行)
    -D删除第二行规则:iptables -D INPUT 2
    -R修改第二行规则:iptables -R INPUT 3 -j ACCEPT  

    修改规则后均需要:service iptables save
             service iptables restart

  6、设置iptables自启及查看状态:systemctl enable iptables.service              自启

                 systemctl start iptables.service                   启动
                 systemctl status iptables.service                 看状态

 

 

水平有限,日志基本作记录本之用,欢迎指正错误,欢迎交流

posted @ 2019-04-28 20:19  张Star  阅读(371)  评论(0编辑  收藏  举报