Linux useful commands and how-to collections

Linux useful commands and how-to collections.

Config for mount with nfs

1) set on source:
$ vi /etc/exports
add below entry:
=> /path/to/share *(ro, root_squash)
$ service nfs start
2) set on destination:
$ mount source_ip:/path/to/share /destination/dir

Run google chrome as root

$ vi /usr/bin google-chrome
=> add "--user-data-dir" at the very end of the file
=> e.g. exec -a "$0" "$HERE/chrome" "$@" --user-data-dir

Install google-chrome-stable on ubuntu

$ wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
$ sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
$ sudo apt-get update sudo apt-get install google-chrome-stable

Zip all files under directory and unzip to directory

$ zip -r file.zip directory
$ unzip -d destination file.zip

Test HTTP using telnet

$ telnet www.google.com 80
=> This will connect to www.google.com on port 80
GET /
GET /index.html HTTP/1.1
GET /index.html HTTP/1.1 host:www.google.com
=> Hit the <enter> twice
=> This will output the info of http response

Install lxml on ubuntu

$ sudo apt-get install libxml2-dev libxslt1-dev python-dev
=> For older version:
$ sudo apt-get install python-lxml

Config default java on rhel

$ /usr/sbin/alternatives --config java

Force a clock update using ntp

$ sudo ntpdate -s time.nist.gov

Find ip addresses

$ ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}' | sed 's/addr://'

Add private key for ssh connection to automatically load and use

$ ssh-agent `ssh-add ~/.ssh/custom-key`

DNS query

$ nslookup <target>
$ host <target>
$ dig <target>

Count number of files

$ ls -l <dir> | wc -l

Replace string in files using sed

$ sed -i -e 's/old/new/g' /path/to/file

Get full path of a file

$ readlink -f file.txt

Extract substring

sub=${parent:start:length}
=> a="helloworld"
=> b=${a:1:3}
=> echo $b
=> ell

Open file on command line using default editor

=> if desktop is gnome:
$ gnome-open file
=> if desktop is kde:
$ kde-open file
or
$ xdg-open file

Run command until success

until command-succeeds do
    another-command
done

Get yesterday date

$ date -d "yesterday 13:00 " '+%Y-%m-%d'

Shortcuts of input prompt

CTRL-U: Delete everything before cursor
CTRL-K: Delete everything after cursor
CTRL-Y: Paste the deleted input
CTRL-E: Jump to the end of input

Start jenkins in standalone mode

$ java -DHUDSON_HOME=dir -jar jenkins.war --httpPort=80

One-line alert with python

$ python -c "import Tkinter, tkMessageBox; Tkinter.Tk().wm_withdraw(); tkMessageBox.showinfo('Tips', 'Hi there it\'s time to have a little rest')"

Show linux kernel info and distro

$ cat /proc/sys/kernel/osrelease
$ lsb_release -a
$ uname -a

Execute two commands

$ cmd1 && cmd2
=> cmd2 will execute only if cmd1 succeeds (return exit code 0)
$ cmd1 ;  cmd2
=> cmd2 will execute regardless cmd1 success or failure

Run program in background

$ nohup java -jar app.jar &

Find application paths

$ find / -name <appname>

Change computer name

$ vi /etc/hostname
$ vi /etc/hosts

Set new date

$ date --set="STRING"
$ date -s "STRING"
e.g.
$ date --set="22 Nov 2014 09:00:00"

Set prompt character

$ vi ~/.bashrc
=> PS1="[\u@\h \W]\\$ "
=> PS1="[\u@\h \W]# "

List installed packages on rhel and centos

=> rpm -qa
=> yum list installed

Config static ip address for ubuntu

1) Add hostname to /etc/hostname
2) Set dns in /etc/resolv.config
=> nameserver xx.xx.xx.xx
3) Set static ip address in /etc/network/interfaces
auto eth0
iface eth0 inet static
address xxx.xxx.xxx.xxx
netmask 255.255.255.0
gateway xxx.xxx.xxx.xxx

Check http resource but not download

$ wget --spider url
$ curl -I url
$ curl --head url

Check disk info

=> List free disk space
$ df -h or df -k
=> List space usage of file or directory
$ du -sh

Route table

$ route -n
=> View routing table
$ route add -net xx.xx.xx.xx netmask 255.255.255.0 gw xx.xx.xx.xx
=> Add route
$ route del -net xx.xx.xx.xx netmask 255.255.255.0 gw xx.xx.xx.xx
=> Delete route

Write to syslog

$ logger hello

Display system uptime

$ uptime

Display users and activity

$ w
$ users
$ who
$ whoami
$ last

List open files

$ lsof
$ lsof -i:22
$ lsof -u ubuntu

List most appearing of english words in text

cat xxx-file | tr -s ' ' '\n' | sort | uniq -c | sort -rn | head -n 10

List active SSH connections

$ ss | grep -i ssh

HTTP benchmark with wrk

$ wrk -t12 -c400 -d30s http://localhost:3000

Run background jobs with tmux

# create new session
$ tmux
# create new session with name
$ tmux new -s name

# ls sessions
$ tmux ls

# attach last session
$ tmux a
# attach specified session
$ tmux attach -t name

# detach session
$ ctrl+b, release, d

# kill session
$ tmux kill-session -t name

Display operating system information with neofetch

# source: https://github.com/dylanaraps/neofetch, download and install first
$ neofetch

linux