Blog

  • 使用iperf测试网络的性能

    准备工作:

    安装epel源

    rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

    更新本地cache安装iperf

    yum makecache -y
    yum install iperf -y
    

     

    测试工作:

    192.168.0.244为server端,192.168.0.236为client端

    在server端和client端可以查看到传输的情况,还可以通过ifstat, iptraf查看网卡的流量

    TCP测试

    server(0.244)

    iperf -s -i 1
    
    -s 服务器模式
    
    -i 报告显示间隔秒数
    

    client(0.236)

    iperf -t 20 -i 1 -c 192.168.0.244
    

    -t 测试用时的秒数

    -c 客户端模式,后面接要连接的服务器

    服务端显示:

    ------------------------------------------------------------
    Server listening on TCP port 5001
    TCP window size: 32.0 KByte (default)
    ------------------------------------------------------------
    [ 4] local 192.168.0.244 port 5001 connected with 192.168.0.236 port 54921
    [ ID] Interval Transfer Bandwidth
    [ 4] 0.0- 1.0 sec 112 MBytes 938 Mbits/sec
    [ 4] 1.0- 2.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 2.0- 3.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 3.0- 4.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 4.0- 5.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 5.0- 6.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 6.0- 7.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 7.0- 8.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 8.0- 9.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 9.0-10.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 10.0-11.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 11.0-12.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 12.0-13.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 13.0-14.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 14.0-15.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 15.0-16.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 16.0-17.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 17.0-18.0 sec 112 MBytes 941 Mbits/sec
    [ 4] 18.0-19.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 19.0-20.0 sec 112 MBytes 942 Mbits/sec
    [ 4] 0.0-20.0 sec 2.20 GBytes 941 Mbits/sec
    

    客户端显示:

    ------------------------------------------------------------
    Client connecting to 192.168.0.244, TCP port 5001
    TCP window size: 23.2 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.0.236 port 54921 connected with 192.168.0.244 port 5001
    [ ID] Interval Transfer Bandwidth
    [ 3] 0.0- 1.0 sec 115 MBytes 965 Mbits/sec
    [ 3] 1.0- 2.0 sec 112 MBytes 937 Mbits/sec
    [ 3] 2.0- 3.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 3.0- 4.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 4.0- 5.0 sec 112 MBytes 935 Mbits/sec
    [ 3] 5.0- 6.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 6.0- 7.0 sec 112 MBytes 935 Mbits/sec
    [ 3] 7.0- 8.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 8.0- 9.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 9.0-10.0 sec 111 MBytes 934 Mbits/sec
    [ 3] 10.0-11.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 11.0-12.0 sec 111 MBytes 934 Mbits/sec
    [ 3] 12.0-13.0 sec 113 MBytes 945 Mbits/sec
    [ 3] 13.0-14.0 sec 113 MBytes 945 Mbits/sec
    [ 3] 14.0-15.0 sec 113 MBytes 946 Mbits/sec
    [ 3] 15.0-16.0 sec 111 MBytes 931 Mbits/sec
    [ 3] 16.0-17.0 sec 113 MBytes 948 Mbits/sec
    [ 3] 17.0-18.0 sec 111 MBytes 934 Mbits/sec
    [ 3] 18.0-19.0 sec 113 MBytes 945 Mbits/sec
    [ 3] 19.0-20.0 sec 113 MBytes 948 Mbits/sec
    [ 3] 0.0-20.0 sec 2.20 GBytes 942 Mbits/sec
    

    UDP 测试

     

    server(0.244): iperf -u -s -i 1

    client(0.236): iperf -t 20 -i 1 -u -b 1000M -c 192.168.0.244

     

    -u 使用udp协议

    -b 后面接每秒带宽发送量(udp下适用)

     

    服务器显示

    ------------------------------------------------------------
    Server listening on UDP port 5001
    Receiving 1470 byte datagrams
    UDP buffer size: 224 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.0.244 port 5001 connected with 192.168.0.236 port 34489
    [ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
    [ 3] 0.0- 1.0 sec 128 KBytes 1.05 Mbits/sec 0.013 ms 0/ 89 (0%)
    [ 3] 1.0- 2.0 sec 128 KBytes 1.05 Mbits/sec 0.010 ms 0/ 89 (0%)
    [ 3] 2.0- 3.0 sec 128 KBytes 1.05 Mbits/sec 0.015 ms 0/ 89 (0%)
    [ 3] 3.0- 4.0 sec 128 KBytes 1.05 Mbits/sec 0.012 ms 0/ 89 (0%)
    [ 3] 4.0- 5.0 sec 128 KBytes 1.05 Mbits/sec 0.008 ms 0/ 89 (0%)
    [ 3] 5.0- 6.0 sec 129 KBytes 1.06 Mbits/sec 0.008 ms 0/ 90 (0%)
    [ 3] 6.0- 7.0 sec 128 KBytes 1.05 Mbits/sec 0.009 ms 0/ 89 (0%)
    [ 3] 7.0- 8.0 sec 128 KBytes 1.05 Mbits/sec 0.019 ms 0/ 89 (0%)
    [ 3] 8.0- 9.0 sec 128 KBytes 1.05 Mbits/sec 0.011 ms 0/ 89 (0%)
    [ 3] 9.0-10.0 sec 128 KBytes 1.05 Mbits/sec 0.009 ms 0/ 89 (0%)
    [ 3] 10.0-11.0 sec 128 KBytes 1.05 Mbits/sec 0.014 ms 0/ 89 (0%)
    [ 3] 11.0-12.0 sec 129 KBytes 1.06 Mbits/sec 0.014 ms 0/ 90 (0%)
    [ 3] 12.0-13.0 sec 128 KBytes 1.05 Mbits/sec 0.009 ms 0/ 89 (0%)
    [ 3] 13.0-14.0 sec 128 KBytes 1.05 Mbits/sec 0.017 ms 0/ 89 (0%)
    [ 3] 14.0-15.0 sec 128 KBytes 1.05 Mbits/sec 0.016 ms 0/ 89 (0%)
    [ 3] 15.0-16.0 sec 128 KBytes 1.05 Mbits/sec 0.011 ms 0/ 89 (0%)
    [ 3] 16.0-17.0 sec 128 KBytes 1.05 Mbits/sec 0.012 ms 0/ 89 (0%)
    [ 3] 17.0-18.0 sec 129 KBytes 1.06 Mbits/sec 0.017 ms 0/ 90 (0%)
    [ 3] 18.0-19.0 sec 128 KBytes 1.05 Mbits/sec 0.018 ms 0/ 89 (0%)
    [ 3] 19.0-20.0 sec 128 KBytes 1.05 Mbits/sec 0.012 ms 0/ 89 (0%)
    [ 3] 0.0-20.0 sec 2.50 MBytes 1.05 Mbits/sec 0.013 ms 0/ 1785 (0%)
    

    客户端显示

    ------------------------------------------------------------
    Client connecting to 192.168.0.244, UDP port 5001
    Sending 1470 byte datagrams
    UDP buffer size: 224 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.0.236 port 34489 connected with 192.168.0.244 port 5001
    [ ID] Interval Transfer Bandwidth
    [ 3] 0.0- 1.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 1.0- 2.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 2.0- 3.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 3.0- 4.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 4.0- 5.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 5.0- 6.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 6.0- 7.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 7.0- 8.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 8.0- 9.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 9.0-10.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 10.0-11.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 11.0-12.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 12.0-13.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 13.0-14.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 14.0-15.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 15.0-16.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 16.0-17.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 17.0-18.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 18.0-19.0 sec 129 KBytes 1.06 Mbits/sec
    [ 3] 19.0-20.0 sec 128 KBytes 1.05 Mbits/sec
    [ 3] 0.0-20.0 sec 2.50 MBytes 1.05 Mbits/sec
    [ 3] Sent 1785 datagrams
    [ 3] Server Report:
    [ 3] 0.0-20.0 sec 2.50 MBytes 1.05 Mbits/sec 0.013 ms 0/ 1785 (0%)
    
  • sublime的使用

    ubuntu下的安装方法:

    add-apt-repository ppa:webupd8team/sublime-text-2
    apt-get update
    apt-get install sublime-text

    ctrl+tab可以快速切换标签

    ctrl+w关闭当前的标签

    ctrl+r快速跳转到文档中相关函数

    ctrl+p加#可以在当前文件中进行搜索

    ctrl+g加上数字跳到对应的行

    ctrl+`调出console

    使用vim模式:
    按下 Shift + Command + P 调出命令面板,输入 settings user 调出 Preferences:Settings – User,这时添加:

    {
     "ignored_packages": []
    }
  • proxmox虚拟化环境-pve

    proxmox VE 是一个非常棒的同时支持openvz和kvm虚拟化环境平台,简称PVE,基于debian制作。
    下载好镜像之后,可以用u盘安装,http://pve.proxmox.com/wiki/Install_from_USB_Stick
    在linux下使用dd命令制作一个可以安装用的u盘:
    dd if=pve-cd.iso of=/dev/XYZ bs=1M
    安装过程,略
    打开https://ip:8006,使用安装时设置的用户名和密码登陆。
    可以创建的有VM和CT,VM即kvm虚拟机,kvm可以装windows,linux;CT即ConTainer(容器),也就是openvz虚拟机,不能装windows。
    制作CentOS的模板
    对于 kvm 虚拟化的资源 210 是里面的 id ,复制一台新机器 211
    qm clone 210 211 -full 1 -format qcow2 -name ready-to-use.jpuyy.com -storage data

    kvm 从宿主机切换虚拟机使用 terminal

    在宿主机配置文件中编辑,kvm 虚拟机为 2549

    /etc/pve/qemu-server/2549.conf

    添加一行

    serial0: socket

    虚拟机为 centos6

     vim /boot/grub/grub.conf

    在 kernel 一行中追加

    console=tty0 console=ttyS0

    示例

    kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=UUID=dbc46481-4b4c-420f-9b32-8e8ac82a4979 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=tty0 console=ttyS0

    这些做完,

    qm stop 2549
    qm start 2549

    重启机器后

    qm terminal 2549

    即可从宿主机进入虚拟机 terminal.

    参考:

    https://pve.proxmox.com/wiki/Serial_Terminal

    https://pve.proxmox.com/wiki/Qm_manual

  • linux shell操作快捷键

    使用快捷键节省生命

    使用命令操作linux,需要敲不少命令,而命令并不是一次性敲对的,中间改主意的时候很多,所以需要快速对已经打出的命令进行修改。

    剪切光标至行首的字符,效果等同于ctrl+c,也等同于按backspace不放。打字打到一半改变主意就用这个快捷键。

    ctrl+u

    剪切光标至行尾的字符

    ctrl+k

    清除屏幕内容,等同于clear命令

    ctrl+l

    清除最后一个单词,常用

    ctrl+w

    删除前一个字符,同退格键

    ctrl+h

    粘贴所剪切的字符,不是系统剪切板,而是执行剪切或删除命令的undo

    ctrl+y

    删除后一个字符,相当于delete键

    ctrl+d

    光标的跳转

    有些时候shell左右方向键移动很管用,但是Home和End却不管用。这里有万用的跳转方式:

    光标向右(前)

    ctrl+f

    光标向左(后)

    ctrl+b

    跳转到行头,a代表alphabet

    ctrl+a

    跳转到行尾,e代表end

    ctrl+e

    行头和当前光标位置互相跳转

    ctrl+x

    回车

    ctrl+j
    ctrl+m
    

    重用历史命令

    查看历史3条命令

    history 3

    历史记录里都有一个编号,比如说是233,那么重新执行这条命令

    !233

    执行倒数第 4 条命令

    !-4

    再次执行上一条命令

    !!

    搜索历史命令(比history | grep ping更快),回车后执行

    ctrl+r

    显示上一条命令

    ctrl+p

    显示下一条命令

    ctrl+n

    显示最近命令的第一个参数

    !^

    显示最近命令的最后一个参数

    !$

    执行一条命令之后,还要执行一次与上次命令稍有出入的命令,可以使用替换,如

    git checkout dev

    再切换到 master,可以这样

    ^dev^master

    执行最近一次以!后面字母开头的命令

    !vim

    参考:

    http://www.linuxplanet.com/linuxplanet/tutorials/6639/1

  • linux双网卡绑定

    以下操作均在CentOS下,网卡绑定有多种模式,这里是为了增加带宽

    安装需要的组件

    yum install ethtool -y

    添加一个ifcfg-bond0的配置文件,做为master,需要绑定的物理网卡(ifcfg-eth0, ifcfg-eth1)做为slave

    vim /etc/sysconfig/network-scripts/ifcfg-bond0

    添加

    DEVICE=bond0
    ONBOOT=yes
    IPADDR=192.168.1.12
    NETMASK=255.255.255.0
    NETWORK=192.168.1.0
    USERCTL=no
    BOOTPROTO=none

    vim /etc/sysconfig/network-scripts/ifcfg-eth0

    添加

    DEVICE=eth0
    ONBOOT=yes
    USERCTL=no
    MASTER=bond0
    SLAVE=yes
    BOOTPROTO=none

    vim /etc/sysconfig/network-scripts/ifcfg-eth1

    添加

    DEVICE=eth1
    ONBOOT=yes
    USERCTL=no
    MASTER=bond0
    SLAVE=yes
    BOOTPROTO=none

    USERCTL=yes/no 是否允许非root用户控制该设备

    BOOTPROTO=none/static/dhcp 指定启动协议,这里不指定

    如果还有更多网卡,照上面写即可

    接下来需要在CentOS中注册一下bonding模块

    vim /etc/modprobe.d/bonding.conf

    添加

    alias bond0 bonding
    options bond0 mode=4 miimon=100

    mode参数见:

    http://unixfoo.blogspot.com/2008/02/network-bonding-part-ii-modes-of.html

    • Mode 0 (balance-rr)
      This mode transmits packets in a sequential order from the first available slave through the last. If two real interfaces are slaves in the bond and two packets arrive destined out of the bonded interface the first will be transmitted on the first slave and the second frame will be transmitted on the second slave. The third packet will be sent on the first and so on. This provides load balancing and fault tolerance.
    • Mode 1 (active-backup)
      This mode places one of the interfaces into a backup state and will only make it active if the link is lost by the active interface. Only one slave in the bond is active at an instance of time. A different slave becomes active only when the active slave fails. This mode provides fault tolerance.
    • Mode 2 (balance-xor)
      Transmits based on XOR formula. (Source MAC address is XOR’d with destination MAC address) modula slave count. This selects the same slave for each destination MAC address and provides load balancing and fault tolerance.
    • Mode 3 (broadcast)
      This mode transmits everything on all slave interfaces. This mode is least used (only for specific purpose) and provides only fault tolerance.
    • Mode 4 (802.3ad)
      This mode is known as Dynamic Link Aggregation mode. It creates aggregation groups that share the same speed and duplex settings. This mode requires a switch that supports IEEE 802.3ad Dynamic link.
    • Mode 5 (balance-tlb)
      This is called as Adaptive transmit load balancing. The outgoing traffic is distributed according to the current load and queue on each slave interface. Incoming traffic is received by the current slave.
    • Mode 6 (balance-alb)
      This is Adaptive load balancing mode. This includes balance-tlb + receive load balancing (rlb) for IPV4 traffic. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the server on their way out and overwrites the src hw address with the unique hw address of one of the slaves in the bond such that different clients use different hw addresses for the server.

    mode的值表示工作模式,共有7种模式,常用的为0,1,4三种。

    mode=0表示load balancing (round-robin)为负载均衡方式,两块网卡都工作。
    mode=1表示fault-tolerance (active-backup)提供冗余功能,工作方式是主备的工作方式,也就是说默认情况下只有一块网卡工作,另一块做备份
    mode=4是通用的802.3ad协议,类似于第一种,接思科交换机时需要选LACP模式。

    miimon是用来进行链路监测的。比如:miimon=100,那么系统每100ms监测一次链路连接状态,如果有一条线路不通就转入另一条线路;

    重启网络服务

    service network restart

    使用watch -n 1可以每秒更新bond0的状态

    watch -n 1 'cat /proc/net/bonding/bond0'

    参考:http://www.cyberciti.biz/howto/question/static/linux-ethernet-bonding-driver-howto.php

  • 使用openssl创建CSR文件

    在申请正规的ssl证书之前,需要先在本机生成CSR文件
    Login to your server via your terminal client (ssh). At the prompt, type:

    登陆ssh,在提示符下输入命令:

    openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr

    where server is the name of your server.
    这里是server.csr,自己根据自己情况命名。

    This will begin the process of generating two files: the Private-Key file for the decryption of your SSL Certificate, and a certificate signing request (CSR) file used to apply for your SSL Certificate. This command will prompt for the following X.509 attributes of the certificate:

    这将生成两个文件,一个是私钥文件用于解密你的SSL证书,还有就是用于申请SSL的证书签名请求CSR文件。

    在生成过程中有如下提示:
    Country Name (C): Use the two-letter code without punctuation for country, for example: US or CA.
    State or Province (S): Spell out the state completely; do not abbreviate the state or province name, for example: California.
    Locality or City (L): The Locality field is the city or town name, for example: Berkeley.
    Organization (O): If your company or department has an &, @, or any other symbol using the shift key in its name, you must spell out the symbol or omit it to enroll, for example: XY & Z Corporation would be XYZ Corportation or XY and Z Corportation.
    Organizational Unit (OU): This field is the name of the department or organization unit making the request.
    Common Name (CN): The Common Name is the Host + Domain Name. It looks like “www.company.com” or “company.com”.
    Please do not enter your email address, challenge password or an optional company name when generating the CSR.

    在生成CSR过程中,请不要填写email,强密码和备用公司名

    最后将生成server.csr

    如果要自认证证书 server.crt

    openssl x509 -req -in server.csr -signkey server.key -out server.crt

    计算 crt 的 finger print

    SHA-256
    openssl x509 -noout -fingerprint -sha256 -inform pem -in [certificate-file.crt] 
     
    SHA-1
    openssl x509 -noout -fingerprint -sha1 -inform pem -in [certificate-file.crt]
    
    MD5
    openssl x509 -noout -fingerprint -md5 -inform pem -in [certificate-file.crt]