Tuesday, January 20, 2015

How to add user in Linux



This is a small “how-to” on adding users in Linux. As Linux is multi-user supporting operating system so that multiple users can login symeltiniously login and do their job. Linux administrators some time require to create user accounts for a project so that the users can login to the machine and work on it. This can be accomplished by executing adduser linux command from terminal. This command will create user accounts with normal privileged access and with default settings such as home directories, type of the shell, UID and GID as well. Only root user can execute adduser command. If a normal user want to create accounts in the system he should be granted access by root user for sudo access for adduser command.

Step1: Add user account with following command

adduser user-name
or
useradd user-name

Example:

adduser rajesh
or
useradd rajesh

Note: There is one more command useradd which will work similarly as adduser command.

Step2: Set password for this user so that he can login to the machine. If this step is missed user can not login to this machine.

passwd username

Example:

passwd rajesh
passwd ******
confirm passwd ******

Step3: Check if the user account is created successfully or not

grep rajesh /etc/passwd
or
tail /etc/passwd
This command should show you a line contain about user:rajesh details.

Saturday, January 17, 2015

Linux Change File / Directory Ownership - Chown Command



The files, directories and processes (which are again files) in Linux are owned by users. They have a group owner. The owner and group ownership is important as the security of files through permissions (the DAC) is set on owner, group owner and others. The chown command can change the ownership and group ownership of a file.

Linux chown command
Change Ownership
To change the ownership of a file, chown is provided with two arguments, the new owner and the file whose owner is to be changed.

# ls -l corpora/stopwords/english
-rw-r--r-- 1 root root 623 Dec 10 2012 corpora/stopwords/english

# chown raghu corpora/stopwords/english

# ls -l corpora/stopwords/english

-rw-r--r-- 1 raghu root 623 Dec 10 2013 corpora/stopwords/english
The root user is the owner of the file, chown commands makes raghu user the new owner.
Changing owner and group

If the owner is followed by a colon and a group name (without spaces), the group name is changed as well.

# ls -l corpora/stopwords/rajesh
-rw-r--r-- 1 root root 424 Dec 10 2013 corpora/stopwords/rajesh

# chown raghu:altair corpora/stopwords/rajesh
# ls -l corpora/stopwords/rajesh

-rw-r--r-- 1 raghu altair 424 Dec 10 2012 corpora/stopwords/rajesh

Now the new owner of the file is raghu and the new group owner is altair group.
Now, in this syntax involving colon, if a colon but no group name follows the user name, the given user is made the owner of the file and that user's login group is made as the group owner of the file.

# ls -l corpora/stopwords/dutch
-rw-r--r-- 1 root root 453 Dec 10 2012 corpora/stopwords/dutch

# chown raghu: corpora/stopwords/dutch

# ls -l corpora/stopwords/dutch
-rw-r--r-- 1 raghu raghu 453 Dec 10 2013 corpora/stopwords/dutch

If the colon and group are mentioned, only the group of the file is changed. In this case, the command works like chgrp command.

# ls -l corpora/stopwords/finnish
-rw-r--r-- 1 root root 1579 Dec 10 2013 corpora/stopwords/finnish

# chown :altair corpora/stopwords/finnish
# ls -l corpora/stopwords/finnish
-rw-r--r-- 1 root altair 1579 Dec 10 2012 corpora/stopwords/finnish

Changing permissions for directories recursively
The ownership of the directories and files contained in them can be changed recursively with -R option.

# ls -l /home/rajesh/
total 12
-rw-rw-r-- 1 rajesh javaproject 0 Aug 19 2013 file1
drwxrwxr-x 2 rajesh rajesh 4096 Aug 19 2013 hello

# chown -R raghu /home/rajesh/
# ls -l /home/rajesh/
total 12
-rw-rw-r-- 1 raghu javaproject 0 Aug 19 2013 file1
drwxrwxr-x 2 raghu rajesh 4096 Aug 19 2013 hello

Verbose output
The --verbose option shows all the ownership changing. It outputs the diagnostics for each file processed

# chown -R --verbose rajesh /home/rajesh/
changed ownership of `/home/rajesh/hello' to rajesh
changed ownership of `/home/rajesh/.emacs' to rajesh
changed ownership of `/home/rajesh/.bash_history' to rajesh
changed ownership of `/home/rajesh/.bash_logout' to rajesh
changed ownership of `/home/rajesh/.bashrc' to rajesh
changed ownership of `/home/rajesh/file1' to rajesh
changed ownership of `/home/rajesh/.mozilla/plugins' to rajesh
changed ownership of `/home/rajesh/.mozilla/extensions' to rajesh
changed ownership of `/home/rajesh/.mozilla' to rajesh
changed ownership of `/home/rajesh/.bash_profile' to rajesh
changed ownership of `/home/rajesh/' to rajesh

The verbose option outputs processing of each file even when the changes are not made. But with -c or --changes option, the output is reported only when changes are made. For example,

# chown -R --verbose rajesh /home/rajesh/
ownership of `/home/rajesh/hello' retained as rajesh
ownership of `/home/rajesh/.emacs' retained as rajesh
ownership of `/home/rajesh/.bash_history' retained as rajesh
ownership of `/home/rajesh/.bash_logout' retained as rajesh
ownership of `/home/rajesh/.bashrc' retained as rajesh
ownership of `/home/rajesh/file1' retained as rajesh
ownership of `/home/rajesh/.mozilla/plugins' retained as rajesh
ownership of `/home/rajesh/.mozilla/extensions' retained as rajesh
ownership of `/home/rajesh/.mozilla' retained as rajesh
ownership of `/home/rajesh/.bash_profile' retained as rajesh
ownership of `/home/rajesh/' retained as rajesh

Monday, January 12, 2015

How to mount DVD or CDROM in Linux



DVDROM (Digital Versatile Disk Read-only memory) and CDROM (Compact Disc Read-only memory) are optical storage devices to store your data for future uses or for backups. May people use these disks to store movies, photos etc. By default many older Linux machines will not allow you to see the content of them. This is due that, they are not mounted by default in your box. We have to mount them properly so that we can access their content.

There are many ways to do mounting CDROM/DVDROM’s. One of the classic way is to use mount command which is available in Linux. Before mounting a CDROM or DVDROM we have to check what hardware file corresponding to our disk drive. If you have DVD Drive then you should see /dev/dvdrom or /dvd-rw file. If you have CDrom then you should find /dev/cdrom or /dev/cd-rw file. 

Once you are conformed about your device, you can use any one command mention below depending on your device name.

If your device is only CD reader use below command
mount -t iso9660 /dev/cdrom /media/

If your device is a CD readwrite use below command
mount -t iso9660 /dev/cdrw /media/

If your device is a DVD reader use below command
mount -t iso9660 /dev/dvdrom /media/

If your device is a DVD writer use below command
mount -t iso9660 /dev/dvd-rw /media/

Let us see what this command means.

mount is the command to mount devices files etc in a Linux/Unix box.
-t is the option to specify the format of the device, here it is iso9660 format. This is the format how data is written on to the device.

/dev/dvd-rw is a DVD writer present in your machine

/media is the point where you are going to mount your device. once you cd to /media you can see the contact of DVD.

Note:Some times you will not see /dev/cdrom or cdrw or dvd or dvd-rw files. At this time you have to know that it is associated with /dev/hdb or hdc etc.
We can do permanent mounting by using fstab file.

How to change Apache port in Linux ?



Change the port number of Apache web server in Linux and Unix?

Some times we have to change the port for some services such as Apache so that they start listening on the port.

To change the port of Apache we have to edit httpd.conf which is located in /etc/httpd/conf in most Linux distributions. Open httpd.conf file and search for the word Listen. This Listen entry is to inform Apache where to listen for incoming http packets so that Apache can start processing the http packets.

Now change the port assigned to what ever port you want.
Listen <port>

Changing the port to 443 in httpd.conf file.
Listen 443

Once you edited the httpd.conf file save and exit it.

Service httpd restart

Start the Apache service to take this change live. Now try to access website with mention port.