Linux File
Linux File
i. Using useradd
Syntax: useradd options username
examplle : useradd ncth
ncth account is created using the default parameters contained in the following
configuration files: /etc/default/useradd/etc/login.defs. This file contains values
that can be used for the GID and UID parameters when creating an account with
useradd. It also contains defaults for creating passwords in /etc/shadow.
You can also view these default values by entering useradd –D at the shell
prompt.
Options:
o –c Includes the user’s full name.
o –e Specifies the date when the user account will be disabled.
Format the date as yyyy-mm-dd.
o –f Specifies the number of days after password expiration before
the account is disabled. Use a value of –1 to disable this
functionality, e.g., useradd –f –1 jmcarthur.
o –g Specifies the user’s default group.
o –G Specifies additional groups that the user is to be made a
member of.
o –M Specifies that the user account be created without a home
directory.
o –m Specifies the user’s home directory.
o –n Used only on Red Hat or Fedora systems. By default, these
systems create a new group with the same name as the user every
time an account is created. Using this option will turn off this
functionality.
o –p Specifies the user’s password.
o –r Specifies that the user being created is a system user.
o –s Specifies the default shell for the user.
o –u Manually specifies a UID for the user.
o EX: useradd –c “Tommy” ncth1
o useradd –c “Truong Khac Tung” –m –p “tung123” –s “/bin/bash”
tktung
i. Using groupadd
● Syntax:
groupadd options groupname
● Options:
o –g Specifies a GID for the new group.
o –p Specifies a password for the group.
o –r Specifies that the group being created is a system group.
ii. Using groupmod
● To modify a group, including adding users to the group membership, you
use the groupmod utility.
● Syntax:
groupmod options group
● Options:
o –g Changes the group’s GID number.
o –p Changes the group’s password.
o –A Adds a user account to the group.
o –R Removes a user account from the group.
● If we wanted to add ncth to the group, we would enter
groupmod –A “ncth” student at the shell prompt.
iii. Using groupdel
● Syntax:
groupdel group_name
● ex: groupdel student
● Managing ownership
o How ownership works
o Managing ownership from the command line
i. How ownership works
o Anytime a user creates a new file or directory, his or her user account is
assigned as that file or directory’s “owner.” For example, suppose the
“Anchal” user logs in to her Linux system and creates a file named
linux_introduction.odt using OpenOffice.org in home directory. Because
she created this file, she is automatically assigned ownership of
linux_introduction.odt. By right-clicking on this file in the system’s
graphical user interface and selecting Properties | Permissions, you can
view who owns the file.
o You can also view file ownership from the command line using the ls –l
command
ii. Managing ownership from the command line
You can specify a different user and/or group as the owner of a given file
or directory. To change the user who owns a file, you must be logged in
as root. To change the group that owns a file, you must be logged in as
root or as the user who currently owns the file.
✔ Using chown
✔ Using chgrp
● Using chown
o The chown utility can be used to change the user or group that owns a
file or directory.
o Syntax: chown user.group file or directory.
o Ex: If I wanted to change the file’s owner to the ncth1 user, I would
enter chown ncth1 /tmp/myfile.txt
● Using chgrp
o In addition to chown, you can also use chgrp to change the group that
owns a file or directory.
o Syntax: chgrp group file (or directory)
o For example: chgrp student /tmp/newfile.txt.
● Managing ownership
o How ownership works
o Managing ownership from the command line
iii. How ownership works
o Anytime a user creates a new file or directory, his or her user account is
assigned as that file or directory’s “owner.” For example, suppose the
“Anchal” user logs in to her Linux system and creates a file named
linux_introduction.odt using OpenOffice.org in home directory. Because
she created this file, she is automatically assigned ownership of
linux_introduction.odt. By right-clicking on this file in the system’s
graphical user interface and selecting Properties | Permissions, you can
view who owns the file.
o You can also view file ownership from the command line using the ls –l
command
iv. Managing ownership from the command line
You can specify a different user and/or group as the owner of a given file
or directory. To change the user who owns a file, you must be logged in
as root. To change the group that owns a file, you must be logged in as
root or as the user who currently owns the file.
✔ Using chown
✔ Using chgrp
● Using chown
o The chown utility can be used to change the user or group that owns a
file or directory.
o Syntax: chown user.group file or directory.
o Ex: If I wanted to change the file’s owner to the ncth1 user, I would
enter chown ncth1 /tmp/myfile.txt
● Using chgrp
o In addition to chown, you can also use chgrp to change the group that
owns a file or directory.
o Syntax: chgrp group file (or directory)
o For example: chgrp student /tmp/newfile.txt.
❑ Managing permissions
✔ How permissions work
✔ Managing permissions from the command line
✔ Working with default permissions
✔ Working with special permissions
- How permissions work
Each file or directory in your Linux file system stores the specific
permissions assigned to it. These permissions together constitute the
mode of the file. These permissions are assigned to each of three different
entities for each file and directory in the file system:
✔ Owner This is the user account that has been assigned to be the file or
directory’s owner. Permissions assigned to the owner apply only to that
user account.
✔ Group This is the group that has been assigned ownership of the file or
directory. Permissions assigned to the group apply to all user accounts
that are members of that group.
✔ Others This entity refers to all other users who have successful
authenticated to the system. Permissions assigned to this entity apply to
these user accounts.
ls –l
- Managing Permissions from the Command Line with chmod
Chmod entity=permissions filename
Owner, g for Group, and o for Others in the entity portion of the command.
You substitute r, w, and/or x for the permissions portion of the command. For
example, suppose I wanted to change the mode of contacts.odt to –rw–rw–r– –
chmod u=rw,g=rw,o=r contacts.odt
❑ You can also use chmod to toggle a particular permission on or
off using the + or – signs. For example, suppose I want to turn off
the write permission I just gave to Group for the contacts.odt file.
I could enter chmod g–w contacts.odt at the shell prompt.
❑ You can modify all three entities at once with only three
characters. To do this, enter
chmod numeric_permission filename
ex: chmod 660 contacts.odt
Working with default permissions
❑ By default, Linux assigns rw–rw–rw– (666) permissions to every
file whenever it is created in the file system. It also assigns
rwxrwxrwx permissions to every directory created in the file
system. It also assigns rwxrwxrwx permissions to every directory
created in the file system.
❑ To increase the overall security of the system, Linux uses a
variable called umask to automatically remove permissions from
the default mode whenever a file or directory is created in the file
system. The value of umask is a three-digit number.
Practical 8 – Script to perform arithmetic operations on
Integers
SCRIPT:
OUTPUT:
Practical 9 – Script to perform arithmetic operations on
Real Numbers
Practical 10 – Write a script to copy a file
Practical 11 – Write a script to print something using for
loop