Escape From Linux
Escape From Linux
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
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
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
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
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
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
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
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
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
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
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