Tuesday, November 15, 2011

Linux Commands

List open ports and listening services
Command in Linux to list ports
netstat -lnptu

Command in Windows to list ports
netstat -a | find "LISTENING"
Expression to run cronjob on every sunday 2nd and 4th week at 8 PM EST :
0 20 * * 0#2,4
minute (0-59),
hour (0-23),
day of the month (1-31),
month of the year (1-12),
day of the week (0-6 with 0=Sunday)


Ubuntu sudo unable to resolve host

Step 1 : cat /etc/hosts
Step 2 : Add below configuration : 127.0.0.1 localhost
127.0.1.1 glakshmanan-laptop

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Disable cd-rom 
eject -i off

logrotate (http://linuxcommand.org/man_pages/logrotate8.html) - rotates, compresses, and mails system logs 
1) cd /etc/logrotate.d/ 
2) copy the existing file and name the filename you want
3) Add the below in the file (Example) 
/var/log/activemq/*.log {
copytruncate
daily
rotate 7
compress
missingok
create 777 root adm
}
4) Logrotate cron daemon will run based on (/etc/cron.daily/logrotate) and your task will be accomplished.

Setup sendmail on your localbox (Ref : http://flurdy.com/docs/postfix/#config-simple-mta)
MTA - Mail Transport Agent.
1) install (apt-get install postfix) (yum install postfix for cent OS)
2) sudo vi /etc/mailname
Provide domain name or hostname in this file.
3) sudo vi /etc/postfix/main.cf (Paste below configuration). Replace myhostname, you can provide relayhost or leave blank
# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = glakshmanan-laptop
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = glakshmanan-laptop, localhost.operative.com, localhost
#relayhost = mail.operative.com
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

4) sudo /etc/init.d/postfix restart (Once the configuration is done we need to restart postfix)
5) tail -f /var/log/maillog


Install cisco vpn

Follow the link : http://www.painfullscratch.nl/code/vpn/

Note : Remove const in (netdevice.h)

If you get error "linux/autoconf.h: No such file or directory" follow below.

ln -s /usr/src/linux-headers-`uname -r`/include/generated/autoconf.h /usr/src/linux-headers-`uname -r`/include/linux/autoconf.h

Copy the pcf file to /etc/opt/cisco-vpnclient/Profiles

sudo /etc/init.d/vpnclient_init restart

vpnclient connect


Installing vpn got following error below :
--------------------------------------------
1) make: *** /lib/modules/2.6.42.9-1.fc15.x86_64/build: No such file or directory.  Stop.
   make: *** [default] Error 2

Fix
----
yum install kernel-devel

2) make[1]: gcc: Command not found

Fix
----
yum install gcc

Copying vpn pcf file
---------------------
cp NPNVPN.pcf /etc/opt/cisco-vpnclient/Profiles/

Starting vpn
-------------
/etc/init.d/vpnclient_init start

vpnclient connect NPNVPN


System Info

Linux OS Version
cat /etc/issue || cat /proc/version

Memory Information
cat /proc/meminfo | grep 'MemTotal' | awk '{print $2/1024/1024 " GB"}'

cpu information will have information about processor, core, etc...
cat /proc/cpuinfo

Swap Space
cat /proc/swaps

df -h (File System disk space usage)

sudo apt-get install sysinfo (System -> Administration -> Synaptic package Manager (To find broken packages or install packages)

Xmanager (Connect Linux GUI from windows) http://download.cnet.com/Xmanager/3000-7240_4-10038129.html

Install an RPM Package on Ubuntu Linux

Installing software on Ubuntu usually entails using Synaptic or by using an apt-get command from the terminal. Unfortunately, there are still a number of packages out there that are only distributed in RPM format.
There’s a utility called Alien that converts packages from one format to the other. This doesn’t always mean that an rpm will work on your system, though. You will need to install some prerequisite software packages in order to install alien, however. These packages include gcc and make.
Run this command to install alien and other necessary packages:
sudo apt-get install alien dpkg-dev debhelper build-essential
To convert a package from rpm to debian format, use this command syntax. The sudo may not be necessary, but we’ll include it just in case.
sudo alien packagename.rpm
To install the package, you’ll use the dpkg utility, which is the internal package management tool behind debian and Ubuntu.
sudo dpkg -i packagename.deb
The package should now be installed, providing it’s compatible with your system.


INSTALLING VMWARE PLAYER



Connect to remote host without giving passphrase from host (X). Follow steps and you should be good
Run command in your host (X) : ssh-keygen -t rsa
Copy the created .pub file into remote host (Y).
connect to remote host
cat .pub_file >> ~/.ssh/authorized_keys

Curl :

#!/bin/sh
TENANTS="alloysandbox cars carssandbox carstest cars_staging carssandbox carstest tmn tmn_staging"

host=$1
webapp=$2

for k in $TENANTS
do
echo curl -L -d"j_username=supportops@$k.com&j_password=Oper@t1ve&j_inp_password=Oper@t1ve" http://$host:8080/$webapp/api/migrateFinanceInvoices/

curl -L -d"j_username=supportops@$k.com&j_password=Oper@t1ve&j_inp_password=Oper@t1ve" http://$host:8080/$webapp/api/migrateFinanceInvoices/ -d "dummy=data"
done

ls -LR - List all the files from the recursive directory
find -focus - This command also list all the files in recursive directory

du -sh - Disk usage size for the folder (s - size, h - human readable format) Or du -ch | grep total

dmesg - If the program dies with out of memory or died you can search in dmesg. (dmesg | grep "Killed process")

 cat /var/log/kern.log | grep "Killed process" 

Linux Exclude find 
find ./ -name '*.*' | grep -Ev '.pl|.svn'

To check the bit in Ubuntu
file /sbin/init

create multiple directory in linux:

mkdir --parent /tmp/terracotta/client-logs

sudoers: (http://www.mjmwired.net/resources/mjm-fedora-f14.html)
sudoers - list of which users may execute what they want. Certain things if you want to execute you need to have a root permission or switch user to root.

When you try `sudo su` and if it fails than your name is not in sudoers list. In fedora you need to explicitly add the username in sudoers list. Login as root and execute below command to add user.
Execute : echo ' ALL=(ALL) ALL' >> /etc/sudoers
If the file doesn't have write permission execute `chown +w /etc/sudoers` 

How to add url into Fedora Repository ? Its like sources.list in Ubuntu
vi /etc/yum.repos.d/fedora.repo

How to Redirect the stderr to a file using tee ?
Syntax : ./shell_script_name.sh 2>&1 | tee -a error.log

mysql -uroot -ppetra123 test < upgrade_pre_phase.sql > error.out 2>&1


nohup - run a command immune to hangups, with output to a non-tty
Add nohup before you run the shell script or command. nohup will create nohup.out in the same script running directory and write the stdout / stderr into the file.
Ex : nohup ./shell_script_name.sh &
& - If you add amp this command will run in the back-ground

Find Folder Size greater than 200 MB
find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'


Finding Disk usage
Top 100 disk usage : du -ka / | sort -n | tail -100

Find Memory Usage:
ps -auxf | sort -nr -k 4 | head -10


fdisk - partition table manipulator for Linux
fdisk -l - This will list the partition available to mount

How to mount space into system from fdisk
Syntax :  mount disk folder
ex: mount /dev/sda2(disk) /hadoop(folder)

Add export classpath variable in /etc/profile 

http://www.idevelopment.info/data/Unix/Linux/LINUX_PartitioningandFormattingSecondHardDrive_ext3.shtml

Find files by MakeTime and Remove old or archive
Remove files - find *.txt -maxdepth 1 -mtime +1 -type f -exec rm {} \;
Zip files        - find *.txt -maxdepth 1 -mtime +3 -type f -exec gzip -9f {} \;

Rsync - Powerful command to copy files

Rsync Pull :
Syntax : Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

ex : rsync -avz --exclude *.csv root@targethostname:{$directory}${PWD}
a => Archive
v = > Verbose
z => compress
exclude => will exclude the mentioned files

Alternatives - Point your java to point to different directory from default openJdk

alternatives --install "/usr/bin/java" "java" "/usr/java/default/bin/java" 2
alternatives --config java

id_rsa file creation
http://www.laubenheimer.net/ssh-keys.shtml

Grep the text recursively against all the filesystem
grep -r "text" /

Make your Terminal display shorter (Your Terminal prompt show small)
export PS1='$'
$

Provide the Input command in next line in linux
$ hadoop jar example.jar \
$ > wordCount
(Put backslash and enter will prompt with > in next line to proceed)

Stop / Disable the firewall
Type the following two commands (you must login as the root user):
# /etc/init.d/iptables save
# /etc/init.d/iptables stop



Shell script to read a file line by line

#!/bin/bash
file="${DIRECTORY}/$fileName.csv"
while IFS= read -r line
do
    # display $line or do somthing with $line
     echo $line

done <"$file"

Date : 2 day ago :  /bin/date --date="2 day ago" +%Y-%m-%d
       1 day ago from the given date : /bin/date --date="2013-04-20 1 day ago" +%Y-%m-%d

No comments :

// Below script tag for SyntaxHighLighter