---
title: "linux常用命令"
author: "splayx"
date: "2013-11-30T15:01:25Z"
tags: []
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2014-04-13T20:40:39Z"
id: 1983210
url: "https://splayx.iteye.com/blog/1983210"
---

1、lsof使用

lsof -p &lt;pid&gt;

lsof -i:port查看端口port被哪些程序占用了

 

2、在类Unix系统中，/dev/null，或称空设备，是一个特殊的设备文件，它丢弃一切写入其中的 **...** 在程序员行话，尤其是Unix行话中， /dev/null 被称为bit bucket或者黑洞。

 

3、获得进程

54440    31336     1  0 13:52 ?        00:00:12 ./redis-server \*:6379       

ps -ef | grep redis | grep grep -v | awk '{print $2}'

 

4、获得resident set size（rss）Bytes

cat /proc/31336/stat | awk '{print $24 * 4096}'

 

5、得到private dirty

cat /proc/31336/smaps

[http://stackoverflow.com/questions/9922928/what-does-pss-mean-in-proc-pid-smaps](http://stackoverflow.com/questions/9922928/what-does-pss-mean-in-proc-pid-smaps)

 

6、删远程分支

git push origin :simplify\_code\_1（注意origin后面有个空格）

 

7、查看/设置磁盘预读

$ sudo blockdev --getss /dev/sda9 

512

$ sudo blockdev --getra /dev/sda9 

256

所以这个机器一次预读128Kb的数据

 

8、Makefile中的特殊符号

$^ : 所有依赖文件的完整路径

$@ : 表示目标

 

9、find指令使用

find \[dir] \[option] args

find /tmp -iname foo.txt 在/tmp下递归查找名字含有‘foo.txt‘的文件或目录

 

10、dmesg命令

可以用来查看linux服务器是否出现故障，以及用来作为排错的依据，

例如ulimit -c=0，没有开启coredump时，可以通过这个看看是不是有core产生了。

 

11、addr2line

跟dmesg配合可以查在哪一行代码发生了coredump

也可以通过函数地址查函数名

addr2line 0x4BED83 -e objs/nginx -f ngx\_worker\_process\_exit /../src/os/unix/ngx\_process\_cycle.c:1403

 

12、iperf

iperf -s -p 9999， iperf -c sip -p 9999

\------------------------------------------------------------

Server listening on TCP port 9999

TCP window size: 85.3 KByte (default)

\------------------------------------------------------------

\[  4] local 10.232.129.129 port 9999 connected with 10.125.50.241 port 48270

 

\[  4]  0.0-10.0 sec  1015 MBytes    849 Mbits/sec

可以测两端间的最大带宽

 

13.strace -p xxxx（正在运行的进程），strace xxx

列出系统调用，strace -c -p xxxx得到统计

 

14.tcptraceroute

查看到目的地经过的路径

 

15.nc的使用

服务端：nc -l 8888

客户端：nc -s 127.0.0.1 -p 12345 10.125.50.240 8888

然后就可以传数据了

$ echo -n "GET / HTTP/1.0\\r\\n\\r\\n" | nc -w 5 www.google.com 80 -v

Connection to www.google.com 80 port \[tcp/http] succeeded!

加上-v打印点提示信息

 

port forwarding

 

```
$ nc -l 8082 | nc remote_host 80
这样搞remote_host 80返回的数据没有给回8082
用pipe可以实现双向数据通道
```

```
$ mkfifo backpipe
$ nc -l 8082 0<backpipe | nc remote_host 80 1>backpipe
```

 

16.使用ssh做socket代理

ssh -N -D 9000 -f root@localhost

 如果不是本地访问需要加上-g，允许远端机器建立连接。不然一有连接进来就发reset包