Table of Contents

Shell Command

refer: linux_shell_scripting_with_bash_2004.pdf
Advanced Bash-Scripting Guide:http://tldp.org/LDP/abs/html/
Bash script: http://www.linuxconfig.org/Bash_scripting_Tutorial

Basic Commands

man

man -a read
=> output:
POSIX Programmers Manual                   READ(P)
Linux Programmers Manual                   READ(2)

man -a stat
=> output:
User Commands                                STAT(1)
Linux Programmer?s Manual                   STAT(2)

uname

uname -a

⇒Linux sgwp 2.6.17 #3 PREEMPT Fri Apr 4 00:18:16 JST 2008 armv5tel unknown unknown GNU/Linux

date

file and directory commands

dirname

get directory name from file

ls

find files in directory

find . -name filename

ln

ln [OPTION]... TARGET [LINK_NAME]

Create a link to the specified TARGET with optional LINK_NAME. If LINK_NAME is omitted, a link with the same basename as the TAR-GET is created in the current directory

ssh $i "rm -rf AM;mkdir AM;ln -s /home/ztgame/$1/release/ /home/ztgame/AM"

( -s, –symbolic make symbolic links instead of hard links)

du

du -sh /db/mysql/ 
df -ah
df -h

lock/unlock file and directory

cd /home/admin/conf
lsattr -a
-------------e- ./web
----i--------e- ./.
-------------e- ./..
-------------e- ./dns
-------------e- ./mail

⇒ directory /home/admin has attribute i ⇒ can't remove or create new files in this directory with root And remove this attribute

chattr -i /home/admin/conf/

Shell Security

chmod

chmod a+w

⇒permission write for all user

Job and process, threads commands

whereis

whereis java

⇒ specific path of process java

whereis iptables

⇒ iptables: /sbin/iptables /lib/iptables /usr/share/man/man8/iptables.8.gz

kill

kill `pidof zabbix_agentd`
killall zabbix_agentd

netstat

netstat -tlnp (-t:tcp, -l:listening, -n: numberic, -p: programs) config: /etc/services ⇒ presently the policy of IANA to assign a single well-known

The Registered Ports are those from 1024 through 49151
The Dynamic and/or Private Ports are those from 49152 through 65535

ps

Check memory information

Memory space is divided into memory used by processes, disk cache, free memory and memory used by kernel Some basic parameters in memory information:

in /proc/meminfo

cat /proc/meminfo

output:

MemTotal:        1020400 kB
MemFree:           65064 kB
Buffers:          180148 kB
Cached:           166648 kB
SwapCached:           76 kB
Active:           370004 kB
Inactive:         423848 kB
Active(anon):     183084 kB
Inactive(anon):   264156 kB
Active(file):     186920 kB
Inactive(file):   159692 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       1048568 kB
SwapFree:        1048440 kB
Dirty:               408 kB
Writeback:             0 kB
AnonPages:        447048 kB
Mapped:            19032 kB
Shmem:               184 kB
Slab:             140256 kB
SReclaimable:     118196 kB
SUnreclaim:        22060 kB
KernelStack:         992 kB
PageTables:         5092 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     1558768 kB
Committed_AS:     604456 kB
VmallocTotal:   34359738367 kB
VmallocUsed:        7320 kB
VmallocChunk:   34359727504 kB
HardwareCorrupted:     0 kB
AnonHugePages:    143360 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:        6136 kB
DirectMap2M:     1042432 kB
DirectMap1G:           0 kB

We have the expression:

Active(370004) = Active(anon: 183084) + Active(file:186920)
Inactive(423848) = Inactive(anon:264156) + Inactive(file:159692)
Memtotal = MemFree + Buffers + Cached + Active + Mapped + Shmem + Slab + PageTables + VmallocUsed

In top command

top

output:

Cpu(s):  4.0%us,  0.5%sy,  0.0%ni, 94.4%id,  1.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1020400k total,   955336k used,    65064k free,   180148k buffers
Swap:  1048568k total,      128k used,  1048440k free,   166648k cached

The expression:

1020400k total = 955336k used + 65064k free
955336k used = 180148k buffers + 166648k cached + Other Mem(Kernel and App Server Use)

Check memory of process

cat /proc/1704/status

output:

Name:   php-fpm
State:  S (sleeping)
Tgid:   1704
Pid:    1704
PPid:   1703
TracerPid:      0
Uid:    99      99      99      99
Gid:    99      99      99      99
Utrace: 0
FDSize: 64
Groups: 99
VmPeak:   329824 kB
VmSize:   329504 kB
VmLck:         0 kB
VmHWM:    127368 kB
VmRSS:    124412 kB
VmData:   118996 kB
VmStk:        88 kB
VmExe:      9920 kB
VmLib:     12832 kB
VmPTE:       476 kB
VmSwap:        0 kB
Threads:        1
SigQ:   0/7826

Explain the parameters:

pmap

report memory map of a process

Get all threads of process

List Open Files for Process

get open files base on OS information

lsof

Get total open files in linux

run below command:

cat /proc/sys/fs/file-nr

or run with lsof:

lsof | wc -l

There are two reasons lsof | wc -l doesn't count file descriptors. One is that it lists things that aren't open files, such as loaded dynamically linked libraries and current working directories; you need to filter them out. Another is that lsof takes some time to run, so can miss files that are opened or closed while it's running; therefore the number of listed open files is approximate

Text Processing

cat

cat > abc.txt
content
#ctrl+D

wc

# wc -l

cut

paste

Example:

# cat names
Tony
Emanuel
# cat numbers
(307) 555-5356
(212) 555-3456
# paste names numbers
Tony    (307) 555-5356
Emanuel (212) 555-3456

grep

grep -v "#" 

⇒ get content none sign “#”

sed

Basic syntax:

sed -i 's/original/new/g' file.txt

Explain options:

The command string:

Below are some basic examples for searching and replaccing string

cat intro

output:

The Unix operating system. Unix system

awk

# cat server
1-Ngoa Ho 10.30.31.10
3-Tang Long 10.30.31.38

Custom Commands

checksum

md5sum, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum

curl

basic options

-A/--user-agent <string> User-Agent to send to server (H)
-b/--cookie <name=string/file> Cookie string or file to read cookies from (H)
-c/--cookie-jar <file> Write cookies to this file after operation (H)
  --create-dirs   Create necessary local directory hierarchy
  --crlf          Convert LF to CRLF in upload
  --crlfile <file> Get a CRL list in PEM format from the given file
-d/--data <data>   HTTP POST data (H)
  --data-ascii <data>  HTTP POST ASCII data (H)
  --data-binary <data> HTTP POST binary data (H)
  --data-urlencode <name=data/name@filename> HTTP POST data url encoded (H)
  --delegation STRING GSS-API delegation permission
  --digest        Use HTTP Digest Authentication (H)
  --disable-eprt  Inhibit using EPRT or LPRT (F)
  --disable-epsv  Inhibit using EPSV (F)
-F/--form <name=content> Specify HTTP multipart POST data (H)
    --form-string <name=string> Specify HTTP multipart POST data (H)
    --ftp-account <data> Account data to send when requested by server (F)
    --ftp-alternative-to-user <cmd> String to replace "USER [name]" (F)
    --ftp-create-dirs Create the remote dirs if not present (F)
    --ftp-method [multicwd/nocwd/singlecwd] Control CWD usage (F)
    --ftp-pasv      Use PASV/EPSV instead of PORT (F)
-I/--head          Show document info only
-k/--insecure      Allow connections to SSL sites without certs (H)
    --interface <interface> Specify network interface/address to use

Understand about options:

-d/--data <data>   HTTP POST data (H)
  --data-ascii <data>  HTTP POST ASCII data (H)
  --data-binary <data> HTTP POST binary data (H)
  --data-urlencode <name=data/name@filename> HTTP POST data url encoded (H)
  --delegation STRING GSS-API delegation permission
  --digest        Use HTTP Digest Authentication (H)
  --disable-eprt  Inhibit using EPRT or LPRT (F)
  --disable-epsv  Inhibit using EPSV (F)

examples for curl

tcpdump

change timezone

rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Bangkok /etc/localtime

iptables

iptables -F

⇒ Stopping firewall and allowing everyone

iptables-save

mount

mount iso
mount to RAM Disk

ldd: print shared library dependencies of binary file

ldd /usr/sbin/vsftpd
linux-gate.so.1 =>  (0x00b1f000)
libssl.so.6 => /lib/libssl.so.6 (0x008e0000)
libwrap.so.0 => /lib/libwrap.so.0 (0x00944000)
libnsl.so.1 => /lib/libnsl.so.1 (0x00991000)
libpam.so.0 => /lib/libpam.so.0 (0x00110000)
libcap.so.1 => /lib/libcap.so.1 (0x00fc0000)
libdl.so.2 => /lib/libdl.so.2 (0x00d9b000)
libc.so.6 => /lib/libc.so.6 (0x003af000)

objdump: Check target binary

objdump -f mole.vn.register

rsync

( rsync is a program that behaves in much the same way that rcp does, but has many more options and uses the rsync remote-update protocol to greatly speed up file transfers when the destination file is being updated. The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network connection, using an efficient checksum-search algorithm described in the technical report that accompanies this package.)

rsync on windows

  1. Step1: Download and install rsync at here. cwRsync will installed to c:\Program Files (x86)\cwRsync\bin\
  2. Step2: Add path “c:\Program Files (x86)\cwRsync\bin\” to PATH envionment variable on Windows
  3. Step3: Check ssh with private-key:
    ssh -p1362  -i E:\backup\GoogleDrive\keys\id_rsa root@128.199.236.122
  4. Step4: Run rsync via ssh to sync data:
    rsync -avz --exclude=".svn"  -e "ssh -p1362 -i E:\backup\GoogleDrive\keys\id_rsa" "/cygdrive/d/web/www.babies.vn/" root@128.199.236.122:/data/www/www.babies.vn/
  5. Step5: Chown to apache.apache for /data/www/www.babies.vn/
    ssh -p1362  -i E:\backup\GoogleDrive\keys\id_rsa root@128.199.236.122 "chown -R apache.apache /data/www/www.babies.vn/"
  6. Step6: Fix error rsync
    rsync: recv_generator: mkdir failed: Permission denied (13)

    Add option for rsync on windows:

    --no-p --no-g --chmod=ugo=rwX

osgi

b
=>list bundles

start ID
=> start bundles

sh
=>shutdown osgi

nm: list symbols from object files

nm -C -D libhelloworldjni.so ⇒

0000054f T JNI_OnLoad
00000568 T JNI_OnUnload
0000050c T Java_HelloWorldJNI_sayGoodbye
00000520 T Java_HelloWorldJNI_sayHelloWorld__
00000534 T Java_HelloWorldJNI_sayHelloWorld(int0_t)

Cygwin

Install Cygwin

  1. Step1: download cygwin setup from https://cygwin.com/install.html
  2. Step2: Install Cygwin with default mode to setup basic packages, and we will install new packages If application must use them

Using Cygwin