Compare two files and list common lines

You can use the awk command to compare two files and show the common ones. Compared to the Linux command “comm”, advantages of using “awk” is it will give output though the files are not sorted.
Syntax :

awk 'NR==FNR{a[$1]++;next} a[$1] ' file1 file2

Eg :

[root@test]# cat A
apple
orange
mango
grape

[root@test]# cat B
mango
banana
grape

[root@test]# awk 'NR==FNR{a[$1]++;next} a[$1] ' A B
mango
grape

Connect to serial console from Linux command line

The easiest way to connect to a serial console from Linux command line is using the screen command. In my example below, I will show you how to connect to a serial console of a server using the serial to usb cable.

[root@test ~]# screen /dev/ttyUSB0
******************************************
 Connected to *******
******************************************

Enjoy 🙂

Delete files starting with hyphen (-)

Today I have gone through something interesting, Just to remove a file in Linux; but the file starts with a hyphen!

[root@test]# rm '-g.txt'
rm: invalid option -- 'g'
Try rm ./-g.txt' to remove the file `-g.txt'.
Try `rm --help' for more information.

Yes error message said it, just use the format

rm -rf ./filename_with_hiphen

Enjoy 🙂

Device eth0 does not seem to be present, delaying initialization.

You might have come across this headline, especially if you working with Oracle Virtual box machines, machines those are cloned from previous setups etc.

device

To resolve the issue, just remove the file /etc/udev/rules.d/70-persistent-net.rules and reboot the machine. This file contains the MAC address details associated with the machine. If you don’t want to reboot the machine, run the below command :

udevadm trigger --subsystem-match=net

This command will trigger the rules again.

Enjoy 🙂

Install Oracle JDK

Go to the link : http://www.oracle.com/technetwork/java/javase/downloads/

Accept the license agreement and Download the tar.gz file (eg: jdk -8u65-linux-i586.tar.gz)

Extract the file using the command tar -xzvf jdk-8u65-linux-i586.tar.gz

Copy the extracted directory (In my case jdk1.8.0_65) to the required location say /scratch

alternatives –install /usr/bin/java java /scratch/jdk1.8.0_65/bin/java 20000

alternatives –config java

You can see the list of available java versions and select our installed one by typing the number corresponding to that one.

Hack the permissions in Linux

Many of you reached here because of the term ‘Hack’. Sorry to say, I have to disappoint you(or may not be!). Here I am just discussing about how the Linux permissions work and not anything about hacking the system in a way script kiddies think. I am just mentioning few things about how the permissions are actually working and how it can lead to unwanted results if you are not sure what you are doing. I have put some commands below and for a Linux guy, it is enough and you will get an idea what I am talking about :

root@ubuntu:~# mkdir /root/test
root@ubuntu:~# ls -ld /root/test/
drwxr-xr-x 2 root root 4096 Mar 1 15:27 /root/test/
root@ubuntu:~# useradd tom
root@ubuntu:~# cat /etc/passwd|grep tom
tom:x:1001:1001::/home/tom:
root@ubuntu:~# chown -R tom:tom /root/test/
root@ubuntu:~# ls -ld /root/test/
drwxr-xr-x 2 tom tom 4096 Mar 1 15:27 /root/test/
root@ubuntu:~# userdel tom
root@ubuntu:~# ls -ld /root/test/
drwxr-xr-x 2 1001 1001 4096 Mar 1 15:27 /root/test/
root@ubuntu:~# useradd jerry
root@ubuntu:~# cat /etc/passwd|grep jerry
jerry:x:1001:1001::/home/jerry:
root@ubuntu:~# ls -ld /root/test/
drwxr-xr-x 2 jerry jerry 4096 Mar 1 15:27 /root/test/

See how the user jerry got access to tom’s files. It was caused by the same uid both were having. So if you are dealing with a large number of users, never simply delete the user. Just disable the user or change all the permission of the user to something more suitable before removing the user.

Enjoy 🙂