Linux Tips ( grep the text without hashes and blank lines )

In our second post of linux tips i would like to share a very simple example in which i will use grep tool to remove all the hashes and blank lines from a file.

In our example we will use file1.txt with the following content

linux-11oz:/test # cat file.txt 
#The file with hashes
###
####
##

This is a test file

With lots of spaces


and few lines with


####hashes
###

So lets remove the hashes and blank lines with single grep command

We can use the following trick

linux-11oz:/test # grep . file.txt | grep -v "#"
This is a test file
With lots of spaces
and few lines with

The “.” will remove the blank lines and  “-v” will invert the match of #

Thats how simple it is. If you know a better way of doing it , Please send me an email or comment.

Email: learning@itpings.com

Thanks

Salman A. Francis

 

Linux Tips ( chmod tip )

Hey Guys this is going to be a new series of posts in which i will be sharing some cool linux tips.  You can watch the video of this post on http://www.youtube.com/linuxking

So here is the first tip Chmod:

I hope you guys are already familiar with chmod command if not checkout my Youtube “Linuxking” channel for theses commands.

With Chmod you can change the mode of the files and directories.
e.g
chmod 775 dir  command will change the permission on directory with rwxrwxrw-

Also using -R option with chmod will change the permission on the directory as well the content within the directory with same permissions.

However lets say you want to change the permissions of a directories to executable within a directory without changing permissions of file use the below tip.

Lets say you have a directory “test1” & “test2” inside “test” directory

test directory has also a  file inside it called file1.txt

file1.txt has permission rw——-

directory test1 has permission rwx——

directory test2 has permission rw——-

linux-11oz:/test # ls -ltr
total 0
-rw------- 1 root root 0 Mar 22 17:06 file.txt
drwx------ 1 root root 0 Mar 22 17:07 test2
drwx------ 1 root root 0 Mar 22 17:07 test1

Now if you try to change the permission with a simple ” chmod -R command ” of all the directories to “executable” inside test directory you could also change permission of the file inside test diretory.

Example:

linux-11oz:/ # chmod -R ug+x test/
linux-11oz:/ # cd test/
linux-11oz:/test # ls -ltr
total 0
-rwx--x--- 1 root root 0 Mar 22 17:06 file.txt  (You can see that the file also became executable)
drwx--x--- 1 root root 0 Mar 22 17:07 test2
drwx--x--- 1 root root 0 Mar 22 17:07 test1

Now to avoid such situation we will use X instead of x, lets see the impact of X with chmod command

linux-11oz:/ # chmod -R ug+X test/
linux-11oz:/ # cd test/
linux-11oz:/test # ls -ltr
total 0
-rw------- 1 root root 0 Mar 22 17:06 file.txt (You can see the file's permission has not changed to executable)
drwx--x--- 1 root root 0 Mar 22 17:07 test2
drwx--x--- 1 root root 0 Mar 22 17:07 test1
linux-11oz:/test #

I hope you have Enjoyed this tip, keep visiting our site for more information.

Recovery of the File System from Disaster

Hey Guys, long time no see…Well i am back and this time the issue is File System.

What Happened ?

Well My Backup Server’s root partition ran out of space due to some issue, and since it was on LVM (Logical Volume Manager) i took some space from my /home partition (it was 400 GB ) and added to my root partition. Following commands were used to resize the partition.

umount /dev/mapper/centos-home

lvreduce -L 200G /dev/mapper/centos-home

Note first i unmounted the partition and then reduced the space from my /home partition which was mounted as /centos-home
I took 200 GB out of it and added to my root partition.

The Mistake: I forgot to remount /home to /dev/mapper/centos-home

But i extended the partition with the following command:

lvextend -r -l+100%FREE /dev/mapper/centos-root

As per above command the partition will be re-sized to the 100% free space available from the reduced volume. Of course it was 200 GB. All was good when i saw the partition sized changed from no space to +200 GB. But then i realized that i was not able to mount /home to /centos-home and that was the time when all problem started. I “Rebooted The Machine” and when it came back it refused to enter the normal boot prompt but went into “Emergency mode”

Things i Tried:

I tried to fix the partition with xfs_repair tool , but in vain , and i received the error that “Cannot Find Super Block “

I tried to run the fixes couple of time but failed every time.

The Fix:

I fixed the issue by the following commands:

lvremove /dev/mapper/centos-home
lvcreate -L 200GB -n home centos

mkfs.xfs /dev/centos/home

mount /dev/mapper/centos-home

Luckily there was no important data on my /home partition and i was able to save my server.

Good Practice:

If you every fall prey to such situation, remember to “Backup” to an external drive and then proceed with the above mentioned technique to rescue yourself from such difficult situation.

I hope it will help some one some where, Let me know with your feedback.

Thanks & Best Regards,

Salman A. Francis

Linux Partitioning of more than 2GB Disk

So just wanted to share how to partition your hard drive from Linux command line if you have a drive greater than 2GB.

In my case its 3TB and i want to create a single partition out of it.

I used “gdisk” , its similar to “fdisk” but can create and understand large disk partitioning system.

 root@M-PVPS:/# gdisk /dev/sdb

GPT fdisk (gdisk) version 0.8.5

Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing ‘q’ if
you don’t want to convert your MBR partitions to GPT format!
***************************************************************
Command (? for help):

Now we will type “p” to print partition table.

Number Start (sector) End (sector) Size Code Name
1 2048 52889599 25.2 GiB 0700 #####
2 209713152 216107007 3.0 GiB 0700 #####

Now i will delete the available partition with “d”

Command (? for help): d
Partition number (1-2): 1

Now i will delete the 2nd partition with “d” and then typing “2”

Now i will print the partitioning table with “p”

Command (? for help): p
Disk /dev/sdb: 5860533168 sectors, 2.7 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 7F138376-657F-441A-B784-96E22BDAB49D
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 5860533134
Partitions will be aligned on 2048-sector boundaries
Total free space is 5860533101 sectors (2.7 TiB)

Now i will create the partition with “n” and selecting all the default values.

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-5860533134, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-5860533134, default = 5860533134) or {+-}size{KMGTP}:
Current type is ‘Linux filesystem’
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to ‘Linux filesystem’

Now i will print the partition table with “p”

Number  Start (sector)    End (sector)  Size       Code  Name
1            2048      5860533134   2.7 TiB     8300  Linux filesystem

Now we will write and save partition with “w” command.

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

Now we will create the file system with “mkfs -t ext3 /dev/sdb” command

root@M-PVPS:/# mkfs -t ext3 /dev/sdb1
mke2fs 1.42.5 (29-Jul-2012)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y

Now you can check the drive with df -h command

 

 

 

Linux Practical Security Course

Dear Members ,
I am happy to announce that Linux Practical Security by LZH Project is complete. We will launch it shortly. Please support us by buying and rating this course. For first 20 buyers there will be a discount of $5. Total cost is $25. You can also book your copy by sending me email to learning@itpings.com Please take a look at the introductory video here:

Openstack dashboard gives error “Error: Unable to retrieve information”

So today i ran into one more issue with Openstack , the error showed up  when i logged in to the dashboard and clicked on Project–>Instances.

What i saw terrified me because is saw “NOTHING” !

A day before i had the same issue and i thought it’s related to RAM so i restarted and everything started and operated normally, but this time the reboot didn’t help.

So i started scratching my head and started wondering what might be the reason. I searched internet but no success. I searched log files but nothing made sense at that time, so i rebooted one more time and an Error appeared saying something like “Openstack glance-registry api error limit reached ” So that gave me a head start that it has something to do with system being low in space.

A df -h showed that / is 100% full. I removed some unnecessary files , rebooted the system and Viola !

All started to work Normally.

Conclusion:

Make Sure you have enough space with OpenStack

 

Unable to Connect “The Squid Error “

So today i had a strange problem when i was only able to access google.com and not other sites. I was able to browse gmail, but not mail ru. Everything was working fine but out of no where this error “Unable to Connect” appeared on my Browsers. It was kinda strange, since i made no changes to my system” Also i was not sure if it’s the system or the Gateway.
Observation on System:
1) Both Firefox and Chrome giving the same problem.
2) I was able to browse with Chrome if Ultrasuf Extension was enabled.
3) Firebug reported “Aborted” in the Net tab

Steps taken:

Restarted the System
Changed the IP address
Result:
System Not Fixed Continue reading

Solving Call Drop Issue in Elastix / Asterisk

If you change your Network and you find that the working Elastix / Asterisk box is now dropping calls every 15 to 25 secs.  One of the most probable reason is “NAT” .

The solution which worked for me is changing the sip_custom.conf found in /etc/asterisk/ directory.

Open it with your favorite editor such as vim or nano and add the following lines.

externip=1.1.1.1/255.0.0.0  < Your External Ip with Subnet Here >
localnet=192.168.1.0/255.255.255.0 < Your Internal Ip with Subnet Here >
canreinvite=no
NAT=Yes
Qualify=yes
promiscredir=yes

Hope this will help someone.

Thanks,
Salman

Have a Question ? Need Consultancy ? Contact US

Hi Guys , you know that you can ask for free or get free consultancy form us. So, if you want to consult with us regarding Network Setups, Linux , Security and Web Development projects Feel free to ASK !.

Here is how you can Ask !

YouTube: www.youtube.com/linuxking
Facebook Page: www.facebook.com/linuxking77
Facebook Group: www.facebook.com/groups/LZHProject/
Website: www.itpings.com

Thanks,
Sal