linux

文章目录

    People disagree with me. I just ignore them.
    – Linus Torvalds (regarding the use of C++ for the Linux kernel.)

    File size change when copied with scp

    http://www.linuxforums.org/forum/newbie/188379-file-size-change-when-copied-scp.html

    md5sum is an excellent indication.

    Consider a 9K file. On a disk formatted with 4K block size, it will take 3 blocks which is 12K. On a machine with 8K blocks the same file will only take 2 blocks but 16K. This may be part of what’s happening.

    查看 block size 的方法:

    sudo tune2fs -l /dev/sda1 | grep "Block size"
    

    自从开始Linux 软件平台开发,经常遇到“Block Size”。但经常发现此block size非彼block size。意义不一样,大小值也不一样。Open Source的东东有时候也挺烦的。下面是自己的总结。通常Linux的“block size”指的是1024 bytes,Linux用1024-byte blocks 作为buffer cache的基本单位。但linux的文件系统的block确不一样。例如ext3系统,block size是4096。使用tune2fs可以查看带文件系统的磁盘分区的相关信息,包括block size。

    查看搜索二进制程序中的字符串

    My first thought was that there must be list of authoritative WHOIS servers somewhere on the Internet. I checked the “whois” command on my Linux machine to see if it was using a built in list or something else:

    > strings whois | grep whois | less
    * whois.corenic.net
    * whois.denic.de
    * whois.cat
    * whois.nic.ad.jp
    * whois.jprs.jp
    * whois.arin.net
    * whois.nic.mil
    

    产看当前目录下所有目录所占用的磁盘空间

    du | sort -nr | less
    

    fail to run “sudo cat hosts>>/etc/hosts”

    permission denied

    http://www.linuxquestions.org/questions/linux-software-2/sudo-and-permission-denied-651619/

    sudo sh -c "cat hosts>>/etc/hosts"
    

    为 GITLAB 生成 pub key

    SSH key allows you to establish a secure connection between your computer and GitLab

    To generate a new SSH key just open your terminal and use code below.

    $ ssh-keygen -t rsa -C "<your-email>"
    

    Creates a new ssh key using the provided email

    Generating public/private rsa key pair…

    Next just use code below to dump your public key and add to GitLab SSH Keys

    $ cat ~/.ssh/id_rsa.pub
    

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc….

    这里需要注意的是,默认情况下需要保证 key 的名字为 id_rsa 。换成其他名字无法识别。

    应该有参数在 git 操作时制定key。

    添加一个管理员账号

    Ubuntu 12.04

    === 正确的做法 ===

    1. 添加一个普通用户
      $ useradd username
      如果需要创建用户根目录, 并指定 shell
      $ useradd -d /home/username -s /bin/bash -m username
    2. 设置登陆密码
      $ passwd username
    3. 将用户添加到管理员组
      $ usermod -a -G sudo username

    tips: 使用 adduser 命令可以以交互式的方式添加用户。

    查看一个用户所属的组:
    $ groups username

    === 丑陋的做法 ===

    直接修改 /etc/sudoers, 增加两行
    ## Allows just user “username” to run all commands as root
    username ALL=(ALL) ALL

    丑陋之处在于,如果有成百上千的用户需要添加,这个文件就难以维护

    === 不正确的做法 ===
    修改 /etc/passwd, 增加
    username:x:0:502::/home/username:/bin/bash

    即将 account ID 设置为 0, 这样,你就创建了一个可以把 root 用户剔除的用户。

    参考:
    http://serverfault.com/questions/58378/add-new-user-with-root-access-in-linux
    %% }}}

    sudo group 和 admin group 的区别

    实际上是等同的。

    Ubuntu 12.04 新加入了 sudo group;admin 是为了兼容之前的叫法,所以保持了下来。

    参考:
    http://askubuntu.com/questions/43317/what-is-the-difference-between-the-sudo-and-admin-group

    如何查看,CPU 的核数

    Ubuntu 12.04

    $ cat /proc/cpuinfo
    

    里面有几个 processor, 就代表有几个核。

    核数过多的情况下,可以使用

    $ grep -c processor /proc/cpuinfo
    

    使用 curl 发送 POST 请求

    curl -d "name=zhongwei&sex=male" http://localhost/test
    curl https://YOUR-API-KEY@hostedgraphite.com/api/v1/sink --data-binary "conc_users 59"
    

    tips:
    返回结果后,命令行提示符会缀在结果后面。看上去很不时尚,解决方法是,在后面缀上
    ”; echo”

    curl -d "name=zhongwei&sex=male" http://localhost/test; echo
    

    发送 DELETE 请求

    curl -X DELETE http://localhost/persons/1
    

    发送 PUT 请求

    curl -X PUT -d "start_at=2013-04-01&title=test2" http://localhost/news/1
    

    参考:

    • http://curl.haxx.se/docs/manual.html
    • http://stackoverflow.com/questions/12849584/automatically-add-newline-at-end-of-curl-command

    kill signal

    经常会看到重启一个服务时使用

    killall -HUP <process_name>
    

    HUP 的意思是:
    It restarts the process from the begging (reloading any config files it uses)

    开启 ssh 服务

    Ubuntu desktop 版本默认不安装 ssh server 服务,即无法在其他机器通过 ssh 登陆该机器。
    解决的方法是,安装 openssh-server

    $ sudo apt-get install openssh-server
    

    tar

    tar -xJf test.tar.xz
    gtar xvzf test.tgz 
    tar xvjf filename.tar.bz2
    tar xvzf test.tar.gz
    

    在 Centos 中设置开机启动项

    在 /etc/rc.d/rc.local 中添加需要执行的命令即可。

    允许访问指定端口

    显示防火墙的现有规则

    iptables -L
    

    开放指定端口,例如 8081

    iptables -A INPUT -p tcp --dport 8081  -j ACCEPT
    

    参考:
    * https://help.ubuntu.com/community/IptablesHowTo

    查看上次系统重启时间

    $ last reboot
    

    检索历史操作

    $ history | grep kill
    

    最近常用命令统计

    story | awk '{CMD[$2]++;count++;} END { for (a in CMD )print CMD[a] " " CMD[a]/count*100 "% " a }' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
    
     1  134  26.8%  ls
     2  61   12.2%  cd
     3  56   11.2%  dig
     4  46   9.2%   vim
     5  34   6.8%   git
     6  33   6.6%   exit
     7  17   3.4%   whois
     8  14   2.8%   ssh
     9  10   2%     rm
    10  10   2%     compass
    

    mail 的基本操作

    • z 下一页
    • z- 上一页

    Centos 上安装 pysqlite 模块

    ImportError: No module named pysqlite2

    yum install sqlite-devel -y
    pip install pysqlite
    

    ImportError: No module named _sqlite3

    $ find /usr -name _sqlite3.so      
    /usr/lib64/python2.6/lib-dynload/_sqlite3.so
    cp /usr/lib64/python2.6/lib-dynload/_sqlite3.so /usr/local/python/lib/python2.7/sqlite3/
    

    动态产看当前建立的连接

    watch -n 1 -d 'netstat -nap | grep 80 | grep EST'
    

    which processes are using your swap

    • http://lserinol.blogspot.fr/2013/05/which-processes-are-using-your-swap.html

    查看一个域名的整个跳转过程

    curl -vL sunzhongwei.com
    

    重复执行 N 次

    for i in {1..5}; do echo hello; done
    

    如果是后台执行:

    for i in {1..5}; do nohup curl localhost:8888 >/dev/null 2>&1 & done
    

    注意: & 符号和 ; 重复了,所以省去 & 后面的 ;

    参考:

    • http://stackoverflow.com/questions/6904505/fire-a-multi-curl-request-and-dont-wait-for-the-response-php
    • http://stackoverflow.com/questions/6666245/running-bash-pipe-commands-in-background-with-ampersand

    使用 netcat 发送 TCP/UDP 数据包

    • TCP -> echo “YOUR-API-KEY.conc_users 1.2” | nc carbon.hostedgraphite.com 2003
    • UDP -> echo “YOUR-API-KEY.conc_users 59” | nc -uw0 carbon.hostedgraphite.com 2003

    目录权限问题

    IOError: [Errno 13] Permission denied: ‘/var/log/app.log’

    $ sudo chown -R zhongwei:zhongwei /var/log/app.log
    

    找出最大的文件/目录

    文件:

    find . -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}
    

    目录:

    find . -type d -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}
    

    参考:

    • http://superuser.com/questions/9847/linux-utility-for-finding-the-largest-files-directories

    Ubuntu 下解压 7z 压缩包

    sudo apt-get install p7zip-full
    7z x something.7z
    

    查看系统上次重启的时间

    $ last reboot | less

    reboot   system boot  3.13.0-74-generi Sat Apr 16 20:03 - 00:35 (2+04:31)  
    reboot   system boot  3.13.0-74-generi Wed Apr 13 00:25 - 00:35 (6+00:09) 
    

    获取一个文件的全路径

    readlink -f

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式