Category: Linux

  • ethtool检测和管理网卡

    ethtool是检测和管理网卡设置的工具

    ethtool的实用之处之一是可以查看网线是否插到网卡上

    如果有显示信息Link detected: yes,则说明在链路状态,表明插上网线并且在通讯

    #ethtool em1
    
    Settings for em1:
    Supported ports: [ TP ]
    Supported link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Supports auto-negotiation: Yes
    Advertised link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 1
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: Unknown
    Supports Wake-on: g
    Wake-on: d
    Link detected: yes

    如果显示Link detected: no,表明插上网线但没有链路

    #ethtool em2
    Settings for em2:
    Supported ports: [ TP ]
    Supported link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Supports auto-negotiation: Yes
    Advertised link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: Unknown!
    Duplex: Half
    Port: Twisted Pair
    PHYAD: 1
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: Unknown
    Supports Wake-on: g
    Wake-on: d
    Link detected: no

    如果找不到设备,则没有网线插入

    #ethtool em3
    Settings for em3:
    Cannot get device settings: No such device
    Cannot get wake-on-lan settings: No such device
    Cannot get message level: No such device
    Cannot get link status: No such device
    No data available

    查看 veth 设备对的 peer 另一端

    ethtool -S veth1
  • chattr lsattr命令

    chattr与lsattr可以修改文件属性,是比chmod, chown的属性更底层的属性。

    chattr可以结合 -减  +增  = 指定 属性

    查看属性,默认只有一个e属性

    #lsattr /etc/hosts
    -------------e- /etc/hosts

    添加i属性,则这个文件将不能修改,删除,重命名,追加,也不能创建硬链接。root也只能在回收这个属性值之后才能正常使用。

    # chattr +i /etc/hosts
    # lsattr /etc/hosts
    ----i--------e- /etc/hosts
    rm /etc/hosts会出现Operation not permitted

    添加a属性,使文件只能被追加,不能被删除,常用于日志文件,比如说在logrotate的时候对日志文件先-a,然后再+a。如nginx的logrotate

    # This configuration is from jpuyy 2013-12-16
     /web/nginx/logs/access.log {
     weekly
     rotate 5
     compress
     sharedscripts
     prerotate
     /usr/bin/chattr -a /web/nginx/logs/access.log
     endscript
     sharedscripts
     postrotate
     /usr/bin/killall -HUP nginx
     /usr/bin/chattr +a /web/nginx/logs/access.log
     endscript
     }

    chattr -R 递规更改属性

  • 同时向多个目标主机高效传输文件

    转自

    http://engineering.tumblr.com/post/7658008285/efficiently-copying-files-to-multiple-destinations

    需求:从一个源主机local-server拷贝out-1.ogv到两个目的地(或多个),remote-1.jpuyy.com和remote-2.jpuyy.com。不需要安全加密传输。

    平常的操作是从local-server先拷到remote-1,再拷到remote-2,需要两倍时间。如果同时拷,因为local-server的上传带宽要分给两个上传用,所以还是需要两倍的时间。如果你的文件是好几百GB,几TB,那么将等的非常痛苦。

    现在的方法是将local-server与remote-1和remote-2串接起来,形成一个链,由local-server发送最大带宽到remote-1,remote-1接到后不仅自己存,也同时最大带宽传给remote-2,如果还有更多的主机可继续接上。

    首先在最后一台主机(这里是remote-2)上运行

    nc -l 1234 | pigz -d | tar xvf -

    然后在链上的点(这里是remote-1)上执行

    mkfifo myfifo
    nc remote-2.jpuyy.com 1234 <myfifo &
    nc -l 1234 | tee myfifo | pigz -d | tar xvf -

    现在链接打通了,只需要在local-server上向第一台机传送

    tar cv out-1.ogv | pigz | nc remote-1.jpuyy.com 1234

    这样这条链上的所有主机几乎同时接收,就达到了我们的目的,节省了时间。

  • bash while循环

    每0.01秒curl一下某网址

    while true ; do curl http://192.168.1.26/status; sleep 0.01;done

    逐行输出某日志文件

    cat 20131106_access.log1 | while read LINE; do echo $LINE; sleep 0.01 ; done

    checkCaps.sh检测CapsLock是否变化

    #!/bin/bash
    while true
    do
        stat="`/usr/bin/xset -q | grep Caps | awk '{print $4}'`"
        if [ "$stat" == "$statswap" ];then
            echo 
        else
            /usr/bin/notify-send  "Capslock changed"
        fi
        statswap=$stat
    done
  • 使用cpuburn-in与memtester对linux服务器进行压测

    如何将linux下的cpu与内存跑满从而测试其稳定性,用电量等。这里使用cpuburn-in与memtester。

    cpuburn-in(http://cpuburnin.com/)的使用方法

    首先要查看cpu的核心数,然后运行对应数量的多个实例,后面只要跟需要测试的分钟数即可,一天为1440分钟,自己在测试过程中CentOS6.4 x86_64需要安装好glibc.i686包。

    nohup ./cpuburn-in 1440 &

    每个cpu的使用百分比可以通过htop查看

    memtester(http://pyropus.ca/software/memtester/)的使用方法

    下载解压,编译 make之后得到memtester,后面跟参数为测试的内存大小,可以trial and error的方法得到最大的可测内存,比如32GB的总内存,可以填31000测试

    nohup ./memtester 31000 &

    同样可以使用htop查看内存的使用率

    2014-11-26

    unixbench

    直接运行

    nohup ./Run

  • ubuntu移动home目录

    有时候/home目录可能不够用,将现有的目录移动到新的分区需要如下六个步骤操作:

    1.设置目标分区

    2.将目标分区使用fstab挂载到/media/home并重启

    3.使用rsync将/home的数据移到/media/home

    4.将fstab中的新分区/media/home改为/home

    5.将/home改名为/old_home并重启

    6.将/old_home删掉

    具体操作参考:

    https://help.ubuntu.com/community/Partitioning/Home/Moving

    也可参考:

    http://www.ibm.com/developerworks/cn/linux/l-tip-prompt/tip05/