symetric gpg encryption

Symetric encryption is an encryption that uses same key to decrypt.
here is a way to encrypt file with symetric key without user interact ( batch )

gpg --batch -q --passphrase '<password>' --cipher-algo AES256 -c <file>

Create TAP/TUN device CentOS 7

Here is how to do it on CentOS 7 / RedHat 7 where the command  tunctl is missing
and when you try to run ifconfig you get an error

~# ifconfig tap0 10.0.0.1/30
SIOCSIFADDR: No such device
tap0: ERROR while getting interface flags: No such device
SIOCSIFNETMASK: No such device

to solve this you may run this commands
1. first we must create the device using ip command

# ip tuntap add tap0 mode tap

2. run ifconfig

~# ifconfig tap0 10.0.0.1/30
~# ifconfig tap0
tap0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 10.0.0.1 netmask 255.255.255.252 broadcast 10.0.0.3
ether 32:f5:3a:44:a5:64 txqueuelen 500 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Bash Special Variables

Bash shell has some built in variables that can be use in scripts
for example if you want to know the process id of the current command , you may use $$
here is a list of some special variables

$# Number of command-line arguments.
$_ The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.
$- A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).
$? Exit value of last executed command.
$ Process number of the shell.
$! Process number of last background command.
$0 First word; that is, the command name. This will have the full pathname if it was found via a PATH search.
$n Individual arguments on command line (positional parameters). The Bourne shell allows only nine parameters to be referenced directly (n = 1–9); Bash allows n to be greater than 9 if specified as ${n}.
$*, $@ All arguments on command line ($1 $2 …).
“$*” All arguments on command line as one string (“$1 $2…”). The values are separated by the first character in $IFS.
“$@” All arguments on command line, individually quoted (“$1” “$2” …).

simple install sun java

a quick and eazy way to install java under linux
1. download jre or jdk from www.java.com
2. create a directory to hold the versions

# mkdir /usr/java

3. extract the tar file under the directory

# tar -xzvf jre-7u45-linux-x64.tar.gz -C /usr/java
# ln -s /usr/java/jre1.7.0_45 /usr/java/jre-latest

4. the last part is to create links in the system in order to
make sure it uses this new java version we just extracted .
* global java command
debian linux based version:

# update-alternatives --install /usr/bin/java java /usr/java/jre-latest/bin/java 90
# update-alternatives --config java

RedHat linux based version :

# alternatives --install /usr/bin/java java /usr/java/jre-latest/bin/java 90
alternatives --config java

browsers links

ln -s /usr/java/jre-latest/lib/amd64/libnpjp2.so <path to install dir>/plugins/

bash history security

Bash history can reviel what a person commands where .
but what if the person wants to hide his tracks , he may use the

unset HISTFILE

will couse bash to delete the history upon logout .
there is how ever a harden attribute you may set to overcome this problem
when a user login , there are set of files/scripts the profile runs .
one place you may put such script would be under /etc/profile.d/ , with file extension sh
as it is being called by /etc/profile .
just place this small line in there :

readonly HISTFILE

now when a person wants to unset this parameter , bash will scream with this error

-su: unset: HISTFILE: cannot unset: readonly variable

Move ASM disk across storage online

This is a way you can use to copy/move your ASM disks to new location
without the need to stop your instance. it include command to be run
by root and by asm admin
1. creating the new disks(LUN) as ASM devices (run as root)

[[email protected] ~]# /etc/init.d/oracleasm listdisks
DATA1
OCR1
[[email protected] ~]# /etc/init.d/oracleasm createdisk DATA2 /dev/sdb8
Marking disk "DATA2" as an ASM disk:
[ OK ]
[[email protected] ~]#
[[email protected] ~]# /etc/init.d/oracleasm listdisks
DATA1
DATA2
OCR1

2. List disk in disk-group (run as asmadmin)

SQL> select GROUP_NUMBER,NAME,STATE,TOTAL_MB from v$asm_diskgroup ;
GROUP_NUMBER NAME
STATE TOTAL_MB
------------ ------------------------------ ----------- ----------
1 OCR
MOUNTED
964
2 DATA
MOUNTED
4777
SQL> select NAME,GROUP_NUMBER,REDUNDANCY from v$asm_disk ;
NAME
GROUP_NUMBER REDUNDA
------------------------------ ------------ -------
0 UNKNOWN
DATA1
2 UNKNOWN
OCR1
1 UNKNOWN

3. Adding disk to DATA disk-group (run as asdmadmin)

SQL> ALTER DISKGROUP DATA REBALANCE POWER 0 ;
Diskgroup altered.
SQL> ALTER DISKGROUP DATA ADD DISK 'ORCL:DATA2' NAME DATA2 ;
Diskgroup altered.
SQL> ALTER DISKGROUP DATA DROP DISK DATA1 ;
Diskgroup altered.
SQL> ALTER DISKGROUP data REBALANCE POWER 1;
Diskgroup altered.
SQL> select NAME,GROUP_NUMBER,REDUNDANCY from v$asm_disk ;
NAME
GROUP_NUMBER REDUNDA
------------------------------ ------------ -------
0 UNKNOWN
DATA1
1 UNKNOWN
DATA2
1 UNKNOWN
OCR1

Note that DATA1 was marked as DROP , it will stop showing once all data sync to DATA2
the process can be monitored by this query (run as asmadmin)

SELECT g.name "Diskgroup",
100*(max((d.total_mb-d.free_mb)/d.total_mb)-min((d.total_mb-d.free_mb)/d.total_mb))/max((d.total_mb-d.free_mb)/d.total_mb) "Imbalance",
100*(max(d.total_mb)-min(d.total_mb))/max(d.total_mb) "Variance",
100*(min(d.free_mb/d.total_mb)) "MinFree",
count(*) "DiskCnt",
g.type "Type"
FROM v$asm_disk d, v$asm_diskgroup g
WHERE d.group_number = g.group_number and
--d.group_number = 1 and
d.state = 'NORMAL' and
d.mount_status = 'CACHED'
12 GROUP BY g.name, g.type;
Diskgroup
Imbalance Variance MinFree
------------------------------ ---------- ---------- ---------- ----------
Type
------
OCR
EXTERN
DATA
EXTERN
•
DiskCnt
0 63.3817427 1
0 59.5143395 1

Manage partitions on a raw device file

Every once in a while there comes a time when you need to use a file as raw device
for example when using qemu qcow file or if you want to have your home directory portable and encrypted
in this example i am using one file named my-hd-image.file and 2 Linux partitions inside it . lets start
Creating the raw device file is simply by using dd ( can also be use via qemu-img )

dd if=/dev/zero of=my-hd-image.file bs=1M count=1024

Create partitions inside the new disk using fdisk , in my file
I had created 2 partitions of 512M

Device Boot Start End Blocks Id System
my-hd-image.file1 2048 1050623 524288 83 Linux
my-hd-image.file2 1050624 2097151 523264 83 Linux

creating the file system using loop devices to be linked to specific offset inside the file
so that each partition will have its own loop device . the offset calculation is in bits
and i am using bc to calculate it fast for me . the offset of each partition is its start block times 512
witch is the block size ( unless you have set a different one upon creation )
Partition my-hd-image.file1 starts at offset 2048

echo "2048 * 512" | bc
1048576

Partition my-hd-image.file2 starts at offset 1050624

echo "1050624 * 512" | bc
537919488

use the calculation offset to map to the loop device

losetup -o 1048576 /dev/loop1 my-hd-image.file
losetup -o 537919488 /dev/loop2 my-hd-image.file

Now we can format the partition

mkfs.ext4 /dev/loop1
mkfs.ext4 /dev/loop2

all is left to do is mount the partitions

root@# mkdir /mnt/my-hd-partition-1
root@# mkdir /mnt/my-hd-partition-2
root@# mount /dev/loop1 /mnt/my-hd-partition-1
root@# mount /dev/loop2 /mnt/my-hd-partition-2

To remove the device just use unmount and to release the loop device
you will need to use losetup

umount /mnt/my-hd-partition-2
umount /mnt/my-hd-partition-1
losetup -d /dev/loop1
losetup -d /dev/loop2

multi-line comment with vi

Vi editor is one of my favourite text editor to work with under linux/Unix systems ,
and from time to time there comes a need to mark multiple lines as comment .
here is how you can do it in vi/vim :
1. using Ctrl+v (visual block) mark the lines you wish to comment
2. go into insert mode Shift+i then write the comment (could be anything actually )
3. finished by clicking the Esc button .
and vwalla you lines are now marked as comment
to remove multi-line comment ,simply go through the same steps but
after marking the lines just press d for delete or c for clean

oracleasm cant find disks

When installing grid under Oracle Linux/RedHat , the grid installer may fail to see the asm disks
even if we just install the software , the asmca may also fail to see the disks .
in order to fix it , we just need to set the scanning path to where the disks are

/dev/oracleasm/disks/*

and now the installer can see the disks

samba PDC sambaLogonTime update

Samba doesnt update sambaLogonTime in LDAP when a user login
in order to have it set , one can run a script that will do it
set up a script /root/update_sambaLogonTime.sh

#!/bin/bash
TIMESTAMP=$(date +%s)
ldapmodify -x -h localhost -D "cn=Manager,dc=example,dc=com" -y /root/pass-test <<EOF
dn: uid=$1,ou=People,dc=example,dc=com
changetype: modify
replace: sambaLogonTime
sambaLogonTime: $TIMESTAMP
EOF

now create a pass-test file by running echo with “-n” in order to remove the newline “n”
because ldapmodify doesnt not chop it

echo -n "mypassword" > /root/pass-test 

now just add a line under [netlogon] in smb.conf

root preexec = /root/update_sambaLogonTime.sh "%u"