Linux disk partition info: UUID, Label, Name

There are numerous ways to find disk partition info in Linux. The following lists the ways of getting Linux partition information.

Read more »

Howto mount windows NTFS, FAT partition onto Debian / Ubuntu Linux

First lets see GUI mode of mounting windows NTFS / FAT32 partition on Linux system.

On system main menu under Places, it will appear all available partition accessible by system. If your partition having LABEL, label will appear. Other wise partition size will be the menu entry. To mount such partition just click on that. On Command Line Mode: You must know partition name/UUID/ label of your partition. Use the following command to list out all available partitions

sudo sfdisk -l

it output something like this

Disk /dev/sda: 7296 cylinders, 255 heads, 63 sectors/track
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1   *      0+   1274    1275-  10241406    7  HPFS/NTFS
/dev/sda2       1275+   7294    6020-  48355619+   f  W95 Ext'd (LBA)
/dev/sda3          0       -       0          0    0  Empty
/dev/sda4          0       -       0          0    0  Empty
/dev/sda5       1275+   3877-   2603-  20901443   83  Linux
		start: (c,h,s) expected (1023,254,63) found (1023,1,1)
/dev/sda6       3877+   7294-   3418-  27452416    7  HPFS/NTF

Now I am going to mount partition /dev/sda6  of type NTFS. Ensure mount point directory exists or otherwise just create it

sudo mkdir -p /media/mywin

then mount command is

ntfs-3g /dev/sda6 /media/mywin

or you can use mount command also

sudo mount -t ntfs -o nls=utf8,umask=0222 /dev/sda6 /media/mywin

To mount if it is a FAT partition:

sudo mount -t vfat -o iocharset=utf8,umask=000 /dev/sda6 /media/mywin

To unmount (mounted at /media/mywin) partition:

sudo umount /media/mywin

Here,
-t : Specify file system type (such as NTFS or FAT)
umask=VALUE: Set the umask (the bitmask of the permissions that are not present). The default is the umask of the current process. The value is given in octal.
iocharset=VALUE: Character set to use for converting between 8 bit characters and 16 bit Unicode characters. The default is iso8859-1. Long filenames are stored on disk in Unicode format.

Check Linux shell variable is set or exists and find length of Linux shell variable

Often it is required in Linux, to check existence of linux bash shell variable. In simple logic length of variable can be used to test whether variable is empty(Zero length ) or  having some content.

To find Length of shell variable

LENGTH=${#Variable}

But this may not be use full in shell scripting, before proceed to read content from Linux bash shell variable. So to check linux shell variable existence/defined/set

[ -z $VARIABLE ] && echo unset || echo set

SSH Local, Remote and Dynamic port forwarding-ssh tunneling

Introuduction

SSH portfowarding or SSH tunneling allows to create a secure connection between a local computer and a remote machine. With SSH tunnel an virtual secure patch is established. Because the connection is encrypted, SSH tunneling is useful for transmitting information that uses an unencrypted protocol, such as Emails(SMTP,POP3,IMAP), Shell, VNC, or anything.

SSH port forwarding types

  1. Local port forwarding
  2. Remote port forwarding
  3. Dynamic port forwarding
  4. X11 forwarding (GUI forwarding)

Read more »

Linux shell: suppress error messages like ls: cannot access /tmp/*.pdf: No such file or directory

Often I was facing in my linux script, in ls command

$ ls /tmp/*.pdf | wc -l
ls: cannot access /tmp/*.pdf: No such file or directory

like errors. Later I was realized that this error comes only when there is no files matching the given pattern.
Any how it is annoying error messages in script, and it should be suppressed. So good idea is, redirect stderr output to /dev/null.
Use above command as
ls /tmp/*.pdf 2> /dev/null | wc -l
This works fine for me.

Linux: Change the Default Editor for crontab

Many of the utilities in Linux use a text editor to allow you to edit configuration options and files. An example of this is using the crontab command, which allows you to edit your cron jobs using the default editor.
crontab asks to choose a default editor only at first time. There was no option to change default editor program.

This default setting is saved in $HOME/.selected_editor file. There is no strict rule, this file exists in all linux. If exists, Open this file in your favorite editor. The file contents looks like this
# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/bin/ed"

Read more »

Linux shell script: Single and Multi line comments

In Linux script comments are good programming practice. If your are writing large numbers of shell script, you may not be remember every thing what each line doing in script. Comments are very useful when when we diagnose the script on later date.

  1. Single line comment
  2. single line comment is useful, when you want to comment out a full line OR comment out part of line.

    1. Commenting full line

    2. #comments here
      example
      #shows who are logged in
      who

    3. Commenting part of line

    commands #comments here
    example
    who #shows who are logged in
    The only condition is that any content after # sign is treated as comment.

  3. Multi line or Block level comment

We can also use # sign for all lines OR block of lines to comment out full block.

# my comment line 1
# my comment line 2
# …
# my comment line N

This will not efficient if you want comment more lines. Some programming languages having /* … */ style of comment. Unfortunately Linux shell does not support this. There is no multi line comment in Shell script. Any how we can go for alternative way,

#!/bin/bash
echo "my multi line comment"
<< my comment line 1
my comment line 12
...
my comment line N
COMMENT
echo "After comment"

Just make sure that the string ‘COMMENT’ does not appear among the commented lines. Or chose a marker which is not so easily repeated. There is no strict rule to use a phrase COMMENT. You can use any unique word.

#!/bin/bash
echo "my multi line comment"
<<my comment line 1
my comment line 12
...
my comment line N
my_comment_block_tag

Linux: How to find a particular text string in all files all sub-directories recursively?

In Linux grep is power full tool to search text in files.
From command line give command, to search a text in single file.
grep "search phrase" filename

To search in more or all files.
grep “mail” /var/log/*.log
this will search keyword “mail” in all log files.
grep "search phrase" *
This will search in all files in current directory.

There is a nice option called ‘-r’ available in grep to search recursively in all sub directories.
grep -r "password" /var/www/
This will search in all sub directories of /var/www/ recursively.

Another way is by using find command,
Even though grep is pretty easy, It can’t easily search in all sub directories in specific type of file only. This example clarify you how it is,

find /var/www/mydomain.com/ -name *.php -exec grep "search text" {} \;
This seems to be dirty for you, but this will do what exactly we want. Here we are searched in all sub directories of /var/www/mydomain.com/ and file name ends with .php only.

Hope you are enjoy greping.

Linux: How to delete / remove hidden files in with ‘rm’ command?

To delete all content in any directory, including all sub-directories and files, use this:
rm -rf tstdir/*
In this case hidden files (prefixxed with ‘.’ dot ) will be ignored.

Similarly to delete all content of the current directory:

rm -rf *

In these cases hidden files (prefixxed with ‘.’ dot ) will be ignored. Files with a name starting with a dot are hidden in Linux. To delete all the hidden files as well, use command:
rm -rf .*
Any how above command gives some messages like

rm: cannot remove directory: `.'
rm: cannot remove directory: `..'

This is expected one. Because ‘.’ and ‘..’ will also exists in any directory, it refers ‘current’ and ‘parent’ directory.

HowTo access LAN through Linux Server connected to Internet using ADSL BroadBand router

Question

I have a ADSL modem (192.168.1.1) connected to an ubuntu Linux machine (192.168.1.2), and in the modem port forwarding is enabled in such a way that any request on my public ip (internet ip) of the ADSL router is forwarded to 192.168.1.2 on the corresponding port as defined in the port forwarding table in the router.

And also the ubuntu machine has got LAN IP (10.40.155.165) belongs to my LAN network who’s ip ranges from
10.40.155.161 to 10.40.155.188.

And question is about accessing VNC desktop of 10.40.155.175:5900 from internet.
This is setup of my network,

                               192.168.1.2           10.40.155.175
                                       |                  |
----------------         ---------     |  ----------      | -----------
| You are from |         |  ADSL  |    v  | Linux  |      v | LAN     |
| Internet     |-------->| Router |------>| server |------->| machine |
----------------         ----------       ----------        -----------
                       ^            ^                ^
                       |            |                |
             Public IP Addr    192.168.1.1         10.40.155.165

Answer

In various ways we can do that, In any way, first you must do NATing at ADSL router to enter into your private network.

  1. Setup NAT at ADSL router and another NAT at Server

  2. Set up NEt at both Router and Server to carry your request from client to destination LAN machine.

    So Setup NAT/Port forwarding at Router as below

    WAN side Port:10000 to LAN side 192.168.1.2:10000

    and NAT at Linux Server

    WAN side:10000 to LAN side 10.40.155.175:5900

  3. Setup VPN server at Linux server

  4. and access via VPN ( It is bit complicated)
    Install OpenVPN-server( Its free) at Linux server, Configure VPNs server allow clients to access LAN.
    Connect to your Linux server via VPN. After that your will be alloted an Private IP.
    If connected successfully , you can access all resources in LAN.

  5. Setup SSH dynamic port forwarding

  6. SSH to Linux Server with dynamic port forwarding and access it using Web Browser configured with SOCKS proxy
    From your client computer give command,

     ssh -D 9999 user@your-public-ip-or-domain

    then you will be connected to your server via your ADSL router,
    provided that you must properly configure NAT in your ADSL router to allow SSH connection to your server.
    then open an SOCKS enables web browser( firefox will works), set up SOCKS host:localhost, port:9999 ( as given above)
    Enter your address 10.40.155.175:5900, then you will be able to connect using VNC viwer.

  7. Set up SSH Remote port forwarding

At Linux Server set up SSH Remote port forwarding, Syntax for Remote port fowrading is,

ssh user@your-ssh-server -R sshServerPort:RemoteMachineIP:RemoteMachinePort

in our case,

ssh ServerUserID@router-public-ip -R 10000:10.40.155.175:5900

After connection established, open your VNC viwer, give address and port combination as your-public-ip:10000
If every thing is gone well, you cant get desktop of your LAN machine 10.40.155.175