Introduction to System Administration
LAB 6 – BASIC SHELL COMMANDS
Introduction
In this lab you will get an opportunity to use some more BASH commands. You will work with the chmod
and chown commands which are the main commands for setting permissions and ownership on files.
Objectives
The main objectives of this lab are:
Review CAT from last weeks lab
Superuser Account
File Permissions (chmod) and File Ownership (chown)
1. Review CAT from last week
cat Command
cat is one of the most useful commands you’ll use. It can be used to (i) create text files, (ii) view contents
of files. Use man to get help on cat and see some of its additional uses.
Usage Example
Use cat to create a text file cat > file1
“I’m a first year”
Ctrl/D to save and exit
Use cat to view contents of a text file cat file1
Use cat to view the contents of a file stored in a child directory cat /etc/network//if-up.d/ethtool
Use cat to view system information (kernel version) cat /proc/version
Use cat to Retrieve Information on Partition Details cat /proc/partitions
(name,blocksize, etc)
Use cat to get a list of all linux file systems cat /proc/filesystems
2. Superuser Account
Page 1
Introduction to System Administration
In Linux, the Superuser account is the highest privileged account with unrestricted access to all files and
commands. The username to this account is root. When completing many tasks in Linux, you are
required to use this account.
There are two ways to use the account (i) log in as root directly and (ii) execute the command su when
logged in to another user account.
sudo –i signs you in as root … When you enter root mode, a # is displayedexi
To exit from here, type exit. Note the $ denotes a not root user.
Sudo: Selective Access to Superuser Commands
You can also use the sudo command which allows you to run specific commands as root without having to
know the root password. Some of the commands below require privileged mode to run.
sudo [command]
3. File Permissions
Files have been created so far using commands such as touch or cat. Files can also be created using a Text
Editor such as Nano.
As stated earlier, everything in Linux is represented as a file – this includes devices. Files can be (i) static
or variable and (ii) shareable or non-shareable. Commands themselves (cat, touch, grep, etc) are executable
files stored in the /bin directory.
As a Linux System Administrator, you must be very familiar with (i) types of files (ii) permissions set on
files (iii) ownership of files.
File Permissions
This covers how to protect files from unwanted access or provide wanted access. There are three
permissions associated with files – read (r), write (w) and execute (x). There are three groups The
permission of a file is called its file mode. File modes are set with the chmod command.
Permissions can be set using (i) symbolic or (ii) numeric notation. On your own, fill out this table with an
explanation of what the permission does
Page 2
Introduction to System Administration
Symbolic Numeric Explanation
--- 000
--x 001
-w- 010
-wx 011
r-- 100
r-x 101
rw 110
rwx 111
Checking File Permissions
1. `Create a new file called file1. Then, check the file permissions using ls -l.
ls –l file1
Notice rw (read write)
r(read)
Access Classes
There are three basic classes of file access for which permissions may be set:
user access – owner of the file(u),
group access – access granted to members of the same group as group owner of file(g)
other access (o) – access granted to all other normal users.
Operators
+ adds a permission
- removes a permission
= sets exact access specified (removes all access)
-rw- rw- r--
user group other
Changing File Permissions
To change file permissions, you use the chmod command. The syntax for this command is
chmod [access(user,group,other) operator (+ or -) permissions (r,w,x)] [file to change].
Page 3
Introduction to System Administration
Using Symbolic Notation
Try these commands out and write an explanation for each
Command Explanation of Code
chmod u+x file1
chmod g-w file1
chmod o+x file1
chmod a-w file1
chmod a+w file1
chmod a+r file1
chmod a+x file1
chmod = file1
Chmod go= file1
Using Numeric Notation
Command Explanation of Code
chmod 444 file1
chmod 541 file1
chmod 761 file1
chmod 661 file1
chmod 777 file1
Read = 4
Write = 2
Execute = 1
Page 4
Introduction to System Administration
Independent Task
1. On your own, create another file called file2.txt.
2. Set the Permissions on the file as follows:
User: read, write, execute
Group: execute only
Other: read and execute
3. Write out the code you used to complete the task in the space below using both symbolic and numeric
notation:
4. File Ownership
In Linux systems, files have two owners – a user owner and a group owner. These two owners are
separate – a file’s group ownership is independent of the user who owns it. A file’s group ownership can be
the same as the user owner – but it doesn’t have to be. The user owner does not need to be a member of the
group that owns it.
Displaying File Ownership
To display a file’s user and group ownership, use ls –l. In this example, the user and group are both the
same. When a new file is created, the user owner is the user who creates it and the group owner is the
current group of the user who creates the file.
User
Group
Changing File Ownership
To change ownership of a file in Linux, use the chown and chgrp commands.
Changing User Ownership (chown)
The chown command changes the user owner of the file. The syntax is chown [new user owner]
[filename]. To change the ownership of file1 to root user, use the following syntax. As this is a privileged
command, you’ll need to run in sudo mode
sudo chown root file1
Page 5
Introduction to System Administration
Confirm the new user owner of the file using ls –l.
Changing Group Ownership (chgrp)
The chgrp command changes the group owner of the file. The syntax is chgrp [new group owner]
[filename]. To change the group ownership of file1 to group root, use the following syntax
sudo chgrp root file1
Independent Task
1. On your own,
Change the user ownership back to rooneye
Change the group ownership back to rooneye
2. Write out the syntax in the space below
Changing User and Group Ownership Together
You can change both user and group ownership in a single operation using the following syntax: chown
[new owner]:[new group]
To change file1 back to root user and root group:
chown root:root file1
5. Independent Learning Task
Using BASH code, complete the following task:
Create a new file called biatweek9
Check the current permissions on the file
Set the following permissions on the file
User: rwx
Group: rw
Other: rx
Check ownership of the file
Change ownership on the file as follows:
User owner: root
Group owner: backup
Confirm new ownership
Backup the file to the backup folder (if you don’t have one, create one in your /home directory)
Write the code you used in the space below
Page 6
Introduction to System Administration
Page 7