2019年3月26日 星期二

Linux 常用指令

Process 管理

//列出系統上最耗費記憶體的程式
//-e 輸出所有行程的資訊
//-o 指定輸出欄位
//--sort 排序
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

//取當前pid
echo $$

//process 資源限制
ls /proc/{pid}/fd
cat /proc/{pid}/limits

//開檔狀況
ls -l /proc/{pid}/fd | less

//總開檔數
ls -l /proc/{pid}/fd | wc -l

網路相關

//查看80是哪個procese監聽的
netstat -tlnp | grep :22

tcp    0  0 0.0.0.0:22  0.0.0.0:*  LISTEN   1444/sshd
tcp6   0  0 :::22       :::*       LISTEN   1444/sshd

User 管理

sudo -i 切換為root

ssh 管理

//scp
scp "fileName" ubuntu@xxx.xx.xx.xx:/home/ubuntu/
scp -i "keyfile" filename aa@xxx.xxx.xxx.xxx:/home/aa/
sshpass -p "pass" scp "fileName" ubuntu@xxx.xx.xx.xx:/home/ubuntu/

//指定port P大寫
scp -P port root@xxx.xxx.xxx:/root/abc.jpg abc.jpc 
scp -P port abc.jpg root@xxx.xxx.xxx:/root/abc.jpg

//指定key
ssh -i 'keyfile' aa@xxx.xxx.xxx.xxx

//設定ssh timeout 時間
vim /etc/ssh/sshd_config

//找到ClientAliveInterval 参数,如果没有就自己加一行。
//client alive 檢查,秒
ClientAliveInterval=600 

//client alive 檢查次數 
ClientAliveCountMax=12

//依據以上設定,就是600*12=7200秒都沒反應就斷線
//開啟root ssh login
sudo passwd root    //更改root密碼
sudo vi /etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes

//centos 
service sshd restart
systemctl restart sshd

//debain
service ssh restart
systemctl restart sshd

ssh agent

1. Start ssh-agent in the background.
 > Agent pid xxxxx
2. Add ssh key to agent
  $ ssh-add -K pathToPrivateKey
  (-K 只限mac 內建的ssh-add, 表示存 passphase in your keychain)

3. if you key with passphase, agent will ask you to input pass.

//看ssh-agent執行狀況
看變數,是否有輸出
echo $SSH_AUTH_SOCK
echo $SSH_AGENT_PID

//in shell script
//自動加入所有預設rsa的key
if [ -z "$SSH_AUTH_SOCK" ] ; then
  eval `ssh-agent -K`
  ssh-add
fi

//kill ssh-agent
ssh-agent -k

github 使用多個 ssh key 與帳號

Host   連線至遠端的別稱(可以隨便填)
HostName   遠端的Domain或IP
Port   遠端的SSH連接埠(為預設的話可以不填)
User   使用者名稱
PreferredAuthentications   偏好使用的驗證(這邊的值都為publickey)
IdentityFile   私鑰的位置(可為相對或絕對位置,但建議使用絕對位置)

//edit ~/.ssh/config

Host gh.aaa.my
 HostName github.com
 User Hi_AAA
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_aaa_github

Host gh.bbb.company
 HostName github.com
 User Hi_BBB
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_bbb_github

Host bb.aaa.my
 HostName bitbucket.org
 User Yo_AAA
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_aaa_bitbucket

原本 repo 位置會改變
原本 git@github.com:Hi_AAA/my_project.git  
新的 git@gh.aaa.my:Hi_AAA/my_project.git

Domain的部分可以使用設定檔的 Host 名稱取代

//key 加入 ssh-agent 管理
ssh-add 

apt


apt-cache policy mysql-server       列出mysql所有版本
apt-get install mysql-server=5.6.29 安裝mysql指定版本

2019年3月4日 星期一

網路 http

NotFalse 技術客詳細說明

HTTP

1.0 /1.1 /2.0

主要特色

簡單:request - response
不限傳輸資料內容(content-type定義)
無連接:一個request一個連接
無狀態

URI 與URL 參考

URI (Uniform Resource Identifier)統一資源識別符

web上的資源,例如檔案、圖片、程式,都是用URI定義的。
能定位到資源的就是URI,例如:
localhost:80/index.html
localhost:80/user/save

URL(Uniform Resource Location)統一資源定位符

URL是URI的子集。
URL是路徑,URI是實際資源。
URI:localhost:80/user/save
URL:localhost:80/user

Message

HTTP/1.1傳輸是以message為單位。
(HTTP/2是以message frame訊息框為單位)
Message稱為訊息、報文、消息。

Message格式

Start-line(起始行)
Headers(多個header-field+CRLF)
CRLF
Message-body(可選的)

Request Message範例

POST /?id=1 HTTP/1.1        (Start-line=>request line)
Host: echo.paw.cloud        (header-field)
Content-Type: application/json; charset=utf-8 (header-field)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0 (header-field)
Connection: close           (header-field)
Content-Length: 136         (header-field)
(CRLF)
(以下為message-body)
{
  "status": "ok",
  "extended": true,
  "results": [
    {"value": 0, "type": "int64"},
    {"value": 1.0e+3, "type": "decimal"}
  ]
}

Response Message範例

HTTP/1.1 200 OK                      (Start-line=>status line)
Content-Type: text/html; charset=utf-8    (header-field)
Date: Sat, 18 Feb 2017 00:01:57 GMT       (header-field)
Server: nginx/1.11.8                      (header-field) 
transfer-encoding: chunked                (header-field)
Connection: Close                         (header-field)
(CRLF)
(以下為message-body)
<html lang="en">
<head>
<title>echo<title>
</head>

Start-line 起始行

Request start line,依序是:
方法(method)、空白(space)、請求目標(request target)、空白(space)、HTTP版本(http version)、CRLF

Response start line,依序是:
HTTP版本(http version)、空白(space)、狀態碼(status code)、空白(space)、原因短語(reason phrase)、CRLF

Method

HTTP1.0 定義三種方法:post,get,head
HTTP1.1 定義的方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT

GET  请求指定的页面信息,并返回实体主体。
HEAD     类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
POST     向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
PUT      从客户端向服务器传送的数据取代指定的文档的内容。
DELETE   请求服务器删除指定的页面。
CONNECT  HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
OPTIONS  允许客户端查看服务器的性能。
TRACE    回显服务器收到的请求,主要用于测试或诊断
  • POST 
  • 規定資料要放在body中
    四種常用的content-type:
    application/x-www-form-urlencoded
    multipart/form-data
    application/json
    text/xml

Status Code

1xx 請求接受,繼續處理。
2xx 請求成功。
3xx 重定向-要完成請求要進一步處理。
4xx 客戶端錯誤,例如請求語法錯誤。請求無法實現:403。
5xx 服務器端錯誤。

Content Type

NotFalse 技術客詳細說明
  • application/x-www-form-urlencoded
  • 常用於POST表單,是key-value形式

    POST http://www.example.com HTTP/1.1
    Content-Type: application/x-www-form-urlencoded;charset=utf-8
    .....省略
    title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3
    

  • multipart/form-data
  • 使用表單上傳文件時,必須對form的enctype設定為multipart-/form-data

    POST http://www.example.com HTTP/1.1
    Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA
    
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA
    Content-Disposition: form-data; name="text"
    
    title
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA
    Content-Disposition: form-data; name="file"; filename="chrome.png"
    Content-Type: image/png
    
    PNG ... content of chrome.png ...
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA--
    

  • application/json
  • POST http://www.example.com HTTP/1.1 
    Content-Type: application/json;charset=utf-8
    
    {"title":"test","sub":[1,2,3]}
    


  • text/xml
  • POST http://www.example.com HTTP/1.1 
    Content-Type: text/xml
    
    
    <methodcall>
        <methodname>examples.getStateName</methodname>
        <params>
            <param />
                <value><i4>41</i4></value>
            
        </params>
    </methodcall>

keep alive模式

參考
keep alive 需client-server兩邊配合

http 1.0中默認是關閉的,需要在http頭加入"Connection: Keep-Alive”,才能啓用Keep-Alive;

http 1.1中默認啓用Keep-Alive,如果加入"Connection: close “,才關閉。

目前大部分瀏覽器都是用http1.1協議,也就是說默認都會發起Keep-Alive的連接請求了

所以是否能完成一個完整的Keep- Alive連接就看服務器設置情況。

2019年2月15日 星期五

網路 Socket

Beejs guide to network programming詳細說明

Socket Types

  • Stream Socket
  • SOCK_STREAM 串流式
    基於TCP
    資料需要可靠的、需要順序的多半用stream socket
    例如:HTTP/TELNET

  • Datagram Socket
  • SOCK_DGRAM 訊息式
    基於UDP
    不可靠、不一定照順序、但是如果送達(封包重組完成,如果有分割的話),資料就是正確的。
    遊戲玩家位置、音樂、音效,等可容許小量丟失的。
    例如:TFT/DHCPD

  • Row Socket


  • Link Layer Socket

2019年2月13日 星期三

RabbitMQ

基本概念

官方網頁

RabbitMQ就像郵局,唯一不同的是它送數位資料,郵局送信。
Queue就是郵筒,存在郵局裡(RabbitMQ),只受限於ram跟disk。

message只存在queue裡。
多個producers可以送message給同一個queue。
多個consumer可以從同一個queue收message。
基本上每個message被收走就沒有了。
  • RabbitMQ=郵局
  • Queue=郵筒(存在於郵局RabbitMQ)
  • Message=信
  • Exchange=在producer這邊,他是收信的工具、在consumer這邊,他是送信的工具(存在於郵局RabbitMQ)
  • Producer=寄信者
  • Consumer=收信者

連線架構

TCP connection -> connection -> channel

Message 送出後,consumer會回傳ack(acknowledgement),
確認message正確收取,否則會re-queue此message。

訊息模型

Producer -> exchange -> Queue -> exchange -> Consumer
                        Queue -> exchange -> Consumer

以下為完成傳遞必要項目,缺一不可

  • 1 由Producer產生Message
  • 2 Message送到左邊的Exchange
  • 3 左邊的Exchange把Message送到綁定的Queue
  • 4 右邊的Exchange從Queue取出Message
  • 5 右邊的Exchange把Message送給Consumer
Producer及Consumer都可以宣告Exchange及Queue
若是沒宣告就是使用預設的,或是RabbitMQ會自行建立
宣告同名的Exchange或是Queue,就表示用現有的,RabbitMQ不會新建立一個
宣告同名的Exchange或是Queue,參數必須完全相同,否則報錯

基本模型(Producer/Consumer)
Producer -> Queue -> Consumer
Producer/Consumer是位於app,Queue是位於RabbitMQ
多個consumer模型(Worker Queues)
1 massage平均分配給多個consumer
2 等consumer回覆之後,才送新的message下去
Exchange模型(Publish/Subscribe)
所有要收的consumer,自行綁定上該exchange

一 Produce/Consumer(基本模型)

官方網頁

二 Work Queues(message acknowledgement、message durable)

官方網頁
  • Round-robin dispatching 輪詢式派送
    輪流派送給每個註冊的worker

  • Massage acknowledgement 訊息確認
    msgs, err := ch.Consume(
      q.Name, // queue
      "",     // consumer
      false,  // auto-ack
      false,  // exclusive
      false,  // no-local
      false,  // no-wait
      nil,    // args
    )
    
    //幾本上auto-ack都要設成true,不然就要自己送
    //否則rabbitmq 會保留此message,造成資源消耗
    
    go func() {
      for d := range msgs {
        log.Printf("Received a message: %s", d.Body)
        dot_count := bytes.Count(d.Body, []byte("."))
        t := time.Duration(dot_count)
        time.Sleep(t * time.Second)
        log.Printf("Done")
        d.Ack(false)
      }
    }()
    

  • Massage durability 訊息耐用(即便rabbitmq重開,未送出的message也不會消失)
    q, err := ch.QueueDeclare(
      "hello",      // name
      true,         // durable
      false,        // delete when unused
      false,        // exclusive
      false,        // no-wait
      nil,          // arguments
    )
    //producer、consumer宣告的queue durable要設成true
    //宣告同名的queue參數必須相同,否則錯誤
    
    err = ch.Publish(
      "",           // exchange
      q.Name,       // routing key
      false,        // mandatory
      false,
      amqp.Publishing {
        DeliveryMode: amqp.Persistent,
        ContentType:  "text/plain",
        Body:         []byte(body),
    })
    //publishing DeliveryMode 要設成 amqp.Persistent
    
    

  • 完成一個work才繼續派送(Fair dispatch 一節)
    基本上RabbitMQ的派送是平均派送,當他收到就派送,
    所以有可能派送給還沒完成工作的worker。
    可設定prefetch count=1
    這時rabbitMQ會收到前一個message的acknowledgement才會派送下一個。
    
    注意,此時queue有可能被填滿,你需要多設定幾個woker。
    
    err = ch.Qos(
      1,     // prefetch count
      0,     // prefetch size
      false, // global
    )
    

三 Publish/Subscribe(one message to multiple consumers)

官方網頁

Published message are going to be broadcast to all the receivers.
  • Exchanges
    4 types of exchange:direct,topic,headers,fanout
    
    err = ch.ExchangeDeclare(
      "logs",   // name
      "fanout", // type
      true,     // durable
      false,    // auto-deleted
      false,    // internal
      false,    // no-wait
      nil,      // arguments
    )
    
    //fanout:broadcast all messages to all the queues it knows
    
    

  • Bindings(Binding exchange and queue)
    err = ch.QueueBind(
      q.Name, // queue name
      "",     // routing key
      "logs", // exchange
      false,
      nil
    )
    

四 Routing(用路由方式傳送message給不同Queue)

官方網頁

Subscribe only to a subset of the messages.
  • 基本概念
    exchange publish時,可以加上路由(routing key)
    queue可以綁定exchange及routing key
    一個queue可以綁定一個exchange及多個routing key
    這樣就可以一個exchange 用多種routing發布,然後consumer利用不同routing接收
    

  • Bindings
    //Binding 是綁定exchange 及 queue
    //Binding時可以設定路由,用routing key parameter
    
    err = ch.QueueBind(
      q.Name,    // queue name
      "black",   // routing key
      "logs",    // exchange
      false,
      nil)
    
    //route to black
    
    

  • Direct exchange
    //用direct exchange才能使用binding的routing key
    //用fanout exchange會忽略routing key參數,合理,所以他才叫fanout
    
    err = ch.ExchangeDeclare(
                    "logs_direct", // name
                    "direct",      // type
                    true,          // durable
                    false,         // auto-deleted
                    false,         // internal
                    false,         // no-wait
                    nil,           // arguments
    )
    
     q, err := ch.QueueDeclare(
                    "",    // name
                    false, // durable
                    false, // delete when usused
                    true,  // exclusive
                    false, // no-wait
                    nil,   // arguments
     )
            
    err = ch.QueueBind(
      q.Name, // queue name
      "",     // routing key
      "logs", // exchange
      false,
      nil
    )
    

五 Publish/Subscribe(one message to multiple consumers)

官方網頁

Published message are going to be broadcast to all the receivers.
  • Exchanges
    4 types of exchange:direct,topic,headers,fanout
    
    err = ch.ExchangeDeclare(
      "logs",   // name
      "fanout", // type
      true,     // durable
      false,    // auto-deleted
      false,    // internal
      false,    // no-wait
      nil,      // arguments
    )
    
    //fanout:broadcast all messages to all the queues it knows
    
    

  • Bindings(Binding exchange and queue)
    err = ch.QueueBind(
      q.Name, // queue name
      "",     // routing key
      "logs", // exchange
      false,
      nil
    )
    
  • 
    

  • 
    

正式產品需要注意的主題

官方說明文件

  • Connection Management

  • Error Handling

  • Connection Recovery

  • Concurrency

  • Metric Collection

參考頁面
Publisher Confirms and Consumer Acknowledgements
Production Checklist
Mornitoring

linux安裝

主目錄 /opt/rabbitmq
看相關變數 printenv | grep rabbitmq
執行檔 /opt/rabbitmq/sbin
執行檔 /usr/local/bin

服務啟動
systemctl start rabbitmq-server


mac home brew 安裝

官網說明

相關的scripts及cli tools安裝在/usr/local/opt/rabbitmq/sbin,
需要自己加到path
export PATH=$PATH:/usr/local/opt/rabbitmq/sbin

服務啟動
brew services start rabbitmq
brew services stoprabbitmq
啟動在前景
/usr/local/Cellar/rabbitmq/3.7.11/sbin/rabbitmq-server

管理 rabbitmqctl

用這個程式管理rabbitmq大部分項目,像是增加使用者、列出使用者、重啟、列出參數等。

範例



增加使用者 add_user [userName][password]
rabbitmqctl add_user admin admin
更改使用者tag,改成管理者 set_user_tags [...]
rabbitmqctl set_user_tags admin administrator
更改使用者權限 set_permissions [name]
rabbitmqctl set_permissions -p / username ".*" ".*" ".*"
更改密碼 change_password [username] [password]
rabbitmqctl change_password admin admin
查詢未確認的message(unacknowledged) list_queues [queueName] messages_ready messages_unacknowledged
sudo rabbitmqctl list_queues queueName messages_ready messages_unacknowledged

監控

監控程式啟動在 http://localhost:15672 使用瀏覽器就可以開啟,要先建立一個user
rabbitmq-plugins enable rabbitmq-management

外掛管理

rabbitmq-plugins [-n [node]] [-t [timeout]] [-l] [-q] [command] [command options]

Eg.
rabbitmq-plugins [-n ] [-t ] [-l] [-q] is_enabled [plugin1] [plugin2] 

參數

-n node

-q quiet

-h  help

list 列出所有外掛

is_enabled [plugin1][,[plugin2]]   查看plugin是否啟用

enable [plugin]  啟用外掛

範例


啟用外掛
rabbitmq-plugins enable rabbitmq_management

管理工具

rabbitmqadmin

2019年2月5日 星期二

Docker commands

build

官方

說明:用來建立image

常用參數

-t 指定repository:tag
docker build . -t repository:tag
eg: docker build . it myname/golang:1.14-centos7

-f 指定dockerfile
docker build -f dockerfile.debug

build with RUL

docker build github.com/create/docker-firefox 

run

官方

說明:用來執行docker

使用概念

  • 1 參數
  • 2 綁定
  • 3 執行的image

常用參數

--interactive,-i Keep STDIN open even if not attached

--tty,-t Allocate a pseudo-TTY

--ulimit Ulimit options

--mount Attach a filesystem mount to the container

--add-host Add a custom host-to-IP mapping(host:ip)

--attach,-a Attach to STDIN,STDOUT or STDERR

--link Add link to another container

--name Assign a name to the container

--publish,-p Publish a container's port(s) to the host

--publish-all, -P Publish all exposed ports to random ports

--expose expose a port

--rm Automatically remove the container when it exits

--ip IPv4 Address

--ip6 IPv6 address

-w lets the container command being executed inside directory given

Example

Assign name and allocate pseudo-TTY

$ docker run --name test -it debian
Set working directory

$  docker  run -w /path/to/dir/ -i -t  ubuntu pwd
Exporse port 80 of the container without publishing the port to host
Expose port但是沒有連結到host的外面。
$ docker run --expose 80 ubuntu bash
Publish a port
把container port 發布到host介面。
這裡是綁定8080到 host tcp 80 on 127.0.0.1
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash

2019年1月24日 星期四

Linux command netstat

netstat

netstat常用

說明:用來顯示網路系統的資訊(連線、路由表、網路卡等等)

使用概念

  • 第一個參數,決定資訊類型選擇
  • 輸出格式控制(format)

Type of information

(none)  By default, netstat displays a list of open sockets.  
        If you don't specify any address families, 
        then the active sockets of all configured address families will be printed.

--route , -r
       Display the kernel routing tables. See the description in route(8) for details.  
       netstat -r and route -e produce the same output.

--groups , -g
       Display multicast group membership information for IPv4 and IPv6.

--interfaces=iface , -I=iface , -i
       Display a table of all network interfaces, or the specified iface.

--masquerade , -M
       Display a list of masqueraded connections.

--statistics , -s(統計)
       Display summary statistics for each protocol.

None Options(none時候的參數)

netstat  [address_family_options]  
     [--tcp|-t]  [--udp|-u]  [--udplite|-U]  [--sctp|-S]  [--raw|-w]  
     [--listening|-l]  
     [--all|-a]  
     [--numeric|-n]  [--numeric-hosts] [--numeric-ports] [--numeric-users] 
     [--symbolic|-N] 
     [--extend|-e[--extend|-e]] 
     [--timers|-o] [--program|-p] 
     [--verbose|-v] 
     [--continuous|-c] 
     [--wide|-W] 
     [delay]

Common Options

 --verbose , -v
       Tell the user what is going on by being verbose. 
       Especially print some useful information about unconfigured address families.

   --wide , -W
       Do not truncate IP addresses by using output as wide as needed. 
       This is optional for now to not break existing scripts.

   --numeric , -n
       Show numerical addresses instead of trying to determine symbolic host, 
       port or user names.

   --numeric-hosts
       shows numerical host addresses but does not affect the resolution 
       of port or user names.

   --numeric-ports
       shows numerical port numbers but does not affect the resolution 
       of host or user names.

   --numeric-users
       shows numerical user IDs but does not affect the resolution of host or port names.

   --protocol=family , -A
       Specifies  the address families (perhaps better described as low level protocols) 
       for which connections are to be shown.  family is a comma (',') separated list of
       address family keywords like inet, inet6, unix, ipx, ax25, netrom, econet, and ddp.  
       This has the same effect as using the --inet|-4, --inet6|-6, --unix|-x, --ipx,
       --ax25, --netrom, and --ddp options.

       The address family inet (Iv4) includes raw, udp, udplite and tcp protocol sockets.
   -c, --continuous
       This will cause netstat to print the selected information every second continuously.

   -e, --extend
       Display additional information.  Use this option twice for maximum detail.

   -o, --timers
       Include information related to networking timers.

   -p, --program
       Show the PID and name of the program to which each socket belongs.

   -l, --listening
       Show only listening sockets.  (These are omitted by default.)

   -a, --all
       Show both listening and non-listening 
      (for TCP this means established connections) sockets.  
       With the --interfaces option, show interfaces that are not up

   -F
       Print routing information from the FIB.  (This is the default.)

   -C
       Print routing information from the route cache.

   delay
       Netstat will cycle printing through statistics every delay seconds.

Output(重要的輸出項目)

Proto
       The protocol (tcp, udp, udpl, raw) used by the socket.

   Recv-Q
       Established: The count of bytes not copied by the user program connected to this socket.  
       Listening: Since Kernel 2.6.18 this column contains the current syn back‐log.

   Send-Q
       Established: The count of bytes not acknowledged by the remote host.  
       Listening: Since Kernel 2.6.18 this column contains the maximum size of the syn backlog.

   Local Address
       Address and port number of the local end of the socket.  
       Unless the --numeric (-n) option is specified, 
       the socket address is resolved to its canonical  host  name
       (FQDN), and the port number is translated into the corresponding service name.

   Foreign Address
       Address and port number of the remote end of the socket.  Analogous to "Local Address."

   State
       The  state  of the socket. Since there are no states in raw mode 
       and usually no states used in UDP and UDPLite, this column may be left blank. 
       Normally this can be one of several values:
       ESTABLISHED
              The socket has an established connection.

       SYN_SENT
              The socket is actively attempting to establish a connection.

       SYN_RECV
              A connection request has been received from the network.

       FIN_WAIT1
              The socket is closed, and the connection is shutting down.

       FIN_WAIT2
              Connection is closed, and the socket is waiting for a shutdown from the remote end.

       TIME_WAIT
              The socket is waiting after close to handle packets still in the network.

       CLOSE  The socket is not being used.

       CLOSE_WAIT
              The remote end has shut down, waiting for the socket to close.

       LAST_ACK
              The remote end has shut down, and the socket is closed. Waiting for acknowledgement.

       LISTEN The socket is listening for incoming connections.  
              Such sockets are not included in the output 
              unless you specify the --listening (-l) or --all (-a) option.

       CLOSING
              Both sockets are shut down but we still don't have all our data sent.

       UNKNOWN
              The state of the socket is unknown.

   User
       The username or the user id (UID) of the owner of the socket.

   PID/Program name
       Slash-separated pair of the process id (PID) and process name of the process that owns the socket.  
       --program causes this column to be  included.   You  will  also
       need superuser privileges to see this information on sockets you don't own.  
       This identification information is not yet available for IPX sockets.

Example

顯示所有listening or established的tcp

netstat -at


顯示所有listening的tcp

netstat -lt


顯示tcp的統計

netstat -st


顯示每個tcp socket所屬的程式PID跟名稱

netstat -apt  //listening and established
netstat -pt   //established only
netstat -ap | grep ssh   //使用grep篩選

Linux command ulimit

ulimit

說明:顯示/設定使用者、群組等級的shell、process使用的資源限制

使用概念

  • 針對本session shell或session shell中的process做設置
  • 直接顯示或設定資源限制
  • 設定分為soft and hard
  • 使用ulimit做的設定,都是暫時性的(只針對本session)
  • 全系統的永久性修改,須改 /etc/security/limits.conf

Options

  
ulimit [-HSTabcdefilmnpqrstuvx [limit]]

-H: hard limit 執行緒不能在執行中改變數值(除非有root權限)
-S: soft limit 執行緒可在執行中改變數值

不加 -H,-S,默认兩者都设置。

LIMIT 的值,除了可以是数字,也可以是 
    hard, soft, unlimited

    hard: 当前 hard 限制值
    soft: 当前 soft 限制值
    unlimited: 无限制

不加 LIMIT,表示打印对应选项的资源(有多个选项时,会显示资源名和单位):

#ulimit -f
unlimited

#ulimit -c
0

#ulimit -c -f
core file size          (blocks, -c) 0
file size               (blocks, -f) unlimited

             
    -a     All current limits are reported
    -b     The maximum socket buffer size
    -c     The maximum size of core files created
    -d     The maximum size of a process's data segment
    -e     The maximum scheduling priority ("nice")
    -f     The  maximum  size  of files written by the shell and its
          children
    -i     The maximum number of pending signals
    -l     The maximum size that may be locked into memory
    -m     The maximum resident set size (many systems do not  honor
          this limit)
    -n     The maximum number of open file descriptors (most systems
          do not allow this value to be set)
    -p     The pipe size in 512-byte blocks (this may not be set)
    -q     The maximum number of bytes in POSIX message queues
    -r     The maximum real-time scheduling priority
    -s     The maximum stack size
    -t     The maximum amount of cpu time in seconds
    -u     The maximum number of processes  available  to  a  single user
    -v     The  maximum  amount  of  virtual memory available to the
          shell and, on some systems, to its children
    -x     The maximum number of file locks
    -T     The maximum number of threads

    If limit is given, and the -a option is not used, limit  is  the
    new  value  of  the  specified resource.  If no option is given,
    then -f is assumed.  Values are in 1024-byte increments,  except
    for  -t,  which is in seconds; -p, which is in units of 512-byte
    blocks; and -T, -b, -n, and -u, which are unscaled values.   The
    return  status is 0 unless an invalid option or argument is sup‐
    plied, or an error occurs while setting a new limit.

Example

顯示當前user 的open files(n)的hard設定

ulimit -Hn
1024

顯示當前user 所有設定

ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31204
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024         (file descriptor)
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31204
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited


把當前user的file descriptor數量暫時修改為10240

#ulimit -n 10240