0% found this document useful (0 votes)
2 views

Escape From Linux

The document outlines essential system administration duties, Unix operating system features, and various shell types. It covers file structures, permissions, commands for file management, and user account management, including creating and modifying user accounts. Additionally, it discusses Linux distributions, boot processes, and commands for system maintenance and network management.

Uploaded by

Arnold Masmini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Escape From Linux

The document outlines essential system administration duties, Unix operating system features, and various shell types. It covers file structures, permissions, commands for file management, and user account management, including creating and modifying user accounts. Additionally, it discusses Linux distributions, boot processes, and commands for system maintenance and network management.

Uploaded by

Arnold Masmini
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

INTRODUCTION

System admin duties:


-Installing and configuring servers
-Installing and configuring app software
-Creating and maintaining user accounts
-Backup and restore files
-Monitoring and tuning performance

Unix is a multi-user, multi-tasking OS

System call acts as a bridge between the programs and the kernel eg read, write, open etc
A shell is a special-purpose program designed to read commands typed by a user and
execute appropiate programs, it can be in the form of GUI or command line

Bourne shell – simplest shell, doesnt have modern featues like saving command history
C shell – added interactive features to the bourne shell but wasnt backward compatible, its
named c shell becasue its syntax resembles the C prog lang
Korn shell – added interactive features to the bourne shell but was also compatible with
bourne shell scripts
Bourne again shell – a modernized version of the bourne shell with extra features like
interactive abilities, its free and open source

Users and grps:


-Every user has a unique login name (username) and a corresponding UID

Hierachial file structure:


The kernel maintains a single hierachial directory structure to organize all files in the system,
root directory named /

File types:
1.Regular file – normal ones such as documents, images or programs
2.Plain file – a type of regular file that contains simple text (no formatting)

Directory: is a special file whose contents take the form of a table of filenames coupled with
references to the corresponding files; the filename-plus-reference association is called a link
and files may have multiple links, and thus multiple names, in the same or diff directories

Symbolic link:
-Its like a shortcut, points to another file or directory, like a virtual path

Filename can be up to 255 characters long


Pathname is a string consisting of an optional initial slash followed by a series of filenames
seperated by slashes
Home directories – users personal files are often found in the directory /home or /root (for
the superuser)
System directories – these contain system files, documentation, applications

A category tells u the types of files found in directory


1.Categories for programs – usually bin and lib directories
2.Categories for documentation – contained in doc, man, share and info folders
3.Categories for configuration – in etc, init.d, rc.d
4.Categories for programming – in include, src directories
5.Categpries for web files – www, public_html
6.Categories for display – fonts, X11
7.Categories for hardware – in dev (device files for interacting with disks and other
hardware), media and mnt (provide access to disks)
8.Categories for run time files – var (files specific to the comp), lock (lock files, created by
programs to say i am running), log, warning, mail etc

The scope of a directory path describes at a high level, the purpose of an entire directory
hierachy

OS directories:
/boot – files for booting the system, this is where the kernel lives
/proc – describes currently running processes, for advanced users

Permissions:
R – read a file (4)
W – write a file (2)
X – execute a file (1)
U can use ls -l to view the permissions
-rwxrwxr-x 1 juan student 0 Sep 26 12:25 test
The “-“ indicates it is a regular file, if it was d it would have meant directory

To list files we use the ls command


Searching files:
Using find – find ~/ -name example.txt -print
The second parameter (~/) is the starting point, the name example.txt specifies the exact file
The locate command is used to quickly find files or directories by their names. It relies on a
pre-built database of file locations, so it’s much faster than other search methods like find.
However, the database needs to be updated periodically to reflect changes in the file system.
Eg find all files containing the word zip, locate zip

Changing ownership: u use chown... chown <owner> <file/directory name>


Chown alice example.txt
Changing permissions: u use chmod… chmod -R ### <filename or directory>, -R is optional
and will change the permissions of all sub-directories

Chmod 755 example.txt


This means owner can do all, grp can read and execute, and others can read

Moving directories:
Cd, cd .. – these are obv
Cd the system is returned to the user home directory
Cd /home – this is the absolute path, u will go to /home wherever u are
Pwd is a command use to print ur current path
Mkdir – make a directory
Rmdir – remove a directory

Copy file – cp file_1 file_2 (both have to be in the same directory, if not the path must be
given)
Renaming/moving a file – mv file_1 file_2 (if they are in the same dir, its gonna rename, if
not it will move it, the destination must be specified when moving it)
Eg mv document.txt /home/user/Documents

Remove file – rm file_a


Using wildcard: rm h*c – removes all the files starting with h and ending with c
Rm * will remove all files from your working dir
Why Should I choose Linux?
Best price/performance ratio
Reliable
User friendly

Linux distributions
Often referred to as 'distros'.
The Linux kernel with a set of programs/applications (text editors, compilers, office suites,
web browsers, etc) that make the system usable. Eg ubuntu, red hat
Generic distros – include redhat based (eg fedora), and debian based (debian, ubuntu)
Task specific ones – eg BioLinux

Partitioning is a means to divide a single hard drive into many logical drives.
A partition is a contiguous set of blocks on a drive that are treated as an independent disk.

Boot loader – in order for bios to load an OS it looks for ins on the first sector of a hard drive,
where in resides the mbr and is where a boot loader is initialized, then the boot loader loads
the OS and is not used again
The BIOS performs the POST (Power on self test), the post performs some basic checks of
various hardware components, only if the post succeeds will the BIOS attempt to load the
boot loader (typically grub boot loader, older systems use linux loader (LILO), if there are
multiple OS installed, u can tell the boot loader which OS to use
The initial ram disk (initrd) is a temporary file system that’s loaded into memory when the
system boots
The file system can contain helpers that perform hardware detection and load the necessary
modules, sometimes called drivers, to get the actual file system mounted.
The kernel ring buffer contains messages related to the Linux kernel. A ring buffer is a data
structure that is always the same size. Once the buffer is completely full, old messages are
discarded when new messages arrive. To see the contents of the kernel ring buffer, use the
dmesg command.
After the bootstrap has loaded the kernel into memory, it relinquishes control to this system,
which initializes physical devices, virtual memory etc, the init process is started and starts all
other process, before handling control over init, the kernel will create any other kernel
threads
Sometimes, lilo is not able to boot the system, it prints the letters lilo on the screen, it can be
due to disk corrupted, etc, to solve it boot the system from bootable CD, or correct the
etc/lilo.conf

Startup flow control:

Linux uses runlevels to determine what processes and service to start

Historically run levels were controlled by the init program (in /etc/inittab) u could change the
default run level by going to this folder and editing the file, however modern systems use
systemd instead, systemd has the concept of targets, these targets are roughly equivalent to
runlevels eg runlevel5.target is a symlink to graphical.target
To change the default run level with systemd, use the systemctl command followed by set-
default

Changing run level/target:


-With the init command, u can change run levels using the telinit command eg telinit 5
-For systemd, use the systemctl command i.e systemctl isolate reboot.target

The format of the shutdown command is as follows:


Shutdown [options] time [message]
The option to tell a shutdown perform a reboot is -r, u can specify the shutdown using the
“HH:MM” format, you
You can also use +N where N represents the number of minutes to wait before performing
the action. Finally, you can use the now keyword to start immediately. Optionally, you may
specify a message that will be broadcast to all users logged into the system.
# shutdown -r now
To power off a system, use runlevel 0, the poweroff target, or the poweroff command
With init, u can do telinit 0 or init 0
Using systemctl, systemctl isolate poweroff.target
Finally, u can use the power off command, poweroff

Maintenance Mode
• When the system doesn't work because problems. The simplest and best solution is to
shut down the system to maintenance mode (run level 1) or reboot the system to single-user
mode (run level S) and try
to fix problems

LINUX COMMANDS
A command is a program which interacts with the kernel to provide the environment and
perform the functions called for by the user, it can be a built in shell comamnd, an
executable shell file (known as a shell script), or a source compiled, object file
The shell is a command line interpreter, the user interacts with the kernel through the shell,
u can use ascii text scripts to be acted upon by a shell
When you log into the system you are given a default shell.
When the shell starts up it reads its startup files and may set environment variables,
command search paths, and command aliases, and executes any commands specified in
these files
The default prompt for the Bourne shell is $ (or #, for
the root user).
Another popular shell is C Shell. The default prompt
for the C shell is %.

Shell programming – u can write shell programs by creating scripts containing a series of
shell comamnds, the first line of the script should start with #! Which indicates that the
script is directly executable
You immediately follow this with the name of the shell, or program (spaces are allowed), to
execute, using the full path name. So to set up a Bourne shell script the first line would be:
#! /bin/sh
You also need to specify that the script is executable by setting the proper bits on the file
with chmod, e.g.:
$ chmod +x shell_script
Multiple commands separated by ; can be executed one after the other

Help commands:
• To understand the working of the command and possible options use (man
command)
• Using the GNU Info System (info, info command)
• Listing a Description of a Program (whatis command)
• whoami –help (displays info about tool)
• Type command Indicate how a command name is interpreted
• which command Display which executable program will be executed
• The whereis command is used to locate the binary, source code, and manual page of
a command or program.
Many software packages installed on your system have documentation files residing in the
/usr/share/doc directory
Pipelines – the capability of commands to read data from standard input and send to
standard output

Wc means word count

Filters:
-Sort, sorts alphabetically
-wc, print line, word and byte counts
-uniq, omit repeated lines
-tail, look at the last 10 lines of the file
-head, look at the first 10 lines of the file
-grep, print lines matching a pattern

SUID:
This is a permission bit that allows users to execute a file with the permissions of the file's
owner.
Key use case: It’s often used for programs that require elevated permissions temporarily, like
passwd, which needs root access to change passwords
Ln command - used to create hard links(creating another name for the same file, both of
them point to the same file) and symbolic links (like shortcuts) eg ln file1.txt hardlink.txt, to
create a symbolic link add -s to the options
So symbolic link is like a shortcut, if u delete file1.txt the shortcut to it wont work
But when u delete one hard link the other can still remain, its like a copy of the data, which if
u edit also effects its other files
Touch – is used to change timestamp of a file or create new file
Stat – display file or file system status

File viewing and editing: Can be done with


-Emacs (Full screen editor)
-Pico (Simple text editor)
-Vi/Vim (Editor with a command mode and text mode, starts in comand mode)
-gedit (Gui text editor)

File compression, backing up and restoring:


compress - Compress data.
Uncompress - Expand data.
cpio - Can store files on tapes. to/from archives.
gzip - zip a file to a gz file.
gunzip - unzip a gz file.
zip – Compresses a file to a .zip file.
unzip – Uncompresses a file with .zip extension.

The tar command is used to archive files and directories into a single file or extract them
back.

-c is for create archive, -v is for show progress, and -f is for specify output file

Text processing:
Cat – view a file and concatenate files and print on the standard output
Cmp – compare two files
Cut – remove sections
Diff – show the differences between files
Echo – display a line of text

Badblocks – used to search a disk or partition for badblocks


Df – shows the disk free space on or more file systems
Du – shows how much disk space a directory and all of its files contain
Fsck – filesystem check, must not be run on a mounted file system
Sync – Synchronize data on disk with memory
Mount – used to mount a file system
Unmount – unmount a file system

Network management commands:


Dnsdomainname – shows the systems dns domain name
Hostname – used to show or set the name of ur machine for networking
arp This program lets the user read or modify their arp cache.
Dig - Sends a DNS query to get detailed information about a domain or IP address. Used for
debugging DNS issues.
Finger – Displays info about system users, such as login time, usernames and more
ftp – transfer files between ur system and remote system
ip – to show ur current ip address
ifconfig – configure a network interface
ifdown – brings a network interface down
ifup – brings a network interface up
ping – send internet requests
route – show or manipulate the IP routing table
wget – non interactive network downloader
ssh – secure login
telnet – setting up a remote session
exit – terminates the shell
halt – stop the system
init – process control initialization (init 3)
logout – log the user off the system
poweroff – brings the system down
reboot – reboot the system
runlevel – list the current and previous and runlevel
quota – displays users limits and current disk usage
su – single user login
users – print the usernames of users who are currently logged in
wall – sends a message to everybodys terminal
who – display the users logged in
whoami – print effective user id
ps – get status of one or more processes
U can combine the ps command with the grep comamnd, to form the pgrep command,
which prints the PID’s of commands that match the search pattern u supply to it

Ctrl + c – kill the foreground process


Ctrl + z – suspend the foreground process
Bg [%num] – background a suspended process
The top/free comamnd also dispalys memory utilization for a system

Scheduling repeated jobs with cron:


If u need to repeat a task on schedule, u can use the cron service, every min, the cron
service checks to see if there are any scheduled jobs to run and if so runs them. Cron jobs
are used to automate a process or perform routine maintenance
U can use the cron command
Crontab – a program to create, read, update and delete your job schedules

MANAGING USERS
Use of user accounts:
-Verify the identity of everyone in the system
-Permit the per-individual tailoring and access priveleges
/etc/passwd file contains the user ID, and the default home directory
To view the etc/passwd file use the less command, less /etc/passwd
The output is a bunch of rows, for eg one row can be:
Joe:x: 1000:1000: Joe Henderson:/home/joe:/bin/bash
The x above repr the password, the numbers are the UID and GID, next is comments,
followed by home directory, followed by shell dir
The first entry in the /etc/passwd file is the root account

/etc/shadow file contains the encrypted passwords and other password info, the file is
viewable by the root user only, to view it u can use su -root or tail /etc/shadow
This file also contains bunch of rows, its format is:
Username, Pass, Days since pass was changed, and more days stuff

Creating accounts:
Use the useradd command, it needs root priveleges so make sure u are either the root user
or using sudo, -c is a commend, -m is used to create the users home directory, and next is
the shell path`
useradd -c “Grant Stewart” -m -s/bin/bash grant
This creates a user but doesnt yet assign a password, to assign the password as root u can
use passwd grant (u will be asked to type the password and repeat it), a root can change
anyones password, for the user, just type passwd without the user ID

Suspending user account:


-Put * as start of password field in /etc/shadow
-Change login shell to /sbin/nologin or /etc/false
-Use GUI to suspend the user

Deleting accounts:
-U need to be a root user for this, userdel jsmith, if u want to remove the users home
directory as well use userdel -r jsmith
Modifying accounts:
-Use the usermod command, general format is usermod [options] username
In the options, u can have -c,-g (which is for primary grp), -G (for additional grps), -s
/path/to/shell
Eg usermod -g sales jsmith

Root account – has a UID of 0, dont login root as normal user, use su when needed, use sudo
for commands

Groups:
-The main file for grps is /etc/groups, the rows are in the format group name, password,
numeric ID, then a list of members seperated by a comma
-The first entry is the root group
-Password is used for priveleged grps, but rarely is a passowrd kept

Creating a group:
-Use the groupadd command, groupadd [options] group, in options u can keep -g GID, which
sets the group id to a specific number, if u dont keep this it automatically sets a random
number, but u can specify like below:
groupadd -g 2500 db

Deleting a group:
groupdel db

Updating groups:
groupdmod [options] group, in the options u can keep -g GID to replace the current id with
GID, u can also keep -n [New name] to change the name of the group

Assigning users to grps:


usermod -G AccountsPayable jsmith

Setting group permissions:


chgrp -R AccountsPayable AP
chgrp is used to change group ownership, -R means to all files recursively, AccountsPayable
is the name fo the group, and AP is the directory, so that means that AccountsPayable now
has ownership of directory AP and can now view and see it
To change permissions of grp use chmod command:
Chmod 775 AP

Backup and Restore:


-Use tar to make backup on a different disk or tape
-Backup can be scheduled using cron
Backup command: tar -zcvf <tar filename> <Directory tree to be backed up>
Restore command: tar -zxvf <tar filename> <file to be recovered>

Software package management:


Include.tar.gz, .rpm (for redhat, fedora), .deb (debian, ubuntu)
Meta package managers locate packages on the internet, download, install and analyze
inter-package depandancies, eg yum (rpm), apt-get (deb and rpm), so these ones are more
preferred as they also install the inner dependancies automatically, for the normal ones they
dont
Yum package manager commands:
Yum search x – searches for x
Yum install [-y] package – use the y option to automatically answer yes to all yum questions,
this command also requires super user previleges so u have to use sudo if not the super user
Yum remove package
Yum info package
The more manual rpm command can also be done but its very primitive and needs all
dependancies to be installed manually
Rpm -qa – list all installed packages
Rpm -qf /path/to/file – list the package that contains file
Rpm -ivh package.rpm – Install a package from the file named package.rpm
Rpm -ql package – list all the files that belong to the package
Rpm -e emacs – to uninstall/erase a software package
Long form of APT is advanced packaging tool, apt is compromised of few small utilities with
the most common being apt-cache and apt-get
Apt-cache search x – search for x
Apt-get install [-y] package
Apt-get remove package – removes package without the config files
Apt-get purge package – removes package with config files
Apt-cache show package – display info about package
The dpkg command is the manual version of apt
Dpkg -l – list all the installed packages
Dpkg -S /path/to/file – list the package that contains file
Dpkg -i package.deb – install a package from the file named package.deb
Dpkg -L package – list all the files that belong to a package

You might also like