Linux运维常用命令

Posted by Cai Qiqi on 2016-09-09

参考:
https://wiki.tankywoo.com/linux/linux_tips.html
https://wiki.tankywoo.com/linux/linux_network_config.html

w

display who is logged in and what they are doing
这里写图片描述

The w utility prints a summary of the current activity on the system, including what each user is doing. The first line displays the current time of day, how long the system has been running, the number of users logged into the system, and the load averages. The load average numbers give the number of jobs in the run queue averaged over 1, 5 and 15 minutes.
The fields output are the user’s login name, the name of the terminal the user is on, the host from which the user is logged in, the time the user logged on, the time since the user last typed anything, and the name and arguments of the current process.

top/htop

查看进程和CPU状态
这里写图片描述

It is similar to top, but allows you to scroll vertically and horizontally, so you can see all the processes running on the system, along with their full command lines

其他

man ps可以看到各个进程状态的解释,查看进程状态是否有D,表示在等待IO。

while true; do date; ps auxf | awk '{if($8=="S") print $0;}'; sleep 1; done

或者

for x in `seq 1 1 10`; do ps -eo state,pid,cmd | grep "^D"; echo "----"; sleep 1; done

其实工具都只是表层, 最底层的还是读取/proc下的文件, 具体在每个工具的man手册都有介绍。
还有一个比较强大的工具dstat, 集合了vmstat, iostat, ifstat等工具的功能, 并且输出是颜色高亮

这里写图片描述

CPU使用最高的三个进程(Linux)

ps -auxf | sort -nr -k 3 | head -3

这里写图片描述
这里写图片描述

查看本机最常用的10条命令

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

这里写图片描述
这里写图片描述

统计当前目录下(包括子目录)以.py结尾文件的个数及总代码行数

文件个数

find . -name "*.py" | wc -l

单个文件代码行数及总行数

find . -name "*.py" | xargs wc -l

输出错误重定向(不想看到错误输出到屏幕)

ls 1>/dev/null 2>/dev/null
ls >/dev/null 2>&1

显示没个远程IP与本地的连接数

netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}'

替换上条命令的关键字并执行

# 将上一条命令的pint换成traceroute
^ping^traceroute^

搜索最近一条符合关键字的命令
比如上面执行过命令是ping wutianqi.com, 想再执行, 可以

#p是关键字, 也可以 pi, pin, ping都行
!p

网络配置相关

直接用ifconfig配置

设置网卡eth0的IP地址和子网掩码
sudo ifconfig eth0 192.168.2.106 netmask 255.255.255.0
设置网关
sudo route add default gw 192.168.2.254
配置DNS
sudo vim /etc/resolv.conf

ubuntu 网络配置文件

sudo vim /etc/network/interfaces
DHCP连接
auto eth0
iface eth0 inet dhcp
手动配置静态ip
auto eth0
iface eth0 inet static
address 192.169.2.106
gateway 192.168.2.254
netmask 255.255.255.0
#network ?.?.?.?
#broadcast ?.?.?.?
#dns设置
#我印象中重启后resolv.conf的内容会删除
#所以DNS服务器的配置也是在/etc/network/interfaces下
dns-nameservers 8.8.8.8
dns-search xxx.xxx.xxx...
#添加一条静态路由
up route add -net 192.0.0.0/8 gw 192.0.0.1 dev eth0