A Note About Inodes, Hard Links & Soft Links

These are some of the most frequently asked/used Unix/Linux terms. Down the line, you will interact a lot with inodes & links. So, inorder to understand Linux fundamentals better, a clear understanding of inodes & links are vital. This chapter deals with these concepts.

An inode is a file-structure or a database of file-parameters on a file-system. Sounds confusing? ๐Ÿ˜‰ In simple words, an inode stores various information related to files/directories present on a file-system. These parameters include but not limited to: –

  • Inode number
  • File size
  • File type
  • Ownerships
  • Permissions
  • Last accessed/deleted/modified dates
  • Block location
  • Links (discussed below)

Remember that inode doesn’t store file-name or file contents. So, think of inode as a “tag” attached to a file (In Unix, everything is a file! Even a directory!!).

So, why inode is important?

To answer this, lets see a few examples. There are “links” that sometimes we create for some files. It can be “hard” or “soft” link. What is a link, you may ask ๐Ÿ˜‰ Well, a link is a pointer to some other file (analogous to shortcuts in Windows). Being hard or soft depends on the type of pointer. You will understand this by seeing the examples shown below.

[root@server scripts]# cd /tmp
[root@server tmp]# echo "Demonstrating links" >original_file.txt
[root@server tmp]# ll original_file.txt
-rw-r--r-- 1 root root 20 Nov 10 09:12 original_file.txt

Lets create a hard link now.

[root@server tmp]# ln original_file.txt hard_link.txt
[root@server tmp]# ll hard_link.txt
-rw-r--r-- 2 root root 20 Nov 10 09:12 hard_link.txt
[root@server tmp]# cat hard_link.txt
Demonstrating links

Changing permissions for original file changes it for hard link as well.

[root@server tmp]# chmod 744 original_file.txt
[root@server tmp]# ll original_file.txt
-rwxr--r-- 2 root root 20 Nov 10 09:12 original_file.txt
[root@server tmp]# ll hard_link.txt
-rwxr--r-- 2 root root 20 Nov 10 09:12 hard_link.txt

Lets create a soft link.

[root@server tmp]# ln -s original_file.txt soft_link.txt
[root@server tmp]# cat soft_link.txt
Demonstrating links

ls -i command is used to see inode number. You can see that hard link has the same inode number as original file. This is the reason hard links have “no” additional space for it.

[root@server tmp]# ls -li original_file.txt
101 -rwxr--r-- 2 root root 20 Nov 10 09:12 original_file.txt
[root@server tmp]# ls -li hard_link.txt
101 -rwxr--r-- 2 root root 20 Nov 10 09:12 hard_link.txt

Soft link gets a new inode number & hence occupies additional space.

[root@server tmp]# ls -li soft_link.txt
102 lrwxrwxrwx 1 root root 17 Nov 10 09:14 soft_link.txt -> original_file.txt

Changing permissions on original file doesn’t affect soft links.

[root@server tmp]# chmod 644 original_file.txt
[root@server tmp]# ll original_file.txt
-rw-r--r-- 2 root root 20 Nov 10 09:12 original_file.txt
[root@server tmp]# ll hard_link.txt
-rw-r--r-- 2 root root 20 Nov 10 09:12 hard_link.txt
[root@server tmp]# ll soft_link.txt
lrwxrwxrwx 1 root root 17 Nov 10 09:14 soft_link.txt -> original_file.txt

stat is the command used to see inode statistics like its number, date attributes etc.

[root@server tmp]# stat original_file.txt
File: `original_file.txt'
Size: 20 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 101 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-11-10 09:12:56.102357938 +0100
Modify: 2015-11-10 09:12:56.102357938 +0100
Change: 2015-11-10 09:15:55.777501723 +0100>
[root@server tmp]# stat hard_link.txt
File: `hard_link.txt'
Size: 20 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 101 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-11-10 09:12:56.102357938 +0100
Modify: 2015-11-10 09:12:56.102357938 +0100
Change: 2015-11-10 09:15:55.777501723 +0100
[root@server tmp]# stat soft_link.txt
File: `soft_link.txt' -> `original_file.txt'
Size: 17 Blocks: 0 IO Block: 4096 symbolic link
Device: fd02h/64770d Inode: 102 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-11-10 09:14:26.783430469 +0100
Modify: 2015-11-10 09:14:26.783430469 +0100
Change: 2015-11-10 09:14:26.783430469 +0100

Deleting the original file doesn’t affect its hard link but it removes the soft link.

[root@server tmp]# rm original_file.txt
rm: remove regular file `original_file.txt'? yes
[root@server tmp]# ll hard_link.txt
-rw-r--r-- 1 root root 20 Nov 10 09:12 hard_link.txt
[root@server tmp]# ll soft_link.txt
lrwxrwxrwx 1 root root 17 Nov 10 09:14 soft_link.txt -> original_file.txt
[root@server tmp]# cat soft_link.txt
cat: soft_link.txt: No such file or directory
[root@server tmp]#

There is a limitation on hard links, though. Hard links cant be created for files that reside on a different filesystem/partition. To overcome this, soft links or symbolic links or sym links were introduced ๐Ÿ™‚ See it below ๐Ÿ™‚

[root@server tmp]# echo "Demonstrating links" >original_file.txt
[root@serverย tmp]# ln original_file.txt /home/shashank/hard_link.txt
ln: creating hard link `/home/shashank/hard_link.txt' => `original_file.txt': Invalid cross-device link
[root@serverย tmp]# ln -s /tmp/original_file.txt /home/shashank/soft_link.txt
[root@serverย tmp]# cat /home/shashank/soft_link.txt
Demonstrating links

If you omit the absoluteย path for original file while creating soft links on other partition/filesystem, then you will encounter an error that the soft link was not found at time of accessing that. So, make sure to use absolute path (as shown above).

I hope, this was easy & informative ๐Ÿ™‚

Setup A Fedora (Linux) Virtual Machine Using Oracle VirualBox

What good a Linux tutorial is if it doesn’t tell you how to setup your own server to practice Linux on? ๐Ÿ˜‰ We can’t just keep on reading some random text & become proficient with Linux. We need to have at-least a couple Linux servers to learn & master it.

Why couple? You may ask! Well, its possible to learn most of the Linux stuff on a single, isolated server. But in real world, we deal with a lot many servers that interact with each other. Like DNS, iSCSI, Cluster, SSH, Monitoring(Zabbix & Cacti) & LDAP etc. So its always good to have at-least two Linux servers along-with one Windows server for Samba, AD etc (if your host machine has enough resources). So, below is how to setup your own Linux server(s) using Oracle VirtualBox. I will be using Fedora 22 for this tutorial. I assume you already have downloaded Fedora & installed Oracle VirtualBox. Bothย Oracle VirtualBox & Fedora are completely free to use, so no need to worry ๐Ÿ™‚

Fedora is RedHat’s testing platform with quicker (3-4 months) release cycles to keep pace with the latest technology & innovations. While RedHat Enterprise Linux & CentOS are designed for stability, Fedora is the playground for rapid development.

Lets now begin ๐Ÿ™‚

Building Fedora Virtual server.

Launchย Oracle VirtualBox. Click New button to start creating a new virtual machine.

Setup Fedora Server On VirtualBox (2)

You will be presented with instructions that are self-explanatory. Follow them.

Setup Fedora Server On VirtualBox (3)

If you type in a Name, you will see VirtualBox automatically suggesting OS. Choose accordingly if you use any other flavor.

Setup Fedora Server On VirtualBox (4)

Allocate Memory. I have used 768 MB (default) on my 3GB Laptop. Use sufficient RAM for optimum performance.

Setup Fedora Server On VirtualBox (5)

Allocate Virtual Hard Disk. This will act as your Hard Disk for server. Since its a beginner’s tutorial, I wont explain various disk image types. So, just choose default ones.

Setup Fedora Server On VirtualBox (6)

Setup Fedora Server On VirtualBox (7)

Setup Fedora Server On VirtualBox (8)

Make sure you have enough disk-space on the partition you choose. Choose Location that has sufficient space. It can be any partition. Use proper size for your server if you wish to perform many tasks on it. 15-20 GB is a good choice if you want to setup Yum/NFS server. For plain testing on a single server, 8GB is enough.

Setup Fedora Server On VirtualBox (9)

Click Next & it displays the summary. If you are satisfied, press Create to build disk image. Else, press Cancel to start over.

Setup Fedora Server On VirtualBox (10)

Lets install Fedora server on our Virtual Hard Disk.

Now VirtualBox will list your newly built virtual machine. Select that & click Start.

Setup Fedora Server On VirtualBox (11)

Again, a wizard will open guiding you through Fedora installation process.

Setup Fedora Server On VirtualBox (12)

VERY IMPORTANT STEP. Click browse icon to locate your Fedora ISO image. That image will be used to install Fedora server. If you miss this, your virtual machine will fail to boot.

Setup Fedora Server On VirtualBox (13)

Press Open.

Setup Fedora Server On VirtualBox (14)

You will again see a summary of installation. Click Start.

Setup Fedora Server On VirtualBox (16)

Installation will begin now. You will see three options. Select Install Fedora 22 & press Enter.

Setup Fedora Server On VirtualBox (17)

Setup Fedora Server On VirtualBox (18)

You will now be presented with installation screen which is pretty self-explanatory. Act accordingly. Choose all that you need, like language, network, software.

Setup Fedora Server On VirtualBox (19)

Setup Fedora Server On VirtualBox (20)

Click the Disk that says “ATA VBOX HARDDISK”.

Setup Fedora Server On VirtualBox (21)

Choose from the available options. I chose LVM (more on it later).

Setup Fedora Server On VirtualBox (22)

Now add partitions for your disk. I recommend /, /boot, /swap at minimum.
Setup Fedora Server On VirtualBox (24)

Choose suitable values for your mount-points. Don’t worry if you still don’t understand Linux partitions & mount-points. I will cover all these topics on my blog ๐Ÿ™‚

Setup Fedora Server On VirtualBox (23)

Click Accept Changes.

Setup Fedora Server On VirtualBox (25)

Now press Begin Installation.

Setup Fedora Server On VirtualBox (26)

You will see this progress bar. It will take a while for this installation. In the meantime, set your root password & other users.

Setup Fedora Server On VirtualBox (1)

You can see I have booted onto my Fedora 22 server post its installation.

Setup Fedora Server On VirtualBox (15)

Hope it was easy to understand & informative ๐Ÿ™‚

Creating & Managing File/Directories in Linux

Now that we have basic idea of Linux directory structure, we shall now see how to manage files & directories. One interesting thing with Linux is that it doesn’t care for file extensions ๐Ÿ™‚ Even if you rename a .jpg photo with a text file extention, it will still remain a photo ๐Ÿ˜‰ You will always see the photo if you double-click it.

That being said, lets start with this chapter. Below are the commands along with their description. The syntax goes like

command /path_to_file_or_directory

touch – Creates a new blank file. touch command also changes the time-stamp of existing file.

cat – cat or concatenate reads the files & displays the last page of file, if its a text file.

less – less is used to read text files one page at a time (press enter or up/down keys to navigate) & offers features like finding text inside file.

more – Also used to read files one page at a time but doesn’t allow upward scrolling. It is a less feature-rich version of less.

head – If you want to read the first few lines of a file, then head command is used. You may specify the number of lines to read as its switch e.g. head -50 /path_to_file.

tail – Similar to head, but does just the opposite i.e. reads files from end. You may supply the number of lines as command option/switch, just like head.

vi – vi is one of the most popular & widely used text-file editor on Linux. Its a whole new topic & I will cover it later in some other post (there are commands inside it to edit/save/exit/find/replace etc).

vim – An improved version of vi.

cp – Copies a file or directory. Syntax is : –

cp file_to_be_copied destination_of_copied_file

Say, I need to copy a file “old.txt” to a new file “new.txt” within same directory. Below is how we do it.

cp old.txt new.txt

If you want to copy to some other location, just type full path.

cp old.txt /full_path_to_new.txt

There are several options that we can pass to cp. A few commonly used options are :-

-p – Preservers the ownership of file being copied to new file.
-R – Copies the directory recursively. If there are sub-directories inside a directory, use -R
-v – Verbose. You will see the names of files/directories being copied.

mv – This command is used to move (cut/paste) or rename a file/directory. Issue mv old_filename new_filename to rename it. To cut/paste a file/directory, issue mv filename /path_where_its_to_be_moved

rm – Removes the file/directory. Without any switch passed, rm will only remove a file. Use rmdir command to delete an empty directory. If there are files/sub-directories inside, use rm -r option. It will always ask to confirm if you want to delete or not. Press y to delete, or n to not delete. If you don’t want to be prompted, then issue rm -rf command. -f switch force deletes the files/directories. BE VERY CAREFUL WHILE USING f OPTION. Press enter only when you are fully sure!

mkdir – Creates a new directory. -p is a very useful option if we need sub-directories inside directory being created. ย If the parent directory exists, mkdir -p will ignore it & create the sub-directory. Suppose, I want to create a directory tree

/home/shashank/scripts/automation/daily

I will issue : – mkdir -pย /home/shashank/scripts/automation/daily
& all these directories will be created inside my home-directory. Since/home/shashank/scripts are already present, they will be skipped.

That’s all for this post ๐Ÿ™‚ By the end of this post, I hope you might have got a fairly good basic idea of Linux ๐Ÿ™‚ Do let me know if my posts feel uninformative or dull. See you! ๐Ÿ™‚

Adding & modifying Users in Linux (Red Hat/Fedora & CentOS)

Now that we know what user & group is and how various permissions & ownership work in Linux, we shall now see how a user is created, how a password is assigned to it & how do we modify any user properties ๐Ÿ™‚ This is one of the core Linux Admin tasks & it is always a good idea to have a strong understanding of User Management in Linux ๐Ÿ™‚ So, lets discuss it in detail.

Any user information is stored in a file called /etc/passwd. You will like to view this file to see how it stores this data. Just issue cat /etc/passwd & view this file. You will see the user information line by line. Each line consists of several colon-separated fields. Let me explain these fields. Below is a part from passwd file that contains my information.

shashank:x:501:501:Shashank Shekhar Srivastava:/home/shashank:/bin/bash

The first field shashankย is user-name or user ID with which we logon to Linux system.

Second field x means my encrypted password is stored in /etc/shadow file. Only root can see this file.

Third field 501 refers to my UID. Every user is assigned a unique UID. 0-499 UIDs are reserved for OS/system users.

Fourth field 501 refers to my group ID or GID. Refer to this post to know more about groups. GID is also unique.

Fifth fieldย Shashank Shekhar Srivastava means user comments. Its always a good idea to have some meaningful information in comments section while adding users. I will discuss it in a while.

Sixth fieldย /home/shashank is my home-directory. As we can recall from this post, all users get their home-directories under /home directory with their user-name.

Seventh field /bin/bash is the default shell. Shell is an environment that defines howย users interact with OS. Bash is a very popular Linux shell & is widely used for a number of reasons. It offers a lot of useful built-in functions such as script(record your shell session), supports programming loops such as for/while, remembers commands history, easy navigation & much more. Its a whole new topic & I can start a whole new blog on it ๐Ÿ˜‰ Its VAST! ๐Ÿ™‚ I will not focus much on it here.

One more file is left. /etc/group file stores all the information related to group membership.

Lets now see how a user is added to a Linux system.

useradd command is used to add user. I am bisecting the different options that are passed to useradd command. Default syntax is useradd user_name. It will create a new user with the name as user_name, home_directory as /home/user_name, shell as bash(depends on Linux distribution) & use the next available UID/GID for user_name (UID/GID is unique). But we can pass a few more options to it, if we want to customize thisย part. Below is the detailed useradd command.

useradd -d /home-directories/user_name -s /bin/ksh -c "user_name, mobile no. 1234567890" -G dba,developer,admin -U 1001 user_name.

-d specifies the home-directory. If it’s not present, it will be created.

-s specifies the shell.

-c is for adding comments. Useful for tracking users & finding their details.

-G is for adding the user to multiple secondary groups.

-U is to specify a user ID

user_name specifies the name for newly created user.

So, I hope you now have a better idea of the various options that exist while adding users ๐Ÿ™‚ Though, most of the time, default is more than sufficient ๐Ÿ˜‰

Now let’s discuss how to modify a user.

usermod command is used to modify a user in Linux. Almost all the options that are passed to useradd command can be passed to usermod as well. If you want to change shell, just pass -s /path_to_new_shell. If you want to edit comments, ย pass -c "new comments". If you want new home-directory, pass -d /path_to_new_directory.

That’s all for this post ๐Ÿ™‚ I am sure it was easy for you to understand & execute. I hope, I was able to explain it in an easy way ๐Ÿ™‚ Please, do let me know if I missed anything here or if it was not written in easy language. See you ๐Ÿ™‚

More About chmod & chown Commands

In previous chapter, I explained about permissions & ownership of files/directories & users in Linux. In this post, we will see how these commands (chmod/chown) work. ๐Ÿ™‚ Lets start this chapter.

chmod command is used to alter file/directory permissions. As with any Linux command, there are a few options that are used with chmod. One widely used option is -R, which stands for recursive. It means, the permissions are applied to all the files & sub-directories. -R is a universal switch for all Linux commands & means the same for all those commands. Lets take a look at one example.

[shashank@server ~]$ chmod -R 775 /home/shashank/scripts

This will make all the files & sub-directories read/write/executable insideย /home/shashank/scripts for owner & group members. All other users will have only read/write access.

chown command is used to change ownership of files/directories. If we need to change ownership of all sub-directories, then we will include -R option. Lets se an example.

chown -R shashank:shashank /home/shashank/scripts

This will change the ownership of everything that is under /home/shashank/scripts to shashank user & make it member of shashank group. If we just need to change the owner, then use

chown -R user_name /path_to_directory

Remember that only root can change ownership & permissions for all users & files/directories. Other users can only change their own permissions but not ownership. Only root can change that.

That’s all for this post. Last post was quite long & explanatory so this chapter should not get too boring for you ๐Ÿ˜‰ You now know how these commands work ๐Ÿ™‚

Users, Groups, Permissions & Ownership in Linux

In my previous post, I explained the directory structure in Linux. I hope that information was useful to you ๐Ÿ™‚ Now, I will be explaining basic Linux security principles in this post. Linux principles include users, their groups, file/directory permissions & their ownership. In this post, I will explain these things in detail.

A user means a person who logs in to Linux machine. We need a user ID to be created before we can log-in. A few users do exist in Linux environment that don’t actually log-in, but are created for applications like Apache(http user) & Oracle(oracle). Root user is the super-user. Root has full, unrestricted access to ALL the files & directories. There are a few admin tasks, that only root can perform. Such as software installation, application configuration & the like. All other users can be promoted to root user through means of sudo access (/etc/sudoers file or /etc/profile.d/). I will come to it later on in a new post. Lets see how a user is added to Linux. Issue below commands as root –

[root@server ~]# useradd shashank
[root@server ~]# passwd shashank
Changing password for user shashank.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

A user is only allowed to log in if it has its password set. Without password, no user can log in.

Below command shows basic user information like UID(user ID), GID(group ID) & group.

[root@server ~]# id shashank
uid=512(shashank) gid=514(shashank) groups=514(shashank)

A user can have a primary group & multiple secondary groups. Every user that we add becomes a part of its own primary group. Like above, this newly added shashank user becomes a member of its own primary group shashank. If you can recall from my previous post, all users ย have their own home-directories inside /home.

Now, lets see what does we have in shashank’s home-directory? ๐Ÿ™‚ Issue below commands to find out. ll stands for long listing and is an alias to command ls -l.

[root@server ~]# ll /home/shashank
total 0
[root@server ~]# ls -ld /home/shashank
drwx------. 4 shashank shashank 4096 Jul 11 01:24 /home/shashank

ll is basically used to list down information of files/directories. It returns 0 since there is nothing inside my home-directory. ls -ld shows complete information like permissions, ownership, group, size, last modified date & path(in the same order). Permissions are denoted by a 10 hyphens notation. We divide it in a group of 3×3 & an additional hyphen.

The 1st one denotes the file type. Every thing in Linux is a file ๐Ÿ™‚ Even a directory is a file ๐Ÿ˜‰ So, the file-type here is a ‘d’ or directory. Now comesย rwx, that stands for Read, Write, eXecute permissions for file/directory owner. Read permission for a directory means to be able to move to a directory. Read permission for a file means, obviously you know it ๐Ÿ˜‰ Write permission of directory means to be able to rename/create new files/directories under parent one. For file, you know what it means ๐Ÿ˜‰ Execute permissions are interesting ones ๐Ÿ™‚ For directory, it means to be able to list down the contents of it. For file it means to be able to execute it. Like a shell script or binary. I will discuss it below. In Linux, RWX is denoted by digit 7(4+2+1). 4 is for R, 2 for W & 1 for X. Now you will think why 4+2+1? ๐Ÿ˜‰ Well, there’s some maths involved ๐Ÿ™‚ 4 is 100 in binary format. So, it means that R permission is ON & W/X are OFF. 2 is 010. Means W is ON & R/X are not. 1 is 001 which means X is ON ๐Ÿ™‚

Lets analyse it once again. shashank is owner of home-directoryย /home/shashank. Next 3 hyphens are for group & last 3 are for others or anyone who is not owner & part of this group. shashank has full RWX permissions. Members of shashank group & others don’t have any permission here.

drwx------. 4 shashank shashank 4096 Jul 11 01:24 /home/shashank

Note – Root can switch to any other user without entering password. Also, only root can change password for other users. A user can only change her/his password.

Lets switch to shashank user by issuing su command.

[root@server ~]# su - shashank
[shashank@server ~]$ ll
total 0

Since there was no any file there, I will create a new file. Vi is an advanced editor for Linux & is a whole new experience to master it ๐Ÿ™‚ Its not something like Notepad/Wordpad ๐Ÿ˜‰

[shashank@server ~]$ vi test_file

Vi opens a full screen window where we enter the contents of file. Let’s see what I wrote in this ๐Ÿ™‚

[shashank@server ~]$ cat test_file
Hi..This is a test file for user shashank. Only shashank & root can view/edit this file.

So, lets test what I wrote in test_file. I exit from shashank & switched to another normal user.

[shashank@server ~]$ exit
logout

[root@server ~]# su - test_usr
[test_usr@server ~]$ cd /home/shashank/
-bash: cd: /home/shashank/: Permission denied
[test_usr@server ~]$ vi /home/shashank/test_file
"/home/shashank/test_file"
"/home/shashank/test_file" E212: Can't open file for writing
Press ENTER or type command to continue

So, you can see that test_user doesn’t have any right to view or edit shashank’s file.

[test_usr@server ~]$ exit
logout

Lets go back to my home-directory.

[root@server ~]# cd /home/shashank/
[root@server shashank]# ll
total 4
-rw-rw-r--. 1 shashank shashank 89 Jul 11 01:27 test_file
[root@server shashank]# ls -lh
total 4.0K
-rw-rw-r--. 1 shashank shashank 89 Jul 11 01:27 test_file
[root@server shashank]# vi test_file
[root@server shashank]# cat test_file
Hi..This is a test file for user shashank. Only shashank & root can view/edit this file.
Root can modify this file. Root is god :D

Root has full access to everything so root is able to see & edit this file ๐Ÿ™‚ Lets see how execute permissions for files work. They are used to execute shell scripts & binaries. I am demonstrating a simple script ๐Ÿ™‚ This script shows the info about server uptime & logged-in users.

[root@server shashank]# su - shashank
[shashank@server ~]$ vi test_script.sh
[shashank@server ~]$ ./test_script.sh
-bash: ./test_script.sh: Permission denied

Permission was denied to execute script. Why? ๐Ÿ˜ฆ Lets see the permissions.

[shashank@server ~]$ ll test_script.sh
-rw-rw-r--. 1 shashank shashank 9 Jul 11 01:43 test_script.sh

As you can see, even owner doesn’t have execute permission. But sh or shell command can be used to execute shell scripts.

[shashank@server ~]$ sh test_script.sh
01:44:11 up 35 min, 4 users, load average: 0.00, 0.00, 0.00
01:44:12 up 35 min, 4 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 01:12 35:15 5.03s 5.03s /usr/bin/Xorg :
root pts/0 :0.0 01:13 30:45 0.02s 0.02s /bin/bash
root pts/1 192.168.0.126 01:13 1.00s 0.06s 0.00s script users_gr
root pts/2 - 01:23 1.00s 0.27s 0.00s sh test_script.

Lets change the permissions of our scripts. Remember, root can change permission of any file/directory. Owner can change only hers/his. chmod command is used for this.

[shashank@server ~]$ chmod 775 test_script.sh
775 means 4+2+1/4+2+1/4+1 for owner/group/others. Lets check it now ๐Ÿ™‚

[shashank@server ~]$ ll test_script.sh
-rwxrwxr-x. 1 shashank shashank 9 Jul 11 01:43 test_script.sh

Pretty self-explanatory, right? ๐Ÿ™‚

[shashank@server ~]$ ./test_script.sh
01:45:11 up 36 min, 4 users, load average: 0.07, 0.03, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 01:12 36:14 5.03s 5.03s /usr/bin/Xorg :
root pts/0 :0.0 01:13 31:44 0.02s 0.02s /bin/bash
root pts/1 192.168.0.126 01:13 0.00s 0.06s 0.00s script users_gr
root pts/2 - 01:23 0.00s 0.10s 0.00s -bash

See, its now executing fine ๐Ÿ™‚ Lets see how ownership works. I am trying to change ownership from my ID to other ID. chown command is used to change ownership.

[shashank@server ~]$ chown test_usr test_script.sh
chown: changing ownership of `test_script.sh': Operation not permitted

It failed. Only root can change it ๐Ÿ™‚ Lets try with root.

[shashank@server ~]$ exit
logout
[root@server shashank]# chown test_usr /home/shashank/test_script.sh
[root@server shashank]# ll /home/shashank/test_script.sh
-rwxrwxr-x. 1 test_usr shashank 9 Jul 11 01:43 /home/shashank/test_script.sh
[root@server shashank]# su - shashank
[shashank@server ~]$ ./test_script.sh
01:48:00 up 39 min, 4 users, load average: 0.00, 0.01, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 01:12 39:03 5.06s 5.06s /usr/bin/Xorg :
root pts/0 :0.0 01:13 34:33 0.02s 0.02s /bin/bash
root pts/1 192.168.0.126 01:13 0.00s 0.06s 0.00s script users_gr
root pts/2 - 01:23 0.00s 0.08s 0.00s -bash
[shashank@server ~]$ exit
logout

[root@server shashank]# su - test_usr
[test_usr@server ~]$ ./home/shashank/test_script.sh
-bash: ./home/shashank/test_script.sh: No such file or directory
[test_usr@server ~]$ ll ./home/shashank/test_script.sh
ls: cannot access ./home/shashank/test_script.sh: No such file or directory
[test_usr@server ~]$ exit
logout

Above outputs suggest that despite changing ownership of my script to other ID, only I can execute it. Because only I & root have permissions to go to my home-directory & perform something there. test_usr, despite being owner of script file, doesn’t have rights to see my directory.

Lets discuss primary & secondary groups now. I am adding test_usr to shashank group as a secondary group. usermod command changes the user attributes like GID, groups, shell or home-directory. -a switch appends the group (adds another group to user) & -G specifies secondary group.

[root@server shashank]# id shashank
uid=512(shashank) gid=514(shashank) groups=514(shashank)

[root@server shashank]# usermod -aG shashank test_usr
[root@server shashank]# id test_usr
uid=504(test_usr) gid=504(test_usr) groups=504(test_usr),514(shashank)

[root@server shashank]# ls -ld /home/shashank
drwx------. 4 shashank shashank 4096 Jul 11 01:43 /home/shashank

[root@server shashank]# su - test_usr
[test_usr@server ~]$ ll ./home/shashank/test_script.sh
ls: cannot access ./home/shashank/test_script.sh: No such file or directory
[test_usr@server ~]$ ll /home/shashank/test_script.sh
ls: cannot access /home/shashank/test_script.sh: Permission denied
[test_usr@server ~]$ ./home/shashank/test_script.sh
-bash: ./home/shashank/test_script.sh: No such file or directory
[test_usr@server ~]$ exit
logout

test_usr can’t do anything to my files since only the owner, that’s me, has full access. Inspite of being a member of shashank group, test_usr still has no rights over my files. Lets change the ownership back to me.

[root@server shashank]# chown shashank /home/shashank/test_script.sh
[root@server shashank]# ll /home/shashank/test_script.sh
-rwxrwxr-x. 1 shashank shashank 9 Jul 11 01:43 /home/shashank/test_script.sh
[root@server shashank]# exit

That’s all for this post ๐Ÿ™‚ Hope I was able to explain it in an easy manner. Do let me know if the post was informative & easy or not ๐Ÿ™‚ In next post, I will explain the commands used in this post in detail. See you ๐Ÿ™‚

Directory Structure in Linux

If you are from Windows background, you will find Linux directory structure to be entirely different. There won’t be Documents & Settings, Program Files, Users, Windows folders at all. Linux has a very different approach towards directories/folders. In fact, every single thing on a Linux system is a file! Yes, even a directory or device is a file here ๐Ÿ˜‰ This post explains various directories that Linux is made up of. Lets begin this chapter ๐Ÿ™‚

Every directory in Linux begins with / or root directory. Any directory you refer to is relative to /. There are a few predefined directories that you will find in a Linux system. I am not covering each & every directories here. I am just mentioning the ones that a beginner must be aware of. Here they are ๐Ÿ™‚

/etc – All configuration files are placed in /etc. Almost every setting that needs to be changed, can be done by editing plain-text configuration files. Be it Apache web-server or Postfix configuration, you will find all configuration files under this directory. A very typical file that you will need to modify quite often is hosts file which contains hostname-to-IP address resolutions & vice-versa. This file is inside /etc. So, any kind of conf file will be placed here.

/var – var means variable. Means, all the files that grow/shrink are placed under /var. A very typical example is log files. They fill up & then nullified. So, log files are kept inside /var/log directory. System logs are contained in /var/log/messages file.

/bin – bin stands for binaries. It means all the commands (binaries) are placed inside /bin. Commands placed here are available for ALL users, like ping, grep, uptime etc. These are commands that can be used to operate Linux in non-GUI mode. Commands are nothing but executable binaries.

/sbin – This directory holds all the commands for System Admin or root user. Commands like reboot, fdisk (format disk), shutdown are meant for root user only & hence, are placed here. root user has all the rights & permissions to do ANYTHING on Linux system. Yes, it involves crashing down the whole system, without even a warning ๐Ÿ˜‰

/home – Your play-ground ๐Ÿ˜‰ This is the place where user home-directories are stored. This is where all your files & directories are present. This is where you have full access to do anything. Like, reading, writing files/directories. Analogous to Users folder in Windows.

/dev – dev stands for devices. All your hardware devices are placed here like Disk, CPU, RAM, USB etc. Linux treats devices as files ๐Ÿ˜‰

/proc – The most interesting directory out here ๐Ÿ™‚ proc stands for process. This is a directory that is not created/doesn’t reside on storage device like Disk. Its rather created in RAM ๐Ÿ™‚ It contains run-time information about CPU, RAM, Swap space & processes. That’s why it is known as virtual file-system. It never occupies any space on storage device.

/root – The home-directory of root. Don’t confuse with /. /root is under / ๐Ÿ˜‰

/opt – opt stands for optional. It means all optional software, that is not critical to running a system, is installed here. For example Java, Veritas NetBackup, Veritas Cluster Server.

/lib – Library directory contains all the shared libraries that various binaries need for their execution.

/usr – User directory is used to store user applications, documentations & source-codes. This is where a user(typically a developer) places her/his developed application & related stuff. Its not mandatory, though. This is also a place where all non-system, non 3rd-party applications are found. For example all applications that are installed using source-code are placed in /usr/local directory. Think of it as a place, where non-critical applications like Python, Cacti (a Linux server monitoring tool) etc are installed.

/mntย – All temporary file-systems/directories/devices are mounted here. Mounting means attachingย file-systems/directories/devices to a location under root, known as mount-point. You can mount a USB stick or a DVD to /mnt temporarily.

Introduction to Linux

The most common misconception about Linux is that people think of it as an OS. But Linux is not an OS, but a kernel. Yes, you read that right! I will not cover the entire history behind its evolution, since it will take a lot of pages to do it. Best resource will be referring to its Wikipedia page.

Linux is a Free & Open Source kernel, which means that any one can view & change its contents. Its distributed under GNU Public License version 2. Linux is distributed in the form of Linux distributions which includes utilities(apps), drivers, application software & Linux kernel. Since its Open Source, many organizations have taken the source code & distributed under their name after modifying it. Most notable of these are Debian, Ubuntu, Linux Mint, SUSE, CentOS & Red Hat Enterprise Linux or RHEL. RHEL is a commercial product which means that they charge for their product. The OS is free to use but customers pay for the support & updates from Red Hat. CentOS is a totally free Red Hat based OS which is powered by support from Linux community.

Linux derives its design principle from Unix & hence, is a part of Unix like (*nix) OS’s. Another common misconception about Linux is that it’s totally command-line interface. But it’s not like that. Linux is backed up by a number of Desktop Environments like KDE & GNOME. A typical Linux installation has a full fledged GUI. Yes, we admins choose CUI over GUI for a number of reasons. Its much faster & convenient typing a few commands to get the work done, rather than clicking a few buttons ๐Ÿ˜‰

Architecture-wise Linux is totally different from Windows OS & I will be covering it in subsequent posts ๐Ÿ™‚ Till, then you must have got an idea about Linux ๐Ÿ˜‰