23 November 2014

JDK

Install Oracle JDK 6 with jdk-6u45-linux-x64.bin:

chmod u+x jdk-6u45-linux-x64.bin
# jdk-6u45-linux-x64.bin is a self-extracting file. Running the following
# command will create a directory named `jdk1.6.0_45` in the current directory.
./jdk-6u45-linux-x64.bin

Install Oracle JDK 7 with jdk-7u80-linux-x64.tar.gz. Just extracting the file.

Sector vs Block

ext4 allocates storage space in units of “blocks”. A block is a group of sectors between 1KiB and 64KiB, and the number of sectors must be an integral power of 2.

https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout

AIO

File Access Permissions

apue.2e’s section 4.5 explains file access permissions really well. And it has a clear explanation of read and execution permissions on directories.

Permission

Default Permission

File default Permissions are 666. Directory default permissions are 777.

Utilities

rsync

Compare directory tree:

rsync --delete -a -n -c -v -e ssh root@linode:~/test/linux-2.6.11/ linux-2.6.11

yum

  • yum search does not suport *. yum list supports *.

AWK

File data contains:

| a | b |

awk -F '|' '{print $2, $3}' data produces:

 a   b

Search for patterns in Java and SQL files:

ack -l --type-set  jing:ext:.java,.sql line_spec src

LVM

Extend a partition:

lvextend -L+10G /dev/mapper/vg_root-lvvar
resize2fs /dev/vg_root/lvvar

Ack-grep

ack-grep -g '.*\.md' | ack -x system

Use a type temporarily:

> mkdir test
> echo google > 1.xx
> echo google > 1.xx
> echo google > 1.yy
> ack -k google
> ack --type-set zz:ext:xx,yy google
1.xx
1:google

1.yy
1:google
> cat ~/.ackrc

Add a type permanently:

> cat ~/.ackrc
--type-set=md:ext:md,markdown

Ag

ag -G '\.proto$' BatchRequest

Grep

With -P, grep support \d pattern.

Ncat

Chain Ncats Together’s second code example does not work. It should be:

host3$ ncat -l
host2$ ncat -l --sh-exec "ncat host3"
host1$ ncat host2

Use ncat -zv hostname port-number to test whether a connection can be made. An alternative way is to use nc -zv hostname port-number.

Now host3 and host1 can talk to each other.

Tcpdump

Lsof

Examples:

lsof -r 1 -u jing -i -c chrome -a

Ping

64 bytes from 192.168.1.20: icmp_seq=264 ttl=62 time=1.12 ms

64 is the length of data wrappted in IP packet. The IP packet length is 84.

Sendip

sudo sendip -p ipv4 -is 10.0.0.8 -p udp -us 5070 -ud 5060 -d "Hello" -v 10.0.0.8
sudo tcpdump -i lo -nn -vv -X -S port 5070

Find

Prune action

find -regextype posix-egrep -regex '.*\.((h)|(c)|(cc)|(hpp)|(cpp))'
find \( -name '*.cc' -or -name '*.h' \) -exec readlink -f {} \;
find -regex ".*\.\(h\|cc\)" -exec readlink -f {} \;

Jekyll

http://jekyllbootstrap.com/usage/jekyll-quick-start.html

brew install ruby
gem install jekyll bundler

bundle exec jekyll serve -watch serving the local web site.

Dpkg

dpkg --list package status: http://ubuntu.aspcode.net/view/63540014012470517533550/what-do-the-various-dpkg-flags-like-ii-rc-mean

Nohup

nohup shell_command > log 2>&1 < /dev/null &

SSH

The following command sets up a SOCKS5 proxy server listening on 7979. I run this command on my laptop. ssh-server is a server which can access web sites which are inaccessible to my laptop.

ssh -ND 7979 ssh-user@ssh-server
+---------------+ ssh     +---------------+
| proxy         |-------->|   ssh-server  |
+---------------+         +---------------+

PS

--lienes only makes sence with --headers

$ ps --headers -e --lines 10
PID TTY          TIME CMD
1255 ?        00:00:00 polkitd
1275 tty1     00:00:00 agetty
1284 ttyS0    00:00:00 agetty
1293 ?        00:00:00 ntpd
1302 ?        00:00:00 nginx
1304 ?        00:00:00 nginx
1368 ?        00:00:00 google_network_
1369 ?        00:00:00 google_clock_sk
1370 ?        00:00:01 google_accounts
PID TTY          TIME CMD
1384 ?        00:00:00 sshd
1438 ?        00:00:02 ssserver
2108 ?        00:00:00 kworker/0:5
3018 ?        00:00:00 sshd
3020 ?        00:00:00 systemd
3021 ?        00:00:00 (sd-pam)
3075 ?        00:00:00 sshd
3076 pts/0    00:00:00 bash
3505 ?        00:00:00 kworker/u2:1
PID TTY          TIME CMD
3572 ?        00:00:00 kworker/u2:0
3675 ?        00:00:00 kworker/0:1
3733 ?        00:00:00 kworker/u2:2
3752 pts/0    00:00:00 ps

--cols only controls the width of CMD column. And a --cols 1 and --cols 2 since the column header CMD must show up fully.

$ ps -ef --cols 1 | head -3
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 03:02 ?        00:00:01 /sb
root         2     0  0 03:02 ?        00:00:00 [kt
$ ps -ef --cols 3 | head -3
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 03:02 ?        00:00:01 /sb
root         2     0  0 03:02 ?        00:00:00 [kt

Programming

Function related to error: perror, strerror. errno is include in errono.h.

API

How does int read(int fildes, void* buf, int nbyte) works in xv6?

  • user space:
    1. Interrupt with the use of int instruction.
  • kernel space:
    1. Trap handler. trap()
    2. Send the read request to the disk and sleep().
    3. After the disk fulfills the read request, IDE interrupts.
    4. IDE interrupt handler reads the data from the disk into the block buffer.
    5. Wake up the sleeping process.
    6. Copy the wanted data from the block buffer into buf
  • user space:
    1. syscall returns.

write syscall works in a similar way.

Web Server

Ngnix

Imagine that there are index.html and css.html in root. http://localhost/index.html can be used to access index.html. There are three a elements in index.html. Click any of them, the browser will go to css.html. ../css.html means to find css.html in a parent folder. Since index.html is at the root, .. is ignored here.

<a href="css.html"></a>
<a href="../css.html"></a>
<a href="/css.html"></a>

fdisk

CentOS

CentOS’s support term is 10 years.

Iptables

Change Hostname CentOS

Centos 6.5

  • Update /etc/hosts and /etc/sysconfig/network.
  • Run /etc/init.d/network restart.

The update to hostname by hostname command does not survive system root. But the Update to /etc/sysconfig/network survives system root.

Centos 7.x

For Centos 7.x, use hostnamectl set-hostname.

chkconfig

  1. Redhat 6 Runlevel
  2. Redhat 7 Using the chkconfig Utility
  3. LSB init scripts
  4. Runlevel Priority
  5. Starting Your Software Automatically on Boot
  6. 15.5. chkconfig

Priority rules:

  1. service with a lower priority starts ealier
  2. service with a higher priority stops ealier

4 and 5 clearly says Rule 1. Rule 2 follows from the implication that service with a higher priority depends on service with a lower priority. This implication is from Rule 1.

A chkconfig script example:

#!/bin/bash
# description: One Start Stop Restart
# processname: one  
# chkconfig: 234 20 80  

case $1 in
  start)
		echo "one start"
    ;;
  stop)   
		echo "one stop"
    ;;
  restart)
		echo "one restart"
    ;;
esac    
exit 0

Make

Shell assignment operator is available with GNU Make version 4.0. Reference is GNU Make 4.0 released.

Bash

Regular Expression Match Test

Metacharacters in regular expression should note be quoted.

> [[ "abc" =~ "a"".""c" ]] && echo "match"
> [[ "abc" =~ "a"."c" ]] && echo "match"
match

> [[ "abc" =~ 'a''.''c' ]] && echo "match"
> [[ "abc" =~ 'a'.'c' ]] && echo "match"

match
> [[ "abc" =~ '.*' ]] && echo "match"
> [[ "abc" =~ .* ]] && echo "match"
match

Excerpt from Bash reference manual:

Any part of the pattern may be quoted to force it to be matched as a string.

History Expansion

Enable it with set -H. history returns:

...
986  exit
987  ls
988  ./foo.sh 1
989  ./foo.sh 1 " "
990  ./foo.sh " " 1
991  vi foo.sh
992  ./foo.sh 1 " "
...

History expansion:

$ echo "!987"
echo "ls"
ls

Tips

Useful tricks:

set -e -x
shopt -s extglob

Check file format (unix or dos):

find -type f -print0 | xargs -0 grep -l `printf '\r\n'`

order of redirections:

Check options set by set in $-. Check options set by shopt by shopt -p.

Resources