1. Ownership & Permissions
Linux systems are multi user environments that allow users to create
files, run programs and share data.
Files and directories have two types of ownership the user and group.
A Linux group consists of one or more users.
Files and directories have three types of access permissions:
a)read permission (r)
b)write permission (w)
c)execute permission (x)
Every file and directory has permissions for three levels or entities of
permissions:
a)user or owner (denoted by u)
b)group (one or more users denoted by g)
c)others or world (denoted by o)
2. Permissions triplets
Each triplet indicates the access permissions for that level – in the example
below,the user/owner has read,write & execute permission,other group
members only have read and execute permissions and all others have no
access permissions.
3. Changing Permissions and Ownership
Use ‘ chmod ’ to change the file:
chmod [ ugoa ][+/-][ rwx ] filename
where u=user, g=group, o=others or world and a=all three
For example, to provide group read access to a file:
$ chmod g+r myfile
Or to remove file access to another than the owner or group members
(in other words, others):
$ chmod o-rwx myfile
The ‘ chown ’ command is used to change file ownership and the
chgrp ’ command can change group ownership of a file. As a regular
user, you can not change the ownership of a file, but you can change
the group ownership if you are a member of the group to which you
are changing the group ownership.
You can use the R argument on any of the above to recursively make
changes on a directory of files.
4. Lab 2
1)First go to your home directory.
2)Make a ‘LinuxClass’ directory using the mkdir command & go into
that directory.
3)Create 5 empty files in LinuxClass directory.
4)Show permission of newly created files.
5)Add execute permission for group on 2 of the files and make other 3
world readable.
6)Now, remove the execute permission of LinuxClass for all.
7)Change directory to your home directory, then try to see all files under
LinuxClass and then finally go into that directory.
8)Explain what happened and why?
5. Simple filter and advance filter commands
grep–pattern matching search of a file
$ grep cat nonsense.txt
$ grep –idog nonsense.txt # case insensitive
uniq–show or remove duplicate lines
$ uniq bears # will show all unique lines
$ uniq–d bears # show only duplicate lines
$ uniq–c bears # show a count of each unique line
Sorting-read a file, sort the contents and output to the terminal
$ sort –r grades.txt
$ sort -k2 grades.txt
$ sort –bnr–k2 grades.txt
awk–text manipulation
$ awk ‘{print $1,$5,$3,$4,$2,$6}’
6. Simple filter and advance filter commands...
sed–stream editor for pattern matching and modification
$ sed ‘s/beat/defeated/g’ hare_tortoise # Replace ‘beat’ with ‘defeated’
The trailing ‘/g’at the end of that command indicates that the change is to be
done globally…without it, only the first occurrence of the word in the file will
be changed.
$ sed -i 's/SEARCH_REGEX/REPLACEMENT/g' INPUTFILE # Replace text in file
head or tail
$ head mascots.txt
-prints out the first 10 lines by default. Can use the –n argument to change
the default number of lines
$ tail –20 mascots.txt
-prints out the last 20 lines
7. Environment Variables
Execution path
In BASH, execution of a program happens when you enter the
program name. Your PATH variable keeps you from having to enter
the full path to the program
Modifying your PATH
$ echo $PATH
$ PATH=$PATH:/data/$USER
$ echo $PATH
PATH set using above method is not persistent after reboot.
To path changes permanent, need to modify the .bashrcfile in your home
directory or update /etc/environment.
9. Package Management : RPM
RPM Package Manager (RPM)
YUM and DNF are simply front-ends to a lower-level tool called RPM. The following
commands should be run as root. The flags are expanded here, but the abbreviated
syntax is also included:
rpm --install --verbose --hash local-rpm-file-name.rpm - Installs an rpm from
the file. rpm is also capable of installing RPM files from http and ftp
sources as well as local files.
rpm --erase package-name(s) - Removes the given package.
rpm --query --all - lists the name of all packages currently installed.
rpm --query package-name(s) - allows you to confirm whether a given package is
installed in your system.
rpm --query --info package-name(s) - displays the information about an
installed package.
rpm --query --list package-name(s) - generates a list of files installed by a
given package. This is complemented by:
rpm --query --file - checks to see what installed package “owns” a given file.
10. Package Management : dpkg
Debian Package Manager (DEB)
Apt-get and apt-cache are merely frontend programs that provide a more usable
interface and connections to repositories for the underlying package management tools
called dpkg.
dpkg -i package-file-name.deb - Installs a .deb file.
dpkg --list search-pattern - Lists packages currently installed on the
system.
dpkg --configure package-name(s) - Runs a configuration interface to set
up a package.
dpkg-reconfigure package-name(s) - Runs a configuration interface on an
already installed package.
dpkg --purge --force-all package – Remove/ uninstall package.
11. Start and Stop services
systemctl commands
sudo systemctl [action] [service name]
service commands
sudo service [service name] [action]
To start a service:
sudo systemctl start ufw
sudo service ufw start
To stop a service:
sudo systemctl stop ufw
sudo service ufw stop
To restart a service:
sudo systemctl restart ufw
sudo service ufw restart
To check the status of service:
sudo systemctl status ufw
sudo service --status-all
Set service to start at boot:
sudo systemctl enable ufw
sudo chkconfig ufw on
Disable service to start at boot:
sudo systemctl disable ufw
sudo chkconfig ufw off
12. Lab 3
1. Install httpd using yum.
2. check status of httpd service.
3. Check httpd is running under which user using ps.
4. Download file index.html from below link, in your home directory.
http://3.93.79.170:9090/webfile.tar.gz
5. Extract that tar file. You will have one file called index.html
6. Next, copy that index.html to /var/www/html/ (You might be asked to overwrite an
existing file, do that.)
7. Set ownership and permission on that file as per the permission of /var/www/html
8. Restart httpd service.
9. Check if your webserver is working or not with below command:
# curl http://localhost
Output should be like:
Note: If your webserver is having issue run below command and try again.
# setenforce 0