Ubuntu

Popular Linux Flavors

Linux Distributions

Popular Linux Flavors

RedHat

Popular Linux Flavors

Open SUSE

Popular Linux Flavors

Fedora

Popular Linux Flavors

Knowledge Sharing

All about Linux and much more...

Monday, May 20, 2013

How to create Soft Link and Hard Link in Linux

Well before moving ahead I hope you know about Symlinks in Linux and its both the types i.e Soft Link and Hard Link. I will just give a brief description on both the types of link.

Soft Link
  1. Using this only a link to the original file is created (shortcut).
  2. The size of created shortcut is null.
  3. If you delete the file then the created link (shortcut) won't work.
  4. In case you delete the shortcut link then it won't affect the original file

Hard Link
  1. Another copy of the file is created.
  2. Both the file have same inode no.
  3. Any changes made in either of the file will appear on the other file.
  4. Deleting any of the one file won't affect the other file.

Creating Soft Link

The syntax to be followed for creating soft links

 # ln -s /path/to/source    /path/to/destination

NOTE: You will have to give the complete path of source and destination file unless they both have to exist in the same directory.


 # echo 12345 > ~/myfile.txt


Now we will create a soft link of this file in some other location

 # ln -s ~/myfile.txt /tmp/

 # cd /tmp
 # ls -l
 lrwxrwxrwx 1 root   root       16 May 20 07:26 myfile.txt -> /root/myfile.txt

Now as you see a symlink has been created which is shown by "Blue colour". 

NOTE: In case if you see a red color symlink instead of blue then it means either the symlink is not created properly or the original file has been moved or deleted.


  • Now let us check the difference between both the files.

 # stat ~/myfile.txt

   File: `myfile.txt'
   Size: 6               Blocks: 8          IO Block: 4096   regular file
 Device: fd00h/64768d    Inode: 20152421    Links: 1
 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
 Access: 2013-05-20 07:24:13.000000000 +0530
 Modify: 2013-05-20 07:24:12.000000000 +0530
 Change: 2013-05-20 07:24:12.000000000 +0530



 # stat /tmp/myfile.txt

   File: `/tmp/myfile.txt' -> `/root/myfile.txt'
   Size: 16              Blocks: 0          IO Block: 4096   symbolic link
 Device: fd00h/64768d    Inode: 22479090    Links: 1
 Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
 Access: 2013-05-20 07:26:06.000000000 +0530
 Modify: 2013-05-20 07:26:03.000000000 +0530
 Change: 2013-05-20 07:26:03.000000000 +0530

So both the inode no. seems to be different.


  • Let us check the size of both the files

 # du -sch ~/myfile.txt
 4.0K    /root/myfile.txt
 4.0K    total



 # du -sch /tmp/myfile.txt
 0       /tmp/myfile.txt
 0       total

So, we can conclude that all the above mentioned points under soft link are true.

Creating Hard Link

The syntax to be followed for creating soft links

 # ln  /path/to/source   /path/to/destination
 # ls -l ~
 -rw-r--r--  2 root root         6 May 20 07:24 myfile.txt
 # ln ~/myfile.txt /tmp/



 # cd /tmp

 # ls -l
 -rw-r--r-- 2 root   root        6 May 20 07:24 myfile.txt

So as we see there is no difference between both the files and hard to find out which one is the original file and which one is the created hard link.


  • Lets check the size of both the file

 # du -sch /tmp/myfile.txt
 4.0K    myfile.txt
 4.0K    total



 # du -sch ~/myfile.txt
 4.0K    /root/myfile.txt
 4.0K    total

So it seems both the file is occupying the same size on the disk.


  • Let us try making some changes in any one of the file

 # echo 123 >> ~/myfile.txt

 # cat /tmp/myfile.txt
 12345
 123

So the changes made in one file are reflected in another file as you can see above. I appended 1 line in the original file inside root and the same changes are reflected on the other file.


  • Let us check the inode number of both the file

 # stat ~/myfile.txt

   File: `/root/myfile.txt'
   Size: 10              Blocks: 8          IO Block: 4096   regular file
 Device: fd00h/64768d    Inode: 20152421    Links: 2
 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
 Access: 2013-05-20 07:50:32.000000000 +0530
 Modify: 2013-05-20 07:50:28.000000000 +0530
 Change: 2013-05-20 07:50:28.000000000 +0530



 # stat /tmp/myfile.txt

   File: `/tmp/myfile.txt'
   Size: 10              Blocks: 8          IO Block: 4096   regular file
 Device: fd00h/64768d    Inode: 20152421    Links: 2
 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
 Access: 2013-05-20 07:50:32.000000000 +0530
 Modify: 2013-05-20 07:50:28.000000000 +0530
 Change: 2013-05-20 07:50:28.000000000 +0530

So as you see both use the same inode no. due to which any change made to 1 file is reflected on the other file.

I hope I cleared all your doubt in case still you have any question feel free to comment for any query.





Sunday, May 19, 2013

How to create a MySQL database USER and ASSIGN PRIVILEGE

Creating a database user and system user are two different things. In this post I will be telling you the steps required to create a MySQL database user and assigning privilege.

For installing and creating database please follow this link

Login to your database

 # mysql -u root -p
 Enter password: 
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1112
 Server version: 5.0.77 Source distribution

 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 mysql> create user deepak@'localhost' identified by 'my_password';
 Query OK, 0 rows affected (0.00 sec)

 mysql> GRANT ALL PRIVILEGES on test.* to deepak@'%';
 Query OK, 0 rows affected (0.00 sec)

 NOTE: The host definition of '%' or '' (null) refers to any host.

 mysql> FLUSH PRIVILEGES;
 Query OK, 0 rows affected (0.00 sec)



 # mysql -u deepak -p

 Enter password:
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1111
 Server version: 5.0.77 Source distribution

 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 mysql> show databases;
 +--------------------+
 | Database           | 
 +--------------------+
 | information_schema |
 | mycomp               |
 +--------------------+
 2 rows in set (0.00 sec)

 mysql> use mycomp;
  
 Database changed
 mysql> INSERT INTO employee VALUES ('3','Anita Chaudhary','22');
 Query OK, 1 row affected (0.00 sec)

 mysql> SELECT * from employee;
 +----------+-----------------+------+
 | SerialNo | Name            | Age  |
 +----------+-----------------+------+
 | 1        | Deepak Prasad   | 23   |
 | 2        | Amit Dubey      | 24   |
 | 3        | Anita Chaudhary | 22   |
 +----------+-----------------+------+
 3 rows in set (0.00 sec)




How to create MySQL Database, Tables in Linux

I have used Red Hat Linux for creating MySQL database so kindly make changes in the commands as
per your distribution.

Installing MySQL.

Make sure mysql package is installed in your machine which you can check using

 # rpm -qa | grep mysql


If nothing comes up you can manually install using

 # yum install mysql


Now once the package is installed by default
Admin user : root
Password : blank

You will have to assign a password to root

 # mysqladmin -u root password 'new-password'

OR

 # mysql -u root -p

 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1108
 Server version: 5.0.77 Source distribution

 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 mysql> USE mysql;
 mysql> UPDATE user SET Password=PASSWORD('new-password') WHERE user='root';
 mysql> FLUSH PRIVILEGES;

Creating database, tables and inserting values into the tables

Login to mysql

 # mysql -u root -p
 Enter Pasword:
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1108
 Server version: 5.0.77 Source distribution

 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 mysql> show databases;           -- "Will list all the databases"

 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 +--------------------+

 mysql> create database mycomp;
 mysql> show databases;

 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 | mycomp             |
 +--------------------+
 mysql> use mycomp;          --"Will connect to specified database"
 Database changed
 mysql> create table employee (SerialNo varchar(3),Name char(20),Age varchar(2));
 Query OK, 0 rows affected (0.01 sec)

 mysql> describe employee;
 +----------+------------+------+-----+---------+-------+
 | Field    | Type       | Null | Key | Default | Extra |
 +----------+------------+------+-----+---------+-------+
 | SerialNo | varchar(3) | YES  |     | NULL    |       |
 | Name     | char(20)   | YES  |     | NULL    |       |
 | Age      | varchar(2) | YES  |     | NULL    |       |
 +----------+------------+------+-----+---------+-------+
 3 rows in set (0.00 sec)

 mysql> show tables;          --To list out available tables in the selected database
 +----------------+
 | Tables_in_test |
 +----------------+
 | employee       |
 +----------------+
 1 row in set (0.00 sec)

 mysql> INSERT INTO employee VALUES ('1','Deepak Prasad','23');
 Query OK, 1 row affected (0.00 sec)

 mysql> INSERT INTO employee VALUES ('2','Amit Dubey','24');
 Query OK, 1 row affected (0.00 sec)

 mysql> SELECT * from employee;
 +----------+---------------+------+
 | SerialNo | Name          | Age  |
 +----------+---------------+------+
 | 1        | Deepak Prasad | 23   |
 | 2        | Amit Dubey    | 24   |
 +----------+---------------+------+
 2 rows in set (0.00 sec)

 mysql> quit
 Bye


Restarting mysql database

 # /etc/init.d/mysqld restart

OR

 # service mysqld restart


Make sure your database is up even after restart

 # chkconfig mysqld on




Friday, May 17, 2013

/www/bin/apxs: No such file or directory

The other day I was trying to compile php in one of my Linux machine and this error threw up. Well then I thought let me provide a solution for this on my blog as well.

This error comes when you are trying to compile php with Apache.

 # ./configure --with-apxs=/www/bin/apxs


Well I was following the INSTALL directory present inside the downloaded php directory which told me to compile using the above syntax if I wanted to integrate my php with Apache. Initially I didn't recognized that the path I was using for apxs was not correct but anyhow we will come to know about it once we run the command.

Solution

This happens because of the following missing package
  1. httpd
  2. httpd-devel (important)
In most cases httpd-devel package is missing which leads to this error.

On Red Hat, Fedora, CentOS

 # yum install httpd httpd-devel


Once the package is installed look out for apxs in your machine

 # which apxs
 /usr/sbin/apxs



So as you see the location of apxs is different as we were using in our command. Now try to compile your PHP

 # ./configure --with-apxs=/www/bin/apxs


NOTE: If your Apache version is higher than 1.3 then you will have to use apxs2 instaed of apxs.

 # ./configure --with-apxs2=/www/bin/apxs




Tuesday, May 07, 2013

Preventing security breaches with ExecShield

ExecShield is designed to prevent security breaches caused by software programs written to crawl through the Internet looking for systems with common vulnerabilities such as worms and viruses. It is enabled in the kernel and works in a way that is non intrusive to the user. Its goal is not to defend against the expert hacker who has broken into your local network or an employee inside the company who already has access to parts of the network. Its goal is to prevent against intruders using scripts that look for vulnerabilities in the way a program running with root privileges is written.

How ExecShield works

One of the ExecShield memory-management techniques is using random memory locations each time a program is started. Using random memory locations prevents worms or viruses from knowing which parts of memory to overwrite with executables that breach the security of the system. 

For example, if the same memory location is used by a program every time the program is run, a hacker can write a virus that waits until the program has written to memory and then overwrites that part of the memory. When the program goes to execute the instructions in memory at a later time, the virus has already overwritten it, and the code from the virus is executed instead. The virus code is executed with whatever permissions the program has. If the program is being run as the root user, significant damage can be done to the system or confidential data stored on the system can be sent to another computer over the network on the Internet.

ExecShield combats this with two features:
. Ascii Zone
. Address Space Randomization

Determinig status of ExecShield

ExecShield is enabled by default in the Red Hat Enterprise Linux kernel. To verify that ExecShield is enabled, execute the following command:

 # cat /proc/sys/kernel/exec-shield


If it returns the value of 1, ExecShield is enabled. The value of 0 means it is disabled. You can also determine the status of ExecShield by executing the following command, but this command must be run as the root user:

 # sysctl -a | grep exec-shield


If it returns the following, ExecShield is enabled:

 kernel.exec-shield = 1

Again, a value of 0 indicates that ExecShield is disabled.

Disabling ExecShield

ExecShield can be disabled by using sysctl or modifying the boot loader configuration file to set the exec-shield kernel parameter to 0.

To disable ExecShield using sysctl, execute the following command:

 # sysctl -w kernel.exec-shield=0


ExecShield is disabled immediately. However, executing this command alone does not disable ExecShield on subsequent reboots. To disable ExecShield for all reboots, add the following line to /etc/sysctl.conf (as root):

 kernel.exec-shield = 0


Changes made to this file are not enabled until a reboot occurs, because the file is only read once during system startup. To enable the change immediately, the sysctl -w kernel.exec-shield=0 command still needs to be executed.

Another way to disable ExecShield at boot time is to add a boot parameter and value to the boot loader configuration file. For x86 and x86_64 systems that use GRUB as the boot loader, append the following line to the kernel line in /etc/grub.conf (as root):

 exec-shield=0


NOTE: If the same boot parameter is set in /etc/grub.conf and in /etc/sysctl.conf, the value from sysctl.conf takes precedence. If you add boot parameters to the GRUB configuration file, make sure there aren’t any conflicting settings in /etc/sysctl.conf.



Configuring CVS server in Linux

From wiki CVS (Concurrent Versions System)uses a client–server architecture: a server stores the
current version(s) of a project and its history, and clients connect to the server in order to "check out" a complete copy of the project, work on this copy and then later "check in" their changes. Typically, the client and server connect over a LAN or over the Internet, but client and server may both run on the same machine if CVS has the task of keeping track of the version history of a project with only local developers.

The following configuration has been tested on Red Hat Linux 5.x so if you are using different distribution then make sure you use relative command.

How to configure CVS server?

Make sure cvs package is installed in your machine.

 # yum install cvs


Now the next thing which you have to do is create a project repository for your projects.

 # mkdir /usr/local/myprojects
 # cvs -d /usr/local/myprojects init

The above command will create a new project repository with the name of "myprojects"

Now let us make some initial changes before starting the cvs server. Make sure your cvs file looks like as shown below.

 # vi /etc/xinetd.d/cvs
 {
         disable                 = no
         port                    = 2401
         socket_type             = stream
         protocol                = tcp
         wait                    = no
         user                    = root
         passenv                 = PATH
         server                  = /usr/bin/cvs
         env                     = HOME=/var/cvs
         server_args             = -f --allow-root=/usr/local/myprojects pserver
         bind                    = 192.168.0.138
 }

Here 192.168.0.138 is the IP of my local machine where I am configuring my cvs server.

Add a new line in the last line

 # vi /etc/services
 cvspserver      2401/tcp                # CVS PServer


Restart the xinetd services

 # service xinetd restart
 Stopping xinetd:                                           [  OK  ] 
 Starting xinetd:                                           [  OK  ]


Verify that the service is running and listening

 # netstat -ntlp | grep 2401
 tcp     0   0   192.168.0.138:2401      0.0.0.0:*   LISTEN      18337/xinetd


Now create a cvs user and assign any password for the project repository we created

 # useradd deepak
 # passwd deepak


Client login into CVS server


Syntax:

 cvs -d :pserver:user_name@server_name:/usr/local/myprojects login



 # cvs -d :pserver:deepak@192.168.0.138:/usr/local/myprojects login


Another way to login

 # export CVSROOT=:pserver:deepak@192.168.0.138:/usr/local/myprojects

 # cvs login
 Logging in to :pserver:deepak@192.168.0.138:/usr/local/myprojects
 CVS password: