Thursday, February 16, 2012

LINUX Tips

Editing files remotely

Let us say you are on node-1 (IP=192.168.1.10) and you wish to edit a file on node-2 (IP=192.168.1.20), while you are logged in on node-1 without SSH'ing to node-2. Its pretty simple. All you need to do is open VIM, and let's say that you are editing a file (on node-1):

vim /root/install.log

Now, within the VIM session we can edit another file (assume that we need to edit /home/jack/hello.c directory from node-1):

:vsplit scp://root@192.168.1.20//home/jack/hello.c

The above command will do a "vertical split" within VIM and open the file "hello.c" that is existing on node-2. Yes, you may be prompted for password (unless you have password-less SSH setup across the two nodes).


Removing annoying "^M" characters in a file

There are quite a few ways of doing this, but a quick way to remove ^M scattered around your file is as follows in VIM:

:%s/C-vC-m//g

Where 'C-v' is 'Ctrl-v' and 'C-m' is 'Ctrl-m'.


Transfer a directory from LOCAL to REMOTE in a single command (using compression)

Let us say '/root/local-dir' exists on the local machine (from where you are executing the command) and '/root/remote-dir' exists on the remote node( with IP 19.2.168.1.100). To make a copy of '/root/local-dir' on '/root/remote-dir' on remote node:

tar -cvjf - *|ssh root@192.168.1.100 "(mkdir -p /root/remote-dir; tar -C /root/remote-dir -xvjf -)"

VIM Cheat Sheet

Click Here


CSCOPE/CTAGS

AS I deal with Java/C/C++ Code, I have a small script (put in /bin directory) which will automatically generate the CSCOPE DB. Assume that I'm in '/home/jack' directory and I have checked out my source tree in '/home/jack/project-1/src'. All the (*.cpp, *.java, *.c *.h etc) files are under 'src' driectory. So, I do a 'cd /home/jack/project-1/src' & then run the below script.
So here is the script ('/bin/do_cscope_ctags.sh'):

#!/bin/bash
echo "Generating CSCOPE.FILES for (*.java) / (*.cpp) / (*.c) / (*.h) / (*.js) files..."
find . -type f \( -iname "*.c" -o -iname "*.h" -o -iname "*.cpp" -o -iname "*.java" -o -iname "*.js" \) > cscope.files
echo "Runnning CTAGS..."
ctags -R
echo "Done !"
echo "Generating CSCOPE database..."
cscope -b -i cscope.files -f cscope.out
echo "---------------------------------------------------------------------"
echo " You can now run 'cscope' or 'cscope -C' (for case insensitive mode) "
echo "---------------------------------------------------------------------"



My ~/.vimrc file


imap ^? ^H
set t_Co=256
set cmdheight=2
syntax enable
syntax on
colorscheme inkpot
set autoindent
set smartindent
set ts=4
set shiftwidth=4
map :W :w
map :Q :q
set smartcase
set ignorecase
filetype plugin on
filetype indent on
set hlsearch
set noswapfile
set backupdir=~/.vim/backup
set laststatus=2
nnoremap :set invpaste paste?
set pastetoggle=
set showmode
set expandtab
set nu
nnoremap :TlistToggle
nnoremap :call g:Jsbeautify()
let Tlist_WinWidth = 50
filetype on
:map w
autocmd FileType java :setlocal sts=4 ts=4 shiftwidth=4
autocmd FileType c :setlocal sts=3 ts=3 shiftwidth=3
autocmd FileType cpp :setlocal sts=3 ts=3 shiftwidth=3
set tags=tags;/
set t_kb=^H
set bs=2
set cscopetag
let Tlist_Ctags_Cmd="/usr/bin/ctags"
fixdel
nnoremap . :tabn
nnoremap , :tabp
:map :e!



Passwordless SSH Setup across 2 Nodes


What is the end goal ?
Setup Passwordless SSH between node1 and node2

From node1, we should be able to execute a command on node2:
Example: ssh root@node2 “cp –r /root/files/*.jpg /tmp”

From node2, we should be able to execute a command on node1:
Example: ssh root@node1 “rm –rf /tmp/backup/*”

-------------------------------------
On Node1 = ibrvm-3-36-1 (hostname)
-------------------------------------

IP address = 10.3.36.1

Generate id_rsa.pub on both the nodes

[root@ibrvm-3-36-1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
4f:8f:b3:ca:52:fb:34:81:24:c8:be:a7:d7:d0:ef:ed root@ibrvm-3-36-1

Create authorized_keys file (if it does not exist) on both the nodes

[root@ibrvm-3-36-1 ~]# touch ~/.ssh/authorized_keys

Setup necessary permissions

[root@ibrvm-3-36-1 ~]# chmod 644 ~/.ssh/authorized_keys
[root@ibrvm-3-36-1 ~]# chmod 700 ~/.ssh

Copy node1’s ~/.ssh/id_rsa.pub to /tmp on node2
[root@ibrvm-3-36-1 ~]# scp ~/.ssh/id_rsa.pub root@10.3.36.2:/tmp

Append the contents of node2’s id_rsa.pub (which we’ve now copied to /tmp) to node1’s authorized_keys file
[root@ibrvm-3-36-1 ~]# cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys

-------------------------------------
On Node2 = ibrvm-3-36-2 (hostname)
-------------------------------------

IP address = 10.3.36.2

Generate id_rsa.pub on both the nodes
[root@ibrvm-3-36-2 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
4f:72:13:b3:47:01:6d:40:a3:c4:3f:18:cf:72:28:6c root@ibrvm-3-36-2
Create authorized_keys file (if it does not exist) on both the nodes

[root@ibrvm-3-36-2 ~]# touch ~/.ssh/authorized_keys

Setup necessary permissions
[root@ibrvm-3-36-2 ~]# chmod 644 ~/.ssh/authorized_keys
[root@ibrvm-3-36-2 ~]# chmod 700 ~/.ssh

Copy node2’s ~/.ssh/id_rsa.pub to /tmp on node1
[root@ibrvm-3-36-2 ~]# scp ~/.ssh/id_rsa.pub root@10.3.36.1:/tmp

Append the contents of node1’s id_rsa.pub (which we’ve now copied to /tmp) to node2’s authorized_keys file
[root@ibrvm-3-36-2 ~]# cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys



Ethernet Link Speed

Q. How do I change the speed, duplex on for my Ethernet card?

A. Under Linux use mii-tool or ethtool package which allows a Linux sys admin to modify/change and view the negotiated speed of network interface card (NIC) i.e. it is useful for forcing specific Ethernet speed and duplex settings.
Depending on which type of Ethernet card is installed on the system you need to use either mii-tool or ethtool. I recommend installing both and use one of the tool, which will work with your card.

Task: Install mii-tool and ethtool tools

If you are using Debian Linux you can install both of these packages with following command:

# apt-get install ethtool net-tools


If you are using Red Hat Enterprise Linux you can install both of these packages with following command:


# up2date ethtool net-tools


If you are using Fedora Core Linux you can install both of these packages with following command:

# yum install ethtool net-tools

Task: Get speed and other information for eth0

Type following command as root user:

# ethtool eth0


Output:

Settings for eth0:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: MII
PHYAD: 32
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: pumbg
Wake-on: d
Current message level: 0x00000007 (7)
Link detected: yes


Or use mii-tool command as follows:

# mii-tool eth0

Output:

eth0: negotiated 100baseTx-FD flow-control, link ok


Task: Change the speed and duplex settings

Setup eth0 negotiated speed with mii-tool
Disable autonegotiation, and force the MII to either 100baseTx-FD, 100baseTx-HD, 10baseT-FD, or 10baseT-HD:

# mii-tool -F 100baseTx-HD
# mii-tool -F 10baseT-HD


Setup eth0 negotiated speed with ethtool

# ethtool -s eth0 speed 100 duplex full
# ethtool -s eth0 speed 10 duplex half


To make these settings permanent you need to create a shell script and call from /etc/rc.local (Red Hat) or if you are using Debian create a script into the directory /etc/init.d/ directory and run update-rc.d command to update the script.
Read man page of mii-tool and ethtool for more information.


Some useful Linux Commands


What's using memory: You may find that your computer is running a bit slower. You can easily find out what's using up your memory:
# ps -aux | awk '{print $5,$6,$11}' | sort –n –k 1


Find files bigger than 1MB: This will find files in your home directory that are bigger than 1 megabyte
# find /tmp -size +1000k -print


To list all files on the system whose file name is `top', regardless of case, type:
# find / -iname top


To list all files on the system whose names begin with the characters `top', type:
# find / -name 'top*'


To list all files in the `/usr/local' directory tree that are greater than 10,000 kilobytes in size, type:
# find /usr/local -size +10000k


To list all files in your home directory tree less than 300 bytes in size, type:
# find ~ -size -300b


To find all empty files in your home directory tree, type:
# find ~ -empty


To list the files in the `/usr/local' directory tree that were modified within the past 24 hours, type:
# find /usr/local -mtime -1


To list the files in the `/usr' directory tree that were modified within the past five minutes, type:
# find /usr -mmin -5


To list all of the files in your home directory tree that were modified yesterday, type:
# find ~ -mtime 1 -daystart


Use the `-r' option with sort to reverse the listing and output the largest directories first.

To output a list of the subdirectories in the current directory tree, sorted in descending order by size, type:
# du -S . | sort -nr


To output a list of the subdirectories in the `/usr/local' directory tree, sorted in descending order by size, type:
# du -S /usr/local | sort -nr


To list the number of files in the `/usr/share' directory tree, type:
# find /usr/share \! -type d | wc –l


To list the number of files and directories in the `/usr/share' directory tree, type:
# find /usr/share | wc -l


To list the number of directories in the `/usr/share' directory tree, type:
# find /usr/share \! -type f | wc -l


Extended grep search *.dat files for "this" or "that" case insensitive (-i) and where found print line number (-n) along with the line contents.
# egrep -in "this|that" *.dat




Miscellaneous Commands


Resize all images in a folder and all of its subfolders

# find . -iname "*.jpg" | xargs -l -i convert -resize 1280x960 {} {}

Find out who is using how much of space in /home

# find /home -maxdepth 1 -type d -print|xargs -l -i du -B 1048576 -s {}|sort -n -k 1

Mounting Windows Shared Folder on LINUX

Assuming that you have shared "C:\MySharedFolder" as "MySharedFolder". This folder is owned by user "Jack" on Windows who has full permissions on the folder.

mount -t cifs //10.6.100.18/MySharedFolder /mnt/WindowsMount -o username=Jack,password=mypassword123,domain=localhost



Multi-threaded (Parallel) Downloads

LFTP - supports FTP, HTTP, and SFTP. Supports using multiple connections
to download a single file. Assuming you want to transfer a file from
remoteServer to localServer, install LFTP on localServer, and run:

lftp -e 'pget -n 4 sftp://userName@remoteServer.com/some/dir/file.ext'

The '-n 4' is how many connections to use in parallel.


SVN clean up

Strip ".svn" folders from a Subversion working copy

find . -name ".svn" -exec rm -rf {} \;



Force Un-mount

HowTo: Force umount a busy device in Linux
When you try to umount the regular way, if a device is busy you’ll get the following :

# umount /dev/sda1
umount2: Device or resource busy
umount: /boot: device is busy

This can be solver very easy by entering this:

# umount -l /dev/sda1



Create a new _Bootable_ DVD ISO file

Assume that you have loop-mounted your ISO and copied the contents to '/path/where-dvd-is/extracted'.

mkisofs -J -l -f -r -v -T -V "HPLinux" \
-o \
-b isolinux/isolinux.bin \
-c isolinux/boot.cat \
-no-emul-boot \
-boot-load-size 8 \
-boot-info-table /path/where-dvd-is/extracted

No comments :

Post a Comment