Category Archives: linux

Meanings of Access,Modify and Change in stat command

I keep forgetting the “subtle” differences between access,modify and change attributes of an inode. I will try to take some note here for you and me. My file test.txt has got the text “abc” in it which is 3 bytes + 1 new line = 4 bytes and I pasted inode information below via stat command.

Continue reading

Increasing the number of inodes

If you are keeping huge number of small files in your disks, this may one day cause running out of available inodes because of which even though you might have sufficient disk space, file system cannot create new files. Lets see how it works in my 1GB disk partition.

The parameter “-i” allows us to play with the number of inodes we can have in a partition. It is called “bytes-per-inode”. The bigger it is less number of inodes you will have. I have set a relatively big size “163840” bytes per inode and the result is I have 6768 inodes. What this means indeed is I can’t create more than 6768 files. (even a bit less than this)

If you don’t believe me, lets do a test. I will mount this partition and create this number of files;

Continue reading

Linux file attributes

Have you ever received such an error when you are logged in as root? This error surprised me as I asked myself: Hang on a second! I am the super user, I should be able to change the permission of this regular file.

Look what prevents me from doing this;

Culprit is the file attribute “a” which means append. With “644” mode, I was trying to remove the write permission from the file but kernel didn’t let me do it because of this bit on. I would appreciate a cleaner error message indeed:) Once I clear the append flag, I successfully changed the permission.

There are a couple of more flags I like;

  • A : atime record of the file isn’t modified if set
  • S : changes are written synchronously on the disk
  • i : My favorite, the immutable bit. If set none can modify the file even can create a link

Moving up levels in directory tree easily

In Linux if you are in a directory level and want to jump for example 2-3 folders in one step, as far as I know there is no built-in command. If there is one, I would be very happy to know. There is a very nice command in Juniper’s JUNOS CLI which is “up” which moves you up in the configuration hierarchy. I wanted to do the same via a bash script like below;

After giving execution permission to the file, I put it under /bin folder BUT the point is I can’t simply run it like;

and expect to move 4 directory levels up. I must run it inside the current shell I am in without forking any other process and here is the handy command “.” comes in. If I am in the following directory for example;

running the command the following way;

will move us up to the /usr/libexec/gcc folder. The argument 2 here stands for in which directory we want to be above our working directory.

Although there may be a built-in bash command which already does this, I think it is good to see how “.” command can be this handy!

Hard link and soft link?

I would like to give some examples to describe practical meaning of hard links,soft links and cache in memory in a linux system. I will start creating two ramdisk partitions:

Now lets look at the free memory usage of my PC;

According to this output, my cache data is 658MB. As you might know, cached amount isn’t actually used by processes actively but kernel keeps them in memory as they might be referenced. Instead of keeping them in the secondary storage (e.g hard disk), as long as memory is available, kernel caches them in the memory. Now we will increase this number sharply.

Continue reading

How to use ulimit utility to restrict users?

You can use ulimit command to prevent users from abusing system resources. Below I will touch two of the options;

Continue reading

Reverse SSH connection

Imagine that you have a host (named A) somewhere that has no publicly reachable IP address but you would like to connect it via SSH. If this host A can reach outside, then you can reverse SSH into it. Here is the scenario;

Host A (192.168.1.1) —  INTERNET —- Host C (10.10.10.1)

On host A, run the following command;

Once you authenticate yourself, this opens a tunnel from Host C to Host A but
from port 2025 to 22. If you want to connect from Host C to Host B back you can simply run;

Can you see that as if you are connecting to localhost? but indeed connection is tunneled towards the Host A