Chapter 4 Study Guide

From ITCwiki
Jump to navigation Jump to search

Modify file and directory ownership

When a user create a file or directory, that users name and primary group becomes the owner and group owner of the file.

The permission structure is affected.

It also determines the ability to modify file and directory permissions and ownership.

Only two users on a Linux system can modify permissions on a file or directory or change its ownership:

The owner of the file or directory and the root user.

Example: to view your current user name, you can type the whoami command.To view your group name you can type the groups command.

[roots@server1 -] # whoami

Root

[root@server1 -] # groups

Root bin daemon sys adm disk wheel

[root@server1 -] #_

To quickly create an empty file, you can use the touch command.

[root@server1 -] # touch file1

[root@server1 -] # ls –l

Total 4 drwx- - - - 3 root root 4096 Apr 8 07:12 Desktop

-rx-r- -r-- 1 root root 0 Apr 29 15:40 file1

[root@server1 -] #

To change the ownership of a file or directory, you can use the chown (change ownership) command. Which takes two arguments at minimum. The new owner and the files directories to change.

Both arguments can be absolute or relative pathnames, and you can change permissions recursively, throughout the directory tree using the –R option to the chown command.

To change the ownership of file1 to the user “user1” and the ownership of the directory Desktop and all its contents to “user1” as well, you can use these commands.

[root@server1 -] # chown user1 file1

[root@server1 -] # chown –R user1 Desktop

[root@server1 -] # ls –l

total 4

drwx 3 user1 root 4096 Apr 8 07:12 Desktop

-rw-r- -r- - 1 user1 root 0 Apr 29 15:40 file1

[root@server1 -] # ls -1 Desktop

Total 16

-rw- - - - - 1 user1 root 163 Mar 29 09:58 Floppy

-rw-r- -r-- 1 user1 root 163 Mar 29 09:58 Home

-rw-r- -r-- 1 user1 root 163 Mar 29 09:58 Start Here

-rw-r- -r-- 1 user1 root 163 Mar 29 09:58 Trash

[root@server1 -] #

If a regular user changes ownership of a file or directory that he owns, that user cannot gain back ownership. Instead the new owner of that fle or directory must change it to the original user. However, the previous examples involve the root user, who always has the ability to regain ownership.

[root@server1 -] # chown root file1

[root@server1 -] # chown –R root Desktop

[root@server1 -] # ls –l

total 4

drwx 3 root root 4096 Apr 8 07:12 Desktop

-rw-r- -r- - 1 root root 0 Apr 29 15:40 file1

[root@server1 -] # ls -1 Desktop

Total 16

-rw- - - - - 1 root root 163 Mar 29 09:58 Floppy

-rw-r- -r-- 1 root root 163 Mar 29 09:58 Home

-rw-r- -r-- 1 root root 163 Mar 29 09:58 Start Here

-rw-r- -r-- 1 root root 163 Mar 29 09:58 Trash

Just like the chown command. You can use the chgrp (change group) command to change group owner of a file and directory. The same rules apply.

Example:

[root@server1 -] # chgrp sys file1

[root@server1 -] # chgrp –R sys Desktop

[root@server1 -] # ls –l

total 4

drwx 3 root sys 4096 Apr 8 07:12 Desktop

-rw-r- -r- - 1 root sys 0 Apr 29 15:40 file1

[root@server1 -] # ls -1 Desktop

Total 16

-rw- - - - - 1 root sys 163 Mar 29 09:58 Floppy

-rw-r- -r-- 1 root sys 163 Mar 29 09:58 Home

-rw-r- -r-- 1 root sys 163 Mar 29 09:58 Start Here

-rw-r- -r-- 1 root sys 163 Mar 29 09:58 Trash

Note: Regular users can change the group of a file or directory only to a group they belong.

Normally, you change both the ownership and group ownership on a file that needs to be maintained by someone else. You can change both at the same time using the chown command.

To change the owner to user1 and the group owner to root for file 1 you will use these commands.

[root@server1 -] # chown user1.root file1

[root@server1 -] # chown –R user1.root Desktop

[root@server1 -] # ls –l

total 4

drwx 3 user1 root 4096 Apr 8 07:12 Desktop

-rw-r- -r- - 1 user1 root 0 Apr 29 15:40 file1

[root@server1 -] # ls -1 Desktop

Total 16

-rw- - - - - 1 user1 root 163 Mar 29 09:58 Floppy

-rw-r- -r-- 1 user1 root 3578 Mar 29 09:58 Home

-rw-r- -r-- 1 user1 root 1791 Mar 29 09:58 Start Here

-rw-r- -r-- 1 user1 root 4096 Mar 29 09:58 Trash

To protect your systems security, you should ensure that most files residing in a users home directory are owned by that user. To change the ownership back to the root user for file1 and the Desktop directory to avoid future problems. You can type the following.

[root@server1 -] # chown root.root file1

[root@server1 -] # chown –R root.root Desktop

[root@server1 -] # ls –l

total 4

drwx 3 user1 root 4096 Apr 8 07:12 Desktop

-rw-r- -r- - 1 user1 root 0 Apr 29 15:40 file1

[root@server1 -] # ls -1 Desktop

Total 16 -rw- - - - - 1 root root 163 Mar 29 09:58 Floppy

-rw-r- -r-- 1 root root 3578 Mar 29 09:58 Home

-rw-r- -r-- 1 root root 1791 Mar 29 09:58 Start Here

drwx- - - - - 2 root root 4096 Mar 29 09:58 Trash

[root@server1 -] #



Understand and Create linked files

What is a linked file? A link is a way of matching two or more file names to the same set of file data.

How many ways can a file be linked? 2 ways

What are they? A symbolic link, or symlic and A hard link.

How are files stored on a file system?

The structural level of a filesystem has three main sections: The superblock, The inode table and data blocks

Superblock : Is the section that contains information about the filesystem. Filesystem Type, Size, status, number of inodes.

Inodes Table: The collection of inodes for all files and directories on a filesystem. Each file in the Linux system gets its own inode. Inode(The portion of a file that holds information on the file’s attributes, access permissions, where it is located, who owns it, and file type. Each inode contains a unique inode number for identificattion purposes)

How do you view the inode number? ls -li

Data Blocks: The data that makes up the content of the file as well as the filename. Blocks are also know as allocation units.

Hard Link Characteristics

  • In a hard link, two files share the same data
  • Hard Link files are direct copies of one another
  • Same Size
  • They share the same inode and inode number
  • The hard link will look and behave the same as the original.
  • A file can be hard linked an unlimited number of times if the files reside on the same filesystem.
  • Hardlinks can not span across partitions
  • Points to a file by its inode

How do you create a Hard Link? To create a hard link you would use the ln (Link) command and specify the existing file to hard link and the target file that will be created.

Symbolic Link or symlic Charateristics

  • One file is a pointer or shortcut to another file. The files may point to files located on other partitions or other network drives
  • Symbolic links do not share the same inode and inode number with their target file
  • Different size
  • Symbolic links point to another file by its name
  • Soft links do not need to reside on the same filesystem
  • removing the target file for a symbolic link breaks the link and it will no longer work.

How do you create a symbolic link? To create a symbolic link you would use the ln (Link) command and use –s followed by the target file and the file you want to link.

Identify the default permissions created on files and directories

New files are given rw-rw-rw- by the system when they are created.

New directories are given rwxrwxrwx by the system when they are created.