Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Tuesday, August 12, 2014

File Permissions in Linux - Umask,Chmod,Chown.



File permissions

Linux uses the same permissions scheme as Unix. Each file and directory on your system is assigned access rights for the owner of the file, the members of a group of related users, and everybody else. Rights can be assigned to read a file, to write a file, and to execute a file.
Permissions on Unix systems are managed in three distinct scopes or classes. These scopes are known as user(U), group(G), and others(O).


Values for Read, Write and Execute.
Read        - 4
Write       - 2
Execute   - 1

  • The read permission grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory, but not to find out any further information about them such as contents, file type, size, ownership, permissions.
  • The write permission grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
  • The execute permission grants the ability to execute a file. This permission must be set for executable programs, including shell scripts, in order to allow the operating system to run them. When set for a directory, this permission grants the ability to access file contents and meta-information if its name is known, but not list files inside the directory, unless read is set also.
           
Symbolic Way
Numeric Way
Permission
User (U)
Group(G)
Others(O)
---                 ---                   ---
000
No Permission
--x                --x                  --x
111
Execute
-w-               -w-                 -w-
222
Write
-wx               -wx                -wx
333
Write & Execute
r--                 r--                   r--
444
Read
r-x                r-x                  r-x
555
Read & Execute
rw-               rw-                 rw-
666
Read & Write
rwx              rwx                rwx
777
Read & Write & Execute






To see the permission settings for a file, we can use the ls command as follows:

root@linuxstorages:~# ls A1.html
-rw-r--r--   1 root root  353 Aug 25 18:13 A1.html

Here file A1.html has the permission 644. Users have read & write, group and others have only read permission.

CHMOD 

Chmod command is used to set permission for the files and folders.

Eg: Numeric way of assigning permission to a file.

root@linuxstorages:~# chmod 640 ab.txt  

root@linuxstorages:~# chmod 666 ac.txt

root@linuxstorages:~# chmod 755 ad.txt

Eg: Symbolic way of assigning permission to a file.

root@linuxstorages:~#chmod  ugo+x  ab.txt

Adding execution permission to all user, group and others.

root@linuxstorages:~# chmod u=w,g=wx,o=x dd.txt

user – write, group – write and execute, others – execute.

root@linuxstorages:~# chmod u-wx,g-x,o-rw ac.txt

Removing write and execute from user, execute from group , read and write from others.

What is UMASK ?

The user file creation mode mask [umask] is used to determine the file permission for Normal and Root user.

Default value for Folder  777

Default value for File      666

For Root user Umask value is 022

Type
Permission
Umask
Subtract
Folder
Permission
Folder
777
022
777-022
755
rwxr-xr-x
File
666
022
666-022
644
rw-r--r--

For Normal user Umask value is 002

Type
Permission
Umask
Subtract
Folder
Permission
Folder
777
002
777-002
775
rwxrwxr-x
File
666
002
666-002
664
rw-rw-r--

To understand the value of default UMASK value for Root and Normal user.


root@linuxstorages:~# vi /etc/bashrc


CHOWN


Changing the user ownership of a file or a directory.

root@linuxstorages:~# chown <User Name> <File Name>

root@linuxstorages:~#chown storage sample.txt

-R option is used for recursive mainly for directories and have sub-directories.

root@linuxstorages:~#chown –R storages /home/backup

Permission will reflect to all it sub-directories in backup folder.


CHGRP

To change a group for a file or a folder.


root@linuxstorages:~# chgrp storages abc.txt

root@linuxstorages:~# chgrp storages /data

will change only to data folder.

root@linuxstorages:~#chgrp –R storages /data

will change all its sub directories in data folder.


Monday, May 26, 2014

Cron Job In Linux

Cron is time-based job scheduler. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system wide crontab file (usually in /etc or a subdirectory of /etc) that only system administrators can edit.


Linux Crontab Format


  *         *             *        *          *                   /path-of-script
(min)   (hour)   (dom)   (mon)  (dow )


Field        Description       Allowed Value

MIN        Minute field         0 to 59
HOUR     Hour field           0 to 23
DOM      Day of Month     1-31
MON      Month field         1-12
DOW      Day Of Week     0-6

1. How to list crontab entries

root@linuxstorage:~#crontab -l

2. How to edit and add crontab entries

root@linuxstorage:~#crontab -e 

3.Schedule a Job for Every Minute Using Cron 

* * * * * .         /path-of-script

4.Schedule a Background Cron Job For Every 10 Minutes

*/10 * * * *     /path-of-script

5.Scheduling a Job For a Specific Time

30 09 10 08 *  /path-0f-script
job will run at Aug month(8 month) , 10 day , 9.30 time(am).

6. Schedule a Job For More Than One Instance

00 09,19 * * *   /path-of-script

00 - 0 minute
09 - 9 am
19 - 7 pm

job will run in both timings 9:00 & 19:00.

7 .Schedule a Job for Specific Range of Time

00 09-19 * * *   /path-of-script
09 - 19 = time from 9 to 19.  9,10,11,12,13,14,15,16,17,18 & 19.

job will run from 9:00 to 19:00 every one hour.

8. Cron for visiting URL

* * * * *    /usr/bin/wget   http://your-website/cron.php 

* * * * *   /usr/bin/curl      http://your-website/cron.php

* * * * *    /usr/bin/wget   -q -o http://your-website/cron.php  

Encrypt/Decrypt Zip File In Linux

We can see how zip utility is used and how it's beneficial to use as well as how to secure your zip files and what precautions you should take while zipping or while providing security to your zip files.To enhance security of zip files i have also included encryption and decryption concept a bit.

Encrypting a file in gpg command


root@linuxstorage:~#gpg -c data.zip
Enter passphrase:

gpg utility has been used here to encrypt the zip file.

-c option is used to create gpg based encrypted file.


root@linuxstorage:ls
data.zip.gpg

We can't able to unzip the gpg zipped file without decrypting. So first decrypt and then unzip the file.


root@linuxstorage:gpg data.zip.gpg
Enter passphrase:

now when you enter the passphrase which you have given during encryption time the file will be decrypted.


root@linuxstorage:unzip data.zip
root@linuxstorage:ls
data

How To Compress/Decompress File Or Folder In Linux

In Linux there various commands which  help us to compress and decompress a data. By doing a compression  we can able to reduce the size of the data and data will not be missing while transferring from one server to other.

Zip

zip is a compression and file packaging utility for Unix/Linux. Each file is stored in single .zip {.zip-filename} file with the extension .zip.

Compress for zip

root@linuxstorage:~#zip data.zip data
root@linuxstorage:~#ls
data.zip

Zip a folder for zip

root@linuxstorage:~#zip -r folder.zip data
root@linuxstorage:~#ls
folder.zip

Decompress

root@linuxstorage:~#unzip data.zip
root@linuxstorage:~#ls
data

Gzip 

Gzip compress the size of the given files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz.

gzip is a commonly used compression format in GNU/Linux platforms

Compress for gzip

root@linuxstorage:~#gzip data
root@linuxstorage:~#ls
data.gz

Decompress for gzip
 
root@linuxstorage:~#gunzip data.gz
root@linuxstorage:~#ls
data
 OR
root@linuxstorage:~#gzip -d data.gz
root@linuxstorage:~#ls
data


Bzip2

bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. Compression is generally considerably better than that achieved by bzip command (LZ77/LZ78-based compressors). Whenever possible, each file is replaced by one with the extension .bz2

Compress for bzip2
 
 root@linuxstorage:~#bzip2 data
 root@linuxstorage:~#ls
 data.bz2

 Decompress for bzip2

root@linuxstorage:~# bzip2 -ddata.bz2
or
root@linuxstorage:~#gunzip data.bz2
root@linuxstorage:~#ls
data
  
Gzip and Bzip2 with tarball

The GNU tar is archiving utility but it can be use to compressing large file(s). GNU tar supports both archive compressing through gzip and bzip2. If you have more than 2 files then it is recommended to use tar instead of gzip or bzip2

-z: use gzip compress
-j:
use bzip2 compress

Compress for gzip

root@linuxstorage:~#tar  -zcvf  data.tar.gz   [/path/files]
root@linuxstorage:~#ls
data.tar.gz
  • -z: Compress archive using gzip program
  • -c: Create archive
  • -v: Verbose i.e display progress while creating archive
  • -f: Archive File name
Decompress for gzip

root@linuxstorage:~#tar -zxvf data.tar.gz
root@linuxstorage:~#ls
data
  • -x: Extract files

Compress for bzip2

root@linuxstorage:~#tar  -jcvf  data.tar.bz2 [/path/files]
root@linuxstorage:~#ls
data.tar.bz2

Decompress for bzip2

root@linuxstorage:~#tar -jxvf data.tar.bz2
root@linuxstorage:~#ls
data



 

© 2014 Linux Storages | Updated . All rights resevered. Designed by Templateism