Sunday 15 May 2016

File Links

File Types
            -b         -           block device file Example: HDD and pen drive
            -d         -           directory file
            -           -           common file
            c          -           Character device file Example: terminal
            l           -           Linked file
Linking means reflecting to the original file, In case of copy command updating is not possible after copying the file from the source to destination. In link updating is possible for both the files. 
HARD Link
SOFT Link
1.    The destination file is exact image of the source file.
1.    The destination file size is length of the source file name
2.    If source got deleted also even we can access the destination file
2.  if source got deleted we can’t access destination file
3.    inode numbers of source and destination are same
3. inode numbers of source and destination are different
4.    We can’t put the hard link to different file system (partitions) because it will different.
4. we can put a link between different file systems
5.    Ex: cp –l <source> <destination>
Ex: cp –s <source> <destination>

String Related Commands

String related commands will help you to print/search file text as required
HEAD: Head prints the first N number of data of the given input. By default, it prints first 10 lines of each given file.

Example:       head file2
                      head –n 2 file3  #number of lines
 
head -n Command Output
SORT:   Sort is a simple and very useful command which will rearrange the lines in a text file so that they are sorted, numerically and alphabetically. By default, the rules for sorting are:
ü  Lines starting with a number will appear before lines starting with a letter.
ü  Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
ü  Lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.
Example:       sort –r file2
sort -r Command Output
Options
  -b         ignores leading blanks
  -d         considers only blanks and alphanumeric characters
  -f          fold lower case to upper case characters
  -g         compare according to general numerical value
  -i          consider only printable characters
  -M       compare (unknown) < `JAN' < ... < `DEC'
  -n         compare according to string numerical value
  -r        reverse the result of comparisons
  -c         check whether input is sorted; does not sort
  -k         start a key at POS1, end it at POS2 (origin 1)
  -m        merges already sorted files; do not sort
  -o         write result to FILE instead of standard output
  -s         stabilize sort by disabling last-resort comparison
  -S         use SIZE for main memory buffer
  -t         use SEP instead of non-blank to blank transition
  -T        use DIR for temporaries, not $TMPDIR or /tmp
  -z         end lines with 0 byte, not newline
UNIQ:   Uniq command is helpful to remove or detect duplicate entries in a file.
                         
Example: uniq <file name> - it will print uniq values
uniq Command Output
PASTE:   It is very useful for merging a single file and also for merging set of files as well.
ü  paste command examples for single file handling
ü  paste command examples for multiple files handling

Example: paste –s file1       #All the separate lines are printed as one line
                paste –d, -s file1  #Combined the 'file1' and 'file2' with comma (,) separated.

CUT:   Cut is used for text processing. You can use this command to extract portion of text from a file by selecting columns.

Example: Below screenshot is the best example, first i have used cat command to see the content of file 'testcut'. In next highlighted cut command i have cut the 4 characters from the text.
cut Command Output
TR: It will translate content of the file from one case to another case vice versa. Upper case to Lower case.

Note: if you want to change the file text to caps then redirect the output to another file.

Example: in below example i have used 'file1' content to convert to caps lock characters.
tr Command Output
SED:  Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make changes to the file automatically, sed comes in handy to do this. Most people never learn its power; they just simply use sed to replace text. You can do many things apart from replacing text with sed.

Example: In below screenshot i have replaced the 'linux' string to 'unix' .
sed Command Output
DIFF: To compare the difference between two files text you can use this command
diff Command Output

Saturday 7 May 2016

Access control List ( ACL )

 There is, however, a much more flexible solution that you can manage yourself. Our filesystems support ACLs (Access Control Lists), which you can manage with the commands "getfacl" and "setfacl". What ACLs allow you to do is specify arbitrarily-fine-grained access control on a per-file or per-directory basis. So you could give, say, ravi and kumar "rwx" access to the file, but deny access to everybody else without ravi and kumar being in any Unix groups together.

Here is an example:

#setfacl -r -m user:san:rwx tempfile 
#setfacl -r -m user:test:rwx tempfile

This gives two different users full control of the 'tempfile' file. The -m option means to modify. Using a -s option required complete ACL specifications (easier to use -m). The -r option recalculates the
ACL mask for the file(s).

The 'getfacl tempfile' command produces:

#getfacl tempfile
# file: tempfile
# owner: dl4g
# group: staff
user::rw-
user:san:rwx #effective:rwx
user:test:rwx #effective:rwx
group::r-- #effective:r--
mask:rwx
other:---

   Use on directories with -R to recurse. Reading the man pages may make this seem more complicated, but this simple example and others work perfectly.

User Administration

Type of Users:
Root user                -           Default user highly privileged UID is 0. This will create while installing the operating system
System users         -           is nothing but services, at the time of installing particular package. UID starts from 1 to 499.
Local users            -           after installing of the operating system admin user will create these users. UID starts from 500 to 65534.
Ø  After creating a user, user home directory will be created in default path /home.
Ø  One group is will be created with same user name (primary group)
Ø  Files from /etc/skel will be copied automatically to user home directory
Ø  /etc/passwd file is updated with user information
Ø  /etc/group file is update with primary group information
Important files
/etc/passwd     User Information
/etc/shadow     User Passwords
/etc/group      Group Information
/etc/gshadow    Group Passwords
User Administration Commands:
# useradd <user name>     -           To create specified local user
# useradd –d <home directory> <user name>  - create a user with specified home path
[root@sankar ~]# useradd -d /users/ san
 

[root@sankar ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin


# useradd –u <UID> <user name>         - create user with specific UID.
# passwd <user name>      -           change the user password
# userdel <user name>      -           delete user
# userdel <user name>      -           delete user including home directory
# finger <user name>         -           See user properties
# chfn <user name>            -           Change user information




 
[root@sankar ~]# chfn root
Changing finger information for root.
Name [root]:





 # chage –l <user name> - to check user password expiry and account expiry information



 chage -l  root
Last password change                                    : May 02, 2015
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7
 


 

# su - <user name> -           Switch to other user account
# id <user name>    -           it will show the user id




[root@sankar ~]# id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys), 6(disk),10(wheel)
[root@sankar ~]#



# system-config-users         -           create and manage user account in GUI
Usermod command options:
-c = We can add comment field for the user account.
-d = To modify the directory for any existing user account.
-e = Using this option we can make the account expiry in specific period.
-g = Change the primary group for a User.
-G = To add a supplementary groups.
-a = To add anyone of the group to a secondary group.
-l = To change the login name from tecmint to tecmint_admin.
-L = To lock the user account. This will lock the password so we can’t use the account.
-m = moving the contents of the home directory from existing home dir to new dir.
-p = To Use un-encrypted password for the new password. (NOT Secured).
-s = Create a Specified shell for new accounts.
-u = Used to Assigned UID for the user account between 0 to 999.
-U = To unlock the user accounts. This will remove the password lock and allow us to use the user account.
Creating Groups:
Group information is located/stored on /etc/group file.
# groupadd <group name>           - Create a group with specified name
# usermod –G <group name> <user name> - Add user to group
# gpasswd –a san Administrators – Adds the user ravi to the group Administrators
# gpasswd –A san  Administrators – give user ravi administrative rights to the group
# gpasswd –d san  Administrators – remove user ravi from the group Administrators
# groupdel <group name> - Delete group name
# groupmod –n <new group name> <old group name> - change group name
# newgrp - <group name> - Login into the group if successful, re-initializes the user environment

Profile Management

A user profile is a visual display of personal data associated with a specific user, or a customized desktop environment. A profile refers therefore to the explicit digital representation of a person's identity. A user profile can also be considered as the computer representation of a user model.
/etc/profile                 -           it contains system void variables, if you do any modification in this file it will effect to the administrator and local user profiles.
~/.bash_profile         -           it contains user specific variables, if you do any modification in this file it will effect to that particular account only.
/etc/bashrc                 -           it contains system void alias variables
~/.bashrc                   -           it contains user specific alias variables
.bash_history           -           it contains all executed commands history
Commands:
# alias                        -           it will show the aliases 

[root@sankar ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@sankar ~]#

# unalias <alias name>     -           it will remove mentioned alias
Note: you can always define an alias using /etc/bashrc OR .bashrc files
File Permissions:
Permission
Value
Number
Read
r
4
Write
w
2
Execute
x
1
Default permissions when you create a file or directory
File Permissions
File
644
Directory
755
File and Directory Permissions
In above image explained about file permissions
Commands to Change file/directory permissions
Symbolic permissions
u          -           user/owner
g          -           Group
o          -           Others
w         -           Write
x          -           Execute
+          -           Allow
-           -           deny
# chmod [options] <mode/permissions> <file/directory> - to change permissions file/folder
            Example: chmod 744 file1
# chmod u+rwx file or directory : in case of user only
                        # chmod ug+rwx file or directoty : in case of user and group
                        # chmod u+w,g+r,o+x directory/file
                        # chmod u+rw,g+rw directory/file
                        # chmod u-r, g-w,o-rw directory/file
                        # chmod ugo+rwx file/directory
                        # chmod ugo-rwx file/directory
# chown [options] <new owner> <file/directory> - to change ownership of file/folder
            Example: chown user2 file1
                            chown user1:group1 file2
# chgrp [options] <new group> <file/directory> - to change group of file/folder
            Example: chgrp gorup2 file2