闲人运维

个人运维记录


Linux常用命令合集

<p>[TOC]</p> <h5>信息显示命令</h5> <ul> <li>系统查看命令</li> </ul> <pre><code class="language-bash">uname # 显示操作系统相关信息的命令 uname -a # 查看主机名 uname -r # 内核版本 uname -o # 操作系统 hostname # 查看主机名 dmesg # 查看开机信息,用于诊断系统故障 uptime # 查看系统运行时间及负载 stat $filename # 显示文件或文件系统的状态 du # 计算磁盘空间 df # 查看磁盘空间 top # 实现显示系统资源情况 free # 查看内存 date # 查看时间 cal # 查看日历 which # 查找文件路径 pidof sshd # 通过进程名获取pid top -p $(pidof sshd) # 实时查看状态 killall httpd # 删除进程相关的所有子进程 pidof sshd |tee 1.log # 结果打印终端并写入文件 tac $filename # 从文件末尾查看内容,和cat相反 less $filename # 查看长内容文件,支持上下翻页 nl $filename # 显示内容的同时显示对应行号 time ls # 获取执行时间</code></pre> <ul> <li>date命令</li> </ul> <pre><code class="language-bash">date -s "2016-12-15 00:00:00" # 设置系统日期和时间 date +%s # 查看当前系统时间戳 date +'%F %T' # 查看当前系统时间 date -d "2016-12-15 18:00:00" +%s # 把日期和时间转换成时间戳 date -d '@1481842800' '+%F %T' # 把时间戳转成时间</code></pre> <h5>打包压缩命令</h5> <pre><code class="language-bash">tar xf mongodb-linux-x86_64-rhel70-4.2.19.tgz -C mongo --strip-components 1 # 解压到指定的目录下并命名 tar -czvf - 1.txt | openssl des3 -salt -k password -out 1.tar.gz # 加密压缩 openssl des3 -d -k password -salt -in 1.tar.gz | tar xzf - # 解密压缩 md5sum $filename # 查看文件MD5 cksum $filename # ckr校验</code></pre> <h5>文件查找命令</h5> <pre><code class="language-bash">whereis # 定义文件,查看和文件相关的文件 locate # 查找文件,安装 yum -y install locate,更新索引 updatedb find /data -size +100M # 找大于100M的文件 find /data -type f -name "*.log" # 找log结尾的文件 find . -type d | sort # 找当前所有目录并排序</code></pre> <h5>防火墙命令</h5> <ul> <li>iptables操作命令,3个表为 Filter、Nat、Mangle;5个链为 PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING ;</li> </ul> <pre><code class="language-bash">iptables-save &gt; /etc/iptables-script # 保存规则 iptables-restore &gt;/etc/sysconfig/iptables # 恢复规则 iptables -F # 清除所有规则 iptables -L -n # 查看规则 iptables -L -n --line-number # 查看规则并显示规则的序号 iptables -D INPUT 3 # 根据序号删除规则,入删除第3条规则 iptables -D INPUT -p tcp --dport 22:25 -j ACCEPT # 删除指定规则 iptables -P INPUT DROP # 设置默认链规则,默认是ACCEPT iptables -P FORWARD DROP # 设置FORWARD链 iptables -A INPUT -s 10.0.0.100 -j DROP # 拦截一个IP iptables -A INPUT -p tcp -i eth0 -s 10.0.0.100 -j DROP # 拦截ip在某个网卡上的TCP连接 iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT # 允许ssh连接1,入方向 iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT # 允许ssh连接2,出方向 iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISH -j ACCEPT # 允许访问80 http服务,入方向 iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISH -j ACCEPT # 允许访问80 http服务,出方向 iptables -A INPUT -i eth0 -p tcp -m multiport --dports 22,80,443 -m state --state NEW,ESTABLISH -j ACCEPT # 允许多个端口访问,入方向 iptables -A OUTPUT -o eth0 -p tcp -m multiport --sports 22,80,443 -m state --state ESTABLISH -j ACCEPT # 允许多个端口访问,出方向 iptables -A INPUT -i eth0 -s 10.0.2.0/24 -p tcp --dport 22 -m state --state NEW,ESTABLISH -j ACCEPT # 仅允许某个网段访问 iptables -A INPIT -p icmp --icmp-type echo-request -j ACCEPT # 允许外部ping,入方向 iptables -A OUTPUT -p icmp --icmp-type echo-relay -j ACCPT # 允许响应ping,出方向 iptables -A OUTPUT -p udp -s eth0 --dport 53 -j ACCEPT # 向外请求dns,出方向 iptables -A INPUT -p udp -d eth0 --sport 53 -j ACCEPT # 向外请求dns,允许dns响应,入方向 iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT # 防dos,-limit 每分钟请求数,-limit-burst 总请求数达到多少开始限流 iptables -I INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 30 -j REJECT # 限制并发连接数,超过30个拒绝 iptables -I INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT # 限制每个IP每秒并发连接数最大3个(1) iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT # 限制每个IP每秒并发连接数最大3个(2) iptables -t nat -A PREROUTING -p tcp -d 192.168.102.37 --dport 422 -j DNAT --to 192.168.102.37:22 # 端口转发,将访问端口442的连接路由至端口22</code></pre> <ul> <li>firewall防火墙命令;</li> </ul> <pre><code class="language-bash"># 添加策略 firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="10.0.0.4" port protocol="tcp" port="20208" accept" firewall-cmd --reload firewall-cmd --list-rich-rules firewall-cmd --list-all --zone=public # 删除策略 firewall-cmd --permanent --zone=public --remove-rich-rule="rule family="ipv4" source address="10.0.0.4" port protocol="tcp" port="20208" accept" firewall-cmd --reload firewall-cmd --list-rich-rules firewall-cmd --list-all --zone=public</code></pre> <h5>请求和下载命令</h5> <ul> <li>curl 命令使用技巧;</li> </ul> <pre><code class="language-bash">curl -I www.baidu.com # 仅返回响应头信息 curl -o badu.html http://www.baidu.com # 下载整个页面到文件 curl -s -o baidu.html http://www.baidu.com # 下载整个页面到文件,(不输出下载信息) curl -A "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36" -e "baike.baidu.com" http://127.0.0.1 # 伪装客户端,指定user-agent和referer下载 curl -c ./cookies.txt -F NAME=user -F PWD=123 http://www.example.com/login.html # 模拟用户登录,并保存cookies到文件 curl -b cookies.txt http://www.baidu.com # 使用cookie访问 curl -u user:pass http://www.example.com # 访问HTTP认证页面 curl -T filename ftp://user:pass@ip/a.txt # 上传文件到ftp curl ftp://ip/filename -u user:pass -o filename # 从ftp下载文件 curl -H "Referer: www.example.com" -H "User-Agent: Custom-User-Agent" http://www.baidu.com # 自定义请求头信息 curl -d "userName=tom&amp;passwd=123456" -X POST http://www.example.com/login # 发送post请求,-d默认post请求 curl -d "somedata" -X GET http://www.example.com/api # 发送get请求 curl http://127.0.0.1:8080 -o file.txt --progress # 下载到指定文件并显示进度条 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo # 下载到指定文件 curl -x "http://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" # 设置代理,方式1 curl --proxy "user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" # 设置代理,方式2 curl -H "uuid:umetn3xuuuh2" -H "Content-Type: application/json" -X POST http://127.0.0.1:8081/eventNotification -d '{"Name":"deviceInfoRequest"}' # 发起post请求,提交json数据</code></pre> <ul> <li>wget命令使用技巧;</li> </ul> <pre><code class="language-bash">wget http://nginx.org/download/nginx-1.11.7.tar.gz # 下载单个文件 wget -b http://nginx.org/download/nginx-1.11.7.tar.gz # 后台下载 wget -t 3 -c http://nginx.org/download/nginx-1.11.7.tar.gz -P /data/ # 下载到指定目录下,使用-c和--tries参数可以优化网络下载 wget --spider http://nginx.org/download/nginx-1.11.7.tar.gz # 不下载任何内容,判断URL是否可以访问 wget http://www.baidu.com/index.html -O index.html # 下载内容写到文件 wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo # 下载内容写到文件 wget -i url.list # 从文件中读取URL下载 wget --ftp-user=admin --ftp-password=admin ftp://192.168.1.10/ISO/CentOS-6.5-i386-minimal.iso # 从ftp下载文件 wget -U "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36" --referer "http://nginx.org/en/download.html" http://nginx.org/download/nginx-1.11.7.tar.gz # 伪装客户端,指定user-agent和referer下载</code></pre> <h5>文件同步和传输</h5> <ul> <li>rsync文件同步命令;</li> </ul> <pre><code class="language-bash">rsync -avz abc /opt # 本机复制目录 rsync -avz SRC root@192.168.1.120:~/ # 推送目录到远程主机的目录下 rsync -avz root@192.168.1.10:/data ~/ # 远程主机目录拉取到本地 rsync -avz --delete SRC root@192.168.1.120:DST # 同步远程和本地的文件 rsync -avz --exclude=no_dir SRC root@192.168.1.120:DST 3 # 排除同步某个文件 rsync -avz /etc/hosts -e "ssh -p22" root@192.168.1.120:/opt # 指定ssh端口批量同步</code></pre> <ul> <li>ssh连接命令;</li> </ul> <pre><code class="language-bash">ssh user@192.168.1.120 # 指定用户连接 ssh user@192.168.1.120 'ifconfig' # 远程执行命令 ssh user@192.168.1.120 'cat &gt;&gt; file' &lt; /etc/passwd # 本地文件内容写到远程主机文件</code></pre> <ul> <li>sshpass命令</li> </ul> <pre><code class="language-bash">sshpass -p 123456 ssh root@192.168.1.10 # 免交互SSH登录,需要安装 sshpass -p 123456 scp a.txt 192.168.1.10:/root # 免交互传输文件</code></pre> <ul> <li>pssh命令</li> </ul> <pre><code class="language-bash"># 主机文件创建 cat &gt; all_hosts &lt;&lt;eof 192.168.1.10 192.168.1.12 192.168.1.14 eof pssh -h all_hosts -i 'uptime' # 批量执行命令 pscp -h all_hosts aa.txt /tmp/ # 批量发送文件</code></pre> <h5>文件和路径</h5> <pre><code class="language-bash">basename /usr/bin/sort # 获取路径最后一个名字 du -sh /opt # 查看目录大小 du -sh --exclude=test /opt # 排除某个文件统计目录大小</code></pre> <h5>字符和输出处理</h5> <ul> <li>tr命令</li> </ul> <pre><code class="language-bash">echo "aaacccddd" | tr -s '[a-z]' # 去重字符,输出acd echo "aaabbbccc" | tr -d bbb # 删除字符,输出aaaccc echo -e "a\nb\nc" | tr -d '\n' # 删除换行符 echo "aaabbbccc" | tr '[a-z]' '[A-Z]' # 替换字符,结果AAABBBCCC</code></pre> <ul> <li>seq命令</li> </ul> <pre><code class="language-bash">seq 3 # 数字序列,从1~3 seq 03 # 输出带0的序列,输出 01,02,03 seq 2 5 # 范围数字序列,输出2,3,4,5 seq 1 2 5 # 步长序列,2是步长,输出 1,3,5 seq 5 |shuf # 输出随机序列 shuf -i 5-10 # 输出范围随机序列</code></pre> <h5>网络和路由</h5> <ul> <li>netstat命令</li> </ul> <pre><code class="language-bash">netstat -anltu # 显示所有连接 netstat -antp # 显示所有tcp连接 netstat -anup # 显示所有udp连接 netstat -r # 显示路由表</code></pre> <ul> <li>ss命令</li> </ul> <pre><code class="language-bash">ss -t -a # 显示所有tcp连接 ssu -u -a # 显示所有udp连接 ss -s # 显示所有socket使用 ss -o state established # 显示所有建立的连接 ss -o state TIME-WAIT # 显示所有TIME-WAIT状态</code></pre> <ul> <li>lsof命令</li> </ul> <pre><code class="language-bash">lsof /etc/passwd # 查看哪个进程占用文件 lsof -i:80 # 列出80端口监听的进程 lsof -i tcp # 列出所有TCP网络连接 lsof -i udp # 列出所有UDP网络连接 lsof -p 5373 # 列出进程ID打开的文件</code></pre> <ul> <li>ip命令</li> </ul> <pre><code class="language-bash">ip route # 查看路由表 ip route add default via 192.168.1.1 dev eth0 # 添加路由 ip route del 192.168.1.0/24 via 192.168.1.1 # 删除路由 ip route add 172.17.2.0/24 via 192.168.2.1 dev eth0 # 添加静态路由</code></pre> <ul> <li>nc命令;</li> </ul> <pre><code class="language-bash">nc -z 192.168.1.10 1-65535 # 端口扫描 nc -p 31337 -w 5 192.168.1.10 22 # TCP协议连接到目标端口 nc -z -w 2 192.168.1.10 22 # 探测端口是否开启</code></pre> <ul> <li>tcpdump抓包</li> </ul> <pre><code class="language-bash"># 使用 tcpdump 抓包后,会遇到的 TCP 报文 Flags,有以下几种: - [S] : SYN(开始连接) - [P] : PSH(推送数据) - [F] : FIN (结束连接) - [R] : RST(重置连接) - [.] : 没有 Flag (意思是除上面四种类型外的其他情况,有可能是 ACK 也有可能是 URG) # tcpdump # 默认监听第一块网卡上所有流量 tcpdump -i eth0 # 指定监听某个网卡所有流量 tcpdump -i eth0 host 192.168.56.1 # 捕获主机192.168.56.1经过本机网卡eth0的所有数据包(也可以是能解析的域名) tcpdump arp # 捕获所有arp协议数据包 tcpdump udp port 53 # 监听本机udp端口53的数据包 tcpdump tcp port 22 and host 192.168.56.210 # 监听92.168.56.210主机tcp协议22端口的数据包 tcpdump -i eth0 dst 10.10.1.20 # 捕获经过本机网卡去往10.10.1.20的流量 tcpdump -n icmp # 抓取所有icmp包 tcpdump src host 192.168.10.100 # 抓取源地址192.168.10.100的流量 tcpdump net 192.168.10.0/24 # 抓取某个网段的流量 tcpdump src port 8088 # 源端口过滤 tcpdump dst port 8088 # 目标端口过滤 tcpdump icmp -w icmp.pcap # 结果输出到文件,可以通过wareshark打开查看 tcpdump icmp -r all.pcap # 从文件读取包数据</code></pre>

页面列表

ITEM_HTML