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" "[email protected]" --user-data-dir

Install google-chrome-stable on ubuntu

$ wget -q -O - | sudo apt-key add -
$ sudo sh -c 'echo "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 directory
$ unzip -d destination

Test HTTP using telnet

$ telnet 80
=> This will connect to on port 80
GET /index.html HTTP/1.1
GET /index.html HTTP/1.1
=> 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

Find ip addresses

$ ifconfig | grep inet | grep -v inet6 | grep -v | 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

=> 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
$ xdg-open file

Run command until success

until command-succeeds do

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"
$ 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

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 gw xx.xx.xx.xx
=> Add route
$ route del -net xx.xx.xx.xx netmask 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
Post tagged with: linux