• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Did you find what you needed?
    Show your support and gratitude by making a contribution!!
    Life always offers you a second chance ... Its called tomorrow !!!

    Tuesday, October 17, 2017

    Casino Gambling On Mobile vs Desktop Computers (Laptops)

    The internet, phones, and gaming

    I remember distinctly having my first web enabled phone. With a screen that was probably no more than an inch or so big, it was still useful enough to bring up things (very slowly) to have a read of (very slowly) on my tiny little phone. Just having the power of the internet in your pocket was enough to make you feel like one of the coolest people in the world.
    Later on down the line, I also had the Nokia N-Gage – Nokia’s phone designed specifically for gaming, a truly experimental phone that was awesome to own. Because not only almost no one else had one, but also because I could play Call of Duty, Elder Scrolls, and The Sims on my phone, whenever I wanted. Talk, text, and game – what more could you want?! The next generation of N-Gage gaming even offered World Series of Poker: Pro Challenge, and let me tell you I have done a lot of computer poker playing in my life but it was awesome fun to be able to have it with you while on the go.

    Nokie N-Gage This was a long shot and defined how wrong Nokia was getting things.

    Well, gaming on phones really took off when you combined these two types of technology – the internet and actual games (rather than just snake or whatever it was you had on your old phone). Firstly, as you were now able to download games directly to your phone. And latterly, as internet experience could be combined within your game itself.
    While web enabled phone games that are popular for the average Jo Blow are things like Angry Birds and Candy Crush, for that little kick, others prefer immersive multiplayer games where you can play with friends (or strangers) like Minecraft or Modern Combat. There’s such a huge array of different game types out there, that there really is something for everyone.

    Casino games: from casinos, to your desktop, to your phone

    If you’re a fan of casino games, then you might be interested in learning about the experience on mobile. Many moons ago I was a programmer for a slot machine company that produced games that you’ll see in real casinos and gambling houses all over the world. It was always a bit of a thrill finding a game I’d produced somewhere in a casino far across the world.
    For some people, it’d be a dream to play the pokies all day long and get to trigger jackpots whenever you pleased, however with the lack of motivation to win, I can tell you it got a little dull. But that doesn’t mean that I won’t have a flutter when I get to a real casino now. And it doesn’t mean that I haven’t given it a go on an online casino or two as well. It was interesting for me to see that there was pretty much zero difference between the gaming experience with an actual casino machine and the slot games that you can find on all the online casino sites.
    I was surprised to find out that the first online slot game was released in 1998, from the world’s first online casino, Microgaming (established in 1994). Now, you can be assured that online slots have come a long way since those days! These days, online casino slot games can go head to head easily with a real machine. We no longer need a large machine to do the sort of operations that will power a slot style game – the only thing missing is the lights, decals, and proper buttons. Most online casinos will carry a range of different games, like slots, poker, roulette, and more.
    Of course now we are spending more and more time on our mobile instead of our desktops – particularly when it comes to some fun little time wasters. These online casino houses have been paying attention, too. When your target audience moves to another platform, then you better make the move as well if you want to stay relevant.

    Mobile experience vs desktop experience

    You can play online casino games either through casino providers’ websites or through separate apps, which many companies have chosen to develop to help with providing a smooth, immersive mobile experience. Some companies may even only produce apps, with no desktop games available. Game titles across web and apps may vary, but if you’re playing through a browser, then the game will be exactly the same. Most casino game companies worth their weight have ensured that they have a responsive design for their browser-based game. This ensure that you’re getting an experience that looks and feels good on mobile too – fitting your screen correctly.
    The differences between playing on a browser version of a game on your phone versus playing the browser version of the game on your desktop may vary from company to company as well as from game to game. It all depends on the amount of effort the company has put into making sure that you get a good gaming experience on your mobile phone. For the majority of casino houses they’ve made a fairly good effort to ensure game play is smooth, uninterrupted, and fits well on your screen. In Gonzo’s Quest from software provider Netent, for instance, there is no difference between the desktop and mobile gameplay whatsoever. There are many more examples and the best way to test them for yourself is to play for free using the free spins coupons you can find at this review site. Playing with free spins is one of the best ways to test casino games online.
    If you’re playing casino games through an app then this is designed specifically for mobile and you’ll be receiving the best game play possible. However, both are good choices, and depending on your needs, wants, and the type and style that you want to play, both can be a good choice.

    The type of phone you have will make a difference too

    Depending on the type of mobile you have will dictate how good your experience of gambling will be on the device. It’s also depend how fast your download and uploads speeds are on whatever network you’re connected to. Playing on an old iPhone 4S will not be nowhere near as pleasant as playing on the latest Samsung Galaxy S8. This is due to differences in processor speed, graphics, screen size, touch, etc. etc. While you can play on both, the experience will be superior on the better phone.
    If you’re looking for the best gaming phones for gaming in general, then check out the Samsung Galaxy S8+ (the larger size than the S8), the LG G6, or Huawei Mate 9. These are all well spec’d and will offer the smoothest gaming experience while not taking too much of a toll on the battery.
    However, it’s not generally necessary to have a high power and graphically advanced phone to play online casino games on. The type of games that are truly taxing on a phone are the surreal first person style 3D games that you’d expect to see on a PlayStation. Generally, casino style games are a lot more basic and chew through a lot less power than these other style of games.
    So, in summary: while it is true that the type of phone that you have can make a difference to your gaming experience, and the online casino company through which you play can make a difference, if you choose a house who are dedicated to bring about a good mobile experience for their customers then you won’t find much difference between playing on your laptop and playing on your phone. And for many people, the experience is more enjoyable because it’s right there in your pocket.



    Friday, October 06, 2017

    Do You Need A VPN When Running Linux?

    Lately, internet users (read: everyone) are in a bit of a frenzy regarding online security. Antivirus and antimalware software is soaring in popularity. There are a million VPN apps for your smartphone (most of which don’t work). And everyone is telling you to stop what you're doing and subscribe to one.

    But why do you really need a VPN? And is there any particular reason for a Linux user to get one?

    The online world is dangerous

    It should be no secret by now that the online world is dangerous. As much as we like to pretend we’re perfectly safe, the truth is that our very identities are on the line. Take the recent Equifax breach in the U.S. as an example. This credit checking service that many people had forgotten they'd once subscribed to, leaked so much information that almost half the population of America is at risk of identity theft.

    But if we really stop to think about it, there are easier ways to get a hold of your identity than hacking a big company. Hackers can go directly after you, through your unsecured network. What they'll do once they’re “in” might range from installing malicious software to stealing directly from your bank account.

    And yes, a VPN is not going to magically protect you from master hackers. Persistent hackers can find a way through your security… eventually. But unless you're a politician or celebrity or have some level of national or international importance, they're not going to go to those lengths. In a neighbourhood that doesn't lock its doors, one wall looks very daunting.

    If you're convinced, have a look at the best VPNs for 2017.

    But for Linux users, there’s another reason why VPNs are so attractive.

    The internet should be open

    There will always be a battle between those of us who believe in open access to information and those who want to hoard it. Big companies try to prevent access to content so that they can squeeze out a few extra bucks.

    We see this time and time again with regional blocks on content, whether music, movies, TV series, or games. Companies prevent us from getting access at a reasonable price. And they don’t do it for the artists’ sakes. Record companies have for decades overcharged for albums, and only streaming forced them to change their approach.

    So too with television. Some content you just cannot get in India, for example. And although no one benefits from regional blocks, companies simply cannot let go, for fear that consumers might find that affordable, open access is a worthwhile pursuit.


    VPNs give us a way to continue using the internet as we should – with no frustrating censorship, with access to content no matter where we are, and without spending mountains of money on what we should be able to access for free.













































    Wednesday, September 13, 2017

    Understanding LVM snapshots (create, merge, remove, extend)

    Below steps are tested on Red Hat Enterprise Linux 7.

    Being a system administrator we have to perform activities which leads to filesystem and data changes so it is always a good idea to keep a backup but taking a backup everytime might not be possible although backup is one of the option for a timely run like every night backup to protect our data but what if you know you are planning to perform some application upgrade which has thousands of files and you need an immediate fix so instead of copying all the files which needs huge amount of storage and alot of copy time we prefer to take a snapshot.

    NOTE: LVM snapshot should NOT be mistaken with backup.

    How snapshot sizing works? How different is it from backup?

    LVM snapshot are very much space efficient, by space efficient I mean when a snapshot is created for a logical volume with 10GB space the snapshot will take very less space (next to nothing) but as the content of the source volume increases the snapshot volume size will also increase accordingly. Hence a snapshot must be created but should not be kept for a long time as iy will end up eating alot of space. In such cases backup is the preferred option instead of snapshot.





    So how space efficient is the LVM snapshot?

    Lots of change = not very space efficient
    Small amount of change = very space efficient

    IMPORTANT NOTE: In LVM we have a feature where a snapshot size can be extended but that does not means that any data modified after creating a snapshot will also be restored since the snapshot was extended at a later point of time. 

    Snapshot will revert back to the state of point where it was taken and any data modified after taking the snapshot will be overwritten.
    Let us start with some examples

    I have a setup with below logical volumes
    # lvs
      LV        VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
     
    data      system -wi-a-----  2.00g
      opt       system -wi-ao----  2.00g
      root      system -wi-ao----  2.00g
      swap      system -wi-ao----  4.00g
      tmp       system -wi-ao---- 12.00g
      var       system -wi-ao----  2.00g

    For the sake of this article we will create a snapshot of our 'data' partition. But before that lets check the existing content of our data partition

    Our logical volume is mounted on /data directory
    # df -Th /data/
    Filesystem              Type  Size  Used Avail Use% Mounted on
    /dev/mapper/system-data ext4  2.0G  6.1M  1.8G   1% /data

    with below content
    # ll /data/
    total 28
    drwxr-xr-x 2 root root  4096 Sep 13 03:24 dir1
    drwxr-xr-x 2 root root  4096 Sep 13 03:24 dir2
    drwxr-xr-x 2 root root  4096 Sep 13 03:24 dir3
    -rw-r--r-- 1 root root     0 Sep 13  2017 file1
    -rw-r--r-- 1 root root     0 Sep 13  2017 file2
    -rw-r--r-- 1 root root     0 Sep 13  2017 file3
    drwx------ 2 root root 16384 Sep 13 03:17 lost+found

    So the same is expected after performing a lvm snapshot merge.

    use below command

    To create a snapshot

    # lvcreate -L 1G -s -n snap_data /dev/system/data
    Using default stripesize 64.00 KiB.
    Logical volume "snap_data" created.

    Here,
    -L means assign LogicalVolume Size
    -s or (--snapshot) means create a snapshot volume
    -n or (
    --name) means the name of the snapshot logical volume
    Lastly (/dev/system/data) is the path of the logical volume whose snapshot has to be created

    Here we are creating a snapshot volume with 1G size (snap_data) for /dev/system/data logical volume

    These snapshots are read write so you can mount this snapshot volume and check the data/content.
    # mount /dev/system/snap_data /mnt/
    Check the content of the snapshot
    # ll /mnt/
    total 28
    drwxr-xr-x 2 root root  4096 Sep 13 03:24 dir1
    drwxr-xr-x 2 root root  4096 Sep 13 03:24 dir2
    drwxr-xr-x 2 root root  4096 Sep 13 03:24 dir3
    -rw-r--r-- 1 root root     0 Sep 13  2017 file1
    -rw-r--r-- 1 root root     0 Sep 13  2017 file2
    -rw-r--r-- 1 root root     0 Sep 13  2017 file3
    drwx------ 2 root root 16384 Sep 13 03:17 lost+found

    We can get the details of the snapshot and the parent logical volume
    # lvs /dev/system/snap_data
      LV        VG     Attr      
    LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
     
    snap_data system swi-aos--- 1.00g      data   0.00

    As you see the new logical snapshot volume is visible in the above information

    IMPORTANT NOTE: Make sure the snapshot volume size is always equal to the origin volume if you feel that the origin volume is going to be left out for a long period and high amount of data will be written on the source partition as there are chances of snapshot getting corrupted if the origin volume size becomes larger than the snapshot volume size.

    What if I start working on this origin partition and it gets filled up with more than 1GB of data?

    Here my snapshot volume size is 1G while the actual volume size is 2G where only few MB is used
    # df -h /data/
    Filesystem               Size  Used Avail Use% Mounted on
    /dev/mapper/system-data  2.0G  6.1M  1.8G  
    1% /data

    Let me put some dummy content with 1G size in the /data partition
    # dd if=/dev/zero of=/data/dummy_file2 bs=1G count=1 oflag=dsync
    1+0 records in
    1+0 records out
    1073741824 bytes (1.1 GB) copied, 98.5545 s, 10.9 MB/s

    Check the status of snapshot
    # lvs
      LV        VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      data      system owi-aos---  2.00g
      mgtserv   system -wi-ao----  2.00g
      opt       system -wi-ao----  2.00g
      root      system -wi-ao----  2.00g
     
    snap_data system swi-I-s---  1.00g      data   100.00
      swap      system -wi-ao----  4.00g
      tmp       system -wi-ao---- 12.00g
      var       system -wi-ao----  2.00g

    As you see it shows 100% of the snapshot size is occupied which means now this snapshot is corrupted and cannot be used

    Check this further using below commands
    # lvdisplay /dev/system/snap_data
      --- Logical volume ---
      LV Path                /dev/system/snap_data
      LV Name                snap_data
      VG Name                system
      LV UUID                NwJSQu-NjIr-7Qn0-Wo0q-ig7d-3apy-eChdWD
      LV Write Access        read/write
      LV Creation host, time nds18-rdssrv, 2017-09-13 04:23:35 -0400
      LV snapshot status     INACTIVE destination for data
      LV Status              available
      # open                 0
      LV Size                2.00 GiB
      Current LE             64
      COW-table size         1.00 GiB
      COW-table LE           32
      Snapshot chunk size    4.00 KiB
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:12

    # dmsetup status
    system-var: 0 4194304 linear
    system-
    snap_data: 0 4194304 snapshot Invalid

    # grep Snapshot /var/log/messages
    Sep 13 03:32:19 nds18-rdssrv lvm[12168]: WARNING: Snapshot system-snap_data is now 80.02% full.
    Sep 13 03:54:07 nds18-rdssrv lvm[1595]: WARNING: Snapshot system-snap_data is now 83.52% full.
    Sep 13 04:29:52 nds18-rdssrv lvm[1606]: WARNING:
    Snapshot system-snap_data changed state to: Invalid and should be removed.

    As you see once the snapshot size becomes full it becomes unusable so must be removed
    # lvremove -f /dev/system/snap_data
      Logical volume "snap_data" successfully removed

    How can we avoid such scenarios?

    We have an option to automatically extend the logical volume if the origin volume parition size reaches the threshold or manually extend the snapshot size before it becomes 100% occupied.

    NOTE: Automatic method is preferred as once the snapshot volume reaches 100% space, the snapshot will get corrupted as seen above.

    For manually extending the snapshot volume

    Before extending the snapshot volume
    # lvdisplay /dev/system/snap_data
      --- Logical volume ---
      LV Path                /dev/system/snap_data
      LV Name                snap_data
      VG Name                system
      LV UUID                ETHmgE-sgz0-4o7Q-3GDQ-pUy4-CJPo-D3nlIe
      LV Write Access        read/write
      LV Creation host, time nds18-rdssrv, 2017-09-13 05:21:37 -0400
      LV snapshot status     active destination for data
      LV Status              available
      # open                 0
      LV Size                2.00 GiB
      Current LE             64
     
    COW-table size         1.00 GiB
      COW-table LE           32
      Allocated to snapshot  0.00%
      Snapshot chunk size    4.00 KiB
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:12

    # lvextend -L +1G /dev/system/snap_data
      Size of logical volume system/snap_data changed from 1.00 GiB (32 extents) to 2.00 GiB (64 extents).
      Logical volume system/snap_data successfully resized.

    After extending the snapshot volume
    # lvdisplay /dev/system/snap_data
      --- Logical volume ---
      LV Path                /dev/system/snap_data
      LV Name                snap_data
      VG Name                system
      LV UUID                ETHmgE-sgz0-4o7Q-3GDQ-pUy4-CJPo-D3nlIe
      LV Write Access        read/write
      LV Creation host, time nds18-rdssrv, 2017-09-13 05:21:37 -0400
      LV snapshot status     active destination for data
      LV Status              available
      # open                 0
      LV Size                2.00 GiB
      Current LE             64
      COW-table size         2.00 GiB
      COW-table LE           64
      Allocated to snapshot  0.00%
      Snapshot chunk size    4.00 KiB
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:12

    As you see the LVM COW table size (Copy On Write) size has increased from 1 Gb to 2 GB

    For automatically extending the snapshot volume

    Open the below file and change snapshot_autoextend_threshold value from 100 to some other value lower than 100 and snapshot_autoextend_percent to the value you would like to extend the snapshot volume, more detail on this attrubute is provided in the logfile
    # vim /etc/lvm/lvm.conf
            # Configuration option activation/snapshot_autoextend_threshold.
            # Auto-extend a snapshot when its usage exceeds this percent.
            # Setting this to 100 disables automatic extension.
            # The minimum value is 50 (a smaller value is treated as 50.)
            # Also see snapshot_autoextend_percent.
            # Automatic extension requires dmeventd to be monitoring the LV.
            #
            # Example
            # Using 70% autoextend threshold and 20% autoextend size, when a 1G
            # snapshot exceeds 700M, it is extended to 1.2G, and when it exceeds
            # 840M, it is extended to 1.44G:
            # snapshot_autoextend_threshold = 70
            #
           
    snapshot_autoextend_threshold = 70

            # Configuration option activation/snapshot_autoextend_percent.
            # Auto-extending a snapshot adds this percent extra space.
            # The amount of additional space added to a snapshot is this
            # percent of its current size.
            #
            # Example
            # Using 70% autoextend threshold and 20% autoextend size, when a 1G
            # snapshot exceeds 700M, it is extended to 1.2G, and when it exceeds
            # 840M, it is extended to 1.44G:
            # snapshot_autoextend_percent = 20
            #
            snapshot_autoextend_percent = 50

    For this article I will use above values

    Next again we need to start from scratch which means create a new snapshot volume
    # lvcreate -L 1G -s -n snap_data /dev/system/data
    Now lets again try to fill up the /data partition with some more data more than 1G

    First attempt I will create dummy file with 512MB
    # dd if=/dev/zero of=/data/dummy_file2 bs=512M count=1 oflag=dsync
    1+0 records in
    1+0 records out
    536870912 bytes (537 MB) copied, 16.3479 s, 32.8 MB/s

    Lets check the snapshot volume size, since the snapshot volume was 1G it easily was able to afford this change without extending its volume size
    # lvs
      LV        VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      data      system owi-aos---  2.00g
      mgtserv   system -wi-ao----  2.00g
      opt       system -wi-ao----  2.00g
      root      system -wi-ao----  2.00g
      snap_data system swi-a-s---  1
    .00g      data   50.21

    Next lets create one more dummy file with additional 512M of data
    # dd if=/dev/zero of=/data/dummy_file bs=512M count=1 oflag=dsync
    1+0 records in
    1+0 records out
    536870912 bytes (537 MB) copied, 28.1028 s, 19.1 MB/s

    Now lets check the lvs status for snap_data, as you see the volume size increased to 1.5GB since we had asked lvm to extend to 50% if the threshold is reached
    # lvs
      LV        VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      data      system owi-aos---  2.00g
      mgtserv   system -wi-ao----  2.00g
      opt       system -wi-ao----  2.00g
      root      system -wi-ao----  2.00g
      snap_data system swi-a-s---  
    1.50g      data   66.94
      swap      system -wi-ao----  4.00g
      tmp       system -wi-ao---- 12.00g
      var       system -wi-ao----  2.00g
     

    So this worked perfectly. But make sure the origin data size doesnot increases more than 1.5GB of rlse the snapshot will again get corrupted

    How to restore/merge the snapshot?


    Next once you are done it is time to restore your snapshot.
    IMPORTANT NOTE: Before merging the snapshot make sure that the origin source is in unmounted state or else the merging will be postponed for a pending activation of the logical volume. Below are examples for both the scenario

    If the origin source is in mounted state

    To merge the snapshot use the below command
    # lvconvert --merge /dev/system/snap_data
     
    Can't merge until origin volume is closed.
      Merging of snapshot system/snap_data will occur on next activation of system/data.

    Since /data partition is in mounted state the snapshot merge failed, to perform this merge you have to perform manual lvm activation.
    Unmount the 'data' partition
    # umount /data/
    De-activate the data partition
    # lvchange -an  /dev/system/data
    Re-activate the same
    # lvchange -ay  /dev/system/data
    Now validate you content of 'data' partition.
    # ll /data/
    total 524320
    drwxr-xr-x 2 root root      4096 Sep 13 03:24 dir1
    drwxr-xr-x 2 root root      4096 Sep 13 03:24 dir2
    drwxr-xr-x 2 root root      4096 Sep 13 03:24 dir3
    -rw-r--r-- 1 root root         0 Sep 13 03:24 file1
    -rw-r--r-- 1 root root         0 Sep 13 03:24 file2
    -rw-r--r-- 1 root root         0 Sep 13 03:24 file3
    drwx------ 2 root root     16384 Sep 13 03:17 lost+found

    NOTE: If for some reason you cannot perform this re-activation, then this snapshot merge will happen during the next reboot of the node

    If the origin is in un-mounted state

    # lvconvert --merge /dev/system/snap_data
      Merging of volume system/snap_data started.
      data: Merged: 33.20%
      data: Merged: 54.44%
      data: Merged: 75.88%
      data: Merged: 98.10%
     
    data: Merged: 100.00%

    On another terminal if you check we can see that the data partition has "O" attribute which means snapshot is merging with origin
    # lvs
      LV        VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      data      system
    Owi-a-s---  2.00g             49.74
      mgtserv   system -wi-ao----  2.00g
      opt       system -wi-ao----  2.00g
      root      system -wi-ao----  2.00g
      swap      system -wi-ao----  4.00g
      tmp       system -wi-ao---- 12.00g
      var       system -wi-ao----  2.00g

    and you are done here, our 'data' partition is back to its original state
    # ll /data/
    total 524320
    drwxr-xr-x 2 root root      4096 Sep 13 03:24 dir1
    drwxr-xr-x 2 root root      4096 Sep 13 03:24 dir2
    drwxr-xr-x 2 root root      4096 Sep 13 03:24 dir3
    -rw-r--r-- 1 root root         0 Sep 13 03:24 file1
    -rw-r--r-- 1 root root         0 Sep 13 03:24 file2
    -rw-r--r-- 1 root root         0 Sep 13 03:24 file3
    drwx------ 2 root root     16384 Sep 13 03:17 lost+found



    Related Articles:
    How to reduce LVM size in Linux step by step (online without reboot)
    How to rename Logical Volume and Volume Group in Linux
    How to remove logical and physical volume from Volume Group in Linux
    How to extend/resize Logical Volume and Volume Group in Linux


    Follow the below links for more tutorials

    Configure Red Hat Cluster using VMware, Quorum Disk, GFS2, Openfiler
    Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
    How to configure Samba 4 Secondary Domain Controller
    How to secure Apache web server in Linux using password (.htaccess)
    How to register Red Hat Linux with RHN (Red Hat Network )
    Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
    15 tips to enhance security of your Linux machine
    Why is Linux more secure than windows and any other OS
    What is the difference between "su" and "su -" in Linux?
    What is kernel-PAE in Linux?
    What is swappiness and how do we change its value?
    How to log iptables messages in different log file
    What are the s and k scripts in the etc rcx.d directories
    How to check all the currently running services in Linux
    How to auto start service after reboot in Linux
    What is virtual memory, paging and swap space?

    Tuesday, September 12, 2017

    sed: perform search and replace only on specific lines (not globally)

    By default when we perform search and replace action using sed, that is done globally within a file. Although if you have a line number you can perform the replace based on the line number but what if you have a file where you don't know the line number?

    Let us got through different examples where you can perform such search and replace based on string match but only on the specific lines





    Below is my sample file
    # cat /tmp/file
    four five six
    one
    seve eight nine
    one two three
    one
    ten eleven twelve
    one


    Perform search and replace based on line number

    In my sample file I have multiple instances of word "one" but I would like my search and replace regex to work only on the 4th line

    To do the same use below regex
    # sed -e '4s/one/replaced/g' /tmp/file
    four five six
    one
    seve eight nine
    replaced two three
    one
    ten eleven twelve
    one

    Here as you see I have given my line number along with the substitute regex, so the can be replaced with any other line number where you want to perform your replacement

    Perform search and replace based on a specific string match

    Using a line number is one way but still not the best way, the easier way is if you already know a string which exists n the line where you want to perform the replacement. For eg in my sample file I would like to replace the word 'one' on the line which has string'two'

    Observe the below regex
    # sed -e '/two/s/one/replaced/g' /tmp/file
    four five six
    one
    seve eight nine
    replaced two three
    one
    ten eleven twelve
    one

    As you see with the 'substitute' regex I have provided the string which it should search first before performing the requested action. So even when my file had 3 instances of 'one', the replace was performed only on the line having 'two'

    The above regex can be combined with other meta characters to improvise the task

    Suppose if there are multiple instances of 'two' in the file but you would like to perform replacement only on the line which starts with 'two'
    # sed -e '/^two/s/one/replaced/g' /tmp/file
    To perform a replace on a line which ends with 'two'
    # sed -e '/two$/s/one/replaced/g' /tmp/file







    Monday, September 11, 2017

    sed: delete all blank lines from a text file

    Here our requirement is to remove all the blank lines from a file.





    Our sample file
    [root@golinuxhub ~]# cat /tmp/file
    This is line one

    This is line two
    This is line three

    This is line four

    [root@golinuxhub ~]#

    Use the below command to remove all the blank lines from a text file
    [root@golinuxhub ~]# sed -e '/^$/d' /tmp/file
    This is line one
    This is line two
    This is line three
    This is line four
    [root@golinuxhub ~]#

    Here the regular expressions used are
    ^ - means starting with
    $ - means ending with

    Since we did not provided any other other character between our regular expressions, this is a notation to consider it as blank line.

    To perform the action in the same file use "-i"
    [root@golinuxhub ~]# sed -i '/^$/d' /tmp/file

    [root@golinuxhub ~]# cat /tmp/file
    This is line one
    This is line two
    This is line three
    This is line four


    I hope the article was useful.















    sed: case insensitive actions (search replace delete..)

    In this article I will show you different ways to perform an action (as per your requirement) ignoring the case of the letters to be searched in a file

    Below is our sample file
    [root@golinuxhub ~]# cat /tmp/file
    one
    Two
    three
    Three
    two
    One

    Perform a case insensitive search and replace

    By default if I do a normal search and replace it will look something like below. Here I am replacing "two" with "new-word"
    [root@golinuxhub ~]# sed -e 's/two/new-word/g' /tmp/file
    one
    Two
    three
    Three
    new-word
    One

    Let us perform the same for all the word "two" ir-respective of its case, this can be done using "I" as shown below
    [root@golinuxhub ~]# sed -e 's/two/new-word/Ig' /tmp/file
    one
    new-word
    three

    Three
    new-word
    One

    As you see all the occurrences of the word "two" is replaced with "new-word"





    There are more ways to do this, for example you know that only one letter of the text you wish to replace may have different case so here
    [root@golinuxhub ~]# sed -e 's/[tT]wo/new-word/g' /tmp/file
    one
    new-word
    three
    Three
    new-word
    One

    If here are more than one letters which can be of different case then the same can be done accordingly using below
    [root@golinuxhub ~]# sed -e 's/[tT][wW]o/new-word/g' /tmp/file
    one
    new-word
    three
    Three
    new-word
    One

    To ignore case for all 3 letters
    [root@golinuxhub ~]# sed -e 's/[tT][wW][oO]/new-word/g' /tmp/file
    one
    new-word
    three
    Three
    new-word
    One

    To perform in-file replacement use below syntax
    # sed -i 's/two/new-word/Ig' /tmp/file
    OR
    # sed -i 's/[tT][wW]o/new-word/g' /tmp/file

    Perform a case insensitive search and delete

    To perform a search and delete by default below syntax is used
    [root@golinuxhub ~]# sed -e '/two/d' /tmp/file
    one
    Two
    three
    Three
    One

    To perform the same for all the word "two" ir-respective of its case use "I" as shown below
    [root@golinuxhub ~]# sed -e '/two/Id' /tmp/file
    one
    three
    Three
    One

    There are more ways to do this, for example you know that only one letter of the text you wish to delete may have different case so here
    [root@golinuxhub ~]# sed -e '/[tT]wo/d' /tmp/file
    one
    three
    Three
    One

    If here are more than one letters which can be of different case then the same can be done accordingly using below
    [root@golinuxhub ~]# sed -e '/[tT][wW]o/d' /tmp/file
    one
    three
    Three
    One

    To ignore case for all 3 letters
    [root@golinuxhub ~]# sed -e '/[tT][wW][oO]/d' /tmp/file
    one
    three
    Three
    One

    To perform in-file replacement use below syntax
    # sed -i '/two/Id' /tmp/file
    OR
    # sed -i '/[tT][wW][oO]/Id' /tmp/file



    Sunday, September 10, 2017

    10 Basic examples of SED to perform per line action (print/delete)

    In this article I will show you various sed expressions which can be used and combined to print or delete specific line from a file

    IMPORTANT NOTE:

    My examples provided are only to print the action performed, to use the same for delete action remove '-n' and replace 'p' with 'd' on individual command

    To save the output to a new file after performing delete action use below syntax
    # sed -e 'your regex' /old/file > /new/file2

    To save the action in the same file use below syntax
    # sed -i 'your regex' /path/to/file

    Our sample file
    # cat /tmp/file
    one
    two
    three
    four
    five
    six

    1. Print the first line from a file

    Below command will print the first line from the file
    # sed -ne '1p' /tmp/file
    one

    2. Print the last line from a file

    In regex "$" means last hence the same can be used to print the last line

    Below sed regex can be used
    # sed -ne '$p' /tmp/file
    six

    3. Print specific line number

    In my sample file I have six lines so lets try to print 2nd line

    Use the below syntax to only print 2nd line
    # sed -ne '2p' /tmp/file
    two

    Below regex will print only the 4th line
    # sed -ne '4p' /tmp/file
    four

    4. Print multiple line based on line number

    In the above example we printed single line but the same can be done if you have the list of line numbers where you want to perform your work.

    To print multiple line using line numbers use ";" (semi colon) to do the work as shown in below example

    You can also print the 2nd and 4th file numbers using the below command
    # sed -ne '2p;4p' /tmp/file
    two
    four

    The same can be done using the below syntax for 'n' no of line numbers
    # sed -ne '2p;5p;7p' /path/to/file
    Here I am printing the 2nd,5th and 7th line

    5. Print lines between specific line numbers from a file

    In the above example we are deleting/printing specific line numbers but what if you wish to Print lines between two line numbers

    In the above command semi-colon was used to print specific line numbers, but to print between a range use comma as shown in below example

    Using below sed regex you can print the content between 'two' and 'four'
    # sed -ne '2,4p' /tmp/file
    two
    three
    four

    6. Print all other lines except for the provided line number

    Here we would like to print all the rest of the content of our file instead the line numbers which we want to save/print. Instead of using 'p' we will use '!p' which should do the work.

    Print the content of the file excluding the content between 2nd and 4th line
    # sed -ne '2,4!p' /tmp/file
    one
    five
    six

    Next print all line except first line
    # sed -ne '1!p' /tmp/file
    two
    three
    four
    five
    six

    Next print all line except last line
    # sed -ne '$!p' /tmp/file
    one
    two
    three
    four
    five

    7. Print all lines matching a pattern

    Below example can help you print a(list of) specific line which matches a pattern, for this eg the pattern we are searching os 'two'
    # sed -ne '/two/p' /tmp/file
    two

    8 Print all the lines between two matching patterns

    In our sample file I would like to print the content between 'one' and 'four', so our sed must perform the required action between these two matching patterns

    Similarly to print the same
    # sed -ne '/one/,/four/p' /tmp/file
    one
    two
    three
    four

    9. Print 'n' number of lines after a match

    Use '+n' to print 'n' number of lines after a match using below syntax

    If you wish to print the match and one additional line following the match use below command
    # sed -ne '/one/,+1p' /tmp/file
    one
    two

    Similarly if you wish to print 2 lines following the match
    # sed -ne '/one/,+2p' /tmp/file
    one
    two
    three

    10. Print all the lines which has numerical value

    Here lets change our sample file to below
    # cat /tmp/file
    one 1
    2 two
    three 2003
    4 four
    five 55
    six 666

    Here firstly print all the lines which starts with numerical value
    # sed -ne '/^[0-9]/p' /tmp/file
    2 two
    4 four

    Print all lines with has two or more numerical value only
    # sed -ne '/[0-9][0-9]/p' /tmp/file
    three 2003
    five 55
    six 666

    Print all lines which ends with numerical value
    # sed -ne '/[0-9]$/p' /tmp/file
    one 1
    three 2003
    five 55
    six 666