Operating Systems and You: Becoming a Power User
This course has following submodules:
This course has following submodules:
In this, we’ll learn about:
The CLI interpreter on Linux is called a shell, and the language that we’ll use to interact with this shell is called Bash.
To list contents of C drive
ls C:\
To get help for specific command
Get-Help <command name>
ls
command,Get-Help ls
Get-Help ls-full
To see hidden files in a directory
ls -Force C:\
To list the contents of root directory
ls /
ls
command has very useful flags too.
To see available flags for ls
ls --help
man
shows the manual pages.
man <command>
To see hidden files, and long listing
ls -al
You can hide a file by prepending a .
in the start of the filename.
Similar to Windows command parameters, a flag is a way to specify additional options for a command.
An absolute path is one that starts from the main directory.
A Relative path is the path from your current directory.
To know where you are in the folder
pwd
To change the directory you’re in
cd <path\to\the\directory>
To go one level up
cd ..
Get to the $HOME directory
cd ~
To make a new directory
mkdir <directory name>
To make a directory with spaces in its name
mkdir 'directory name'
mkdir directory` name
To make a directory with spaces in its name
mkdir directory\ name
mkdir 'directory name'
To see the history of previous commands
history
To reverse-search through history, shortcut is <ctrl+r>
To clean PowerShell palette
clear
To copy a file
cp <Path\to\the\file\to\be\copied> <Path\to\the\directory\of\copying>
To copy multiple file at once, Wildcard is used
cp *.<common pattern> <path\to\where\copied>
To copy a directory and its content
cp <directory name> <Path\to\where\copied> -Recurse -Verbose
A character that’s used to help select files based on a certain pattern.
To copy a directory
cp <Directory/to/be/copied> <Path/where/to/be/copied>
To view the file contents
cat <File Name>
To view the file contents, one page at a time
more <File Name>
To see only part of the file contents
cat <File Name> -Head <Number of Lines>
To see only part of the file contents from the tail
cat <File Name> -Tail <Number of Lines>
To see file’s contents, interactively
less <File Name>
more
has been abandoned in favor of more useful less
command on Linux.
To see only part of a file’s contents, head
is used, which by default only shows first 10 lines
head <File Name>
To see only part of file’s contents, tail
is used, which by default only shows last 10 lines
tail <File Name>
To modify file’s contents from a CLI
start notepad++ <File Name>
PowerShell is a powerful and complex command line language.
To list directories, the real PowerShell command is can be found by:
Get-Alias ls
so, to list directories
Get-ChildItem <path\to\directory>
In GUI, Indexing Options applications are used.
In command-line, search is done as:
Select-String <Search String> <path\to\the\file>
To search in multiple files at once
Select-String <Search String> *.<file extension name>
-Filter
parameter is used with ls
so search for particular files in a directory.
-Filter
parameter will filter the results for file names that match a pattern.ls <path\to\the\file> -Recurse -Filter *.exe
To search in files
grep <Search String> <path/to/the/file>
To search through multiple files at once
grep <Search String> *.txt
echo hello_word > hello.py
The echo is an alias for PowerShell command Write-Output
.
Every Windows process and every PowerShell command can take input and can produce output. To do this, we use something called I/O streams or input output streams.
I/O streams are
The symbol > is something we call a Redirector operator that let us change where we want our stdout to go.
The symbol » is used to not create a new file, just append the stdout
echo 'Hello Planet' >> hello.py
|
Pipe operator is used to redirect the stdout of one command to stdin of another command.
cat hello.py | Select-String planet
To put new stdout to a new file.
cat hello.py | Select-String pla > planet.txt
If we don’t want to see error in CLI, to get them in a file
rm secure_file 2> error.txt
If we don’t care about error messages and don’t want to save them in a file, we can redirect them to a null variable (a black hole for stderr)
rm secure_file 2> $null
On Linux, stdin operator can be used via symbol <.
cat < SomeFile.py
To redirect error message to a file
ls /dir/fake_dir 2> error_output.txt
To filter out error message completely without saving
ls /dir/fake_dir 2> /dev/null
Used to help you do advance pattern-based selections.
One who is given access to a machine but has restricted access to do things like install software or change certain settings.
A user that has complete control over a machine.
A network of computers, users, files, etc. that are added to a central database.
A feature on Windows that prevents unauthorized changes to a system.
To check all users on the system and either admin access enabled or not.
Get-LocalUser
To get all the groups present on a local machine
Get-LocalGroup
To check members of an individual group
Get-LocalGroupMember Administrator
To see all groups, who are their members
cat /etc/group
sudo:x:27:user1, user2, user3
cat /etc/passwd
An admin shouldn’t know the password of the user using it.
But as an admin to manage users passwords, computer management application is used.
To change user’s password from CLI
net user <username> <password>
To interactively change the password
net user <username> *
To force user itself to change its password on next logon
net user <username> /logonpasswordchg:yes
To change a password on Linux
sudo passwd <username>
To force a user to change his/her password
sudo passwd -e <username>
To add users
net user <username> * /add
To add a new user and forcing him/her to change its password on new logon
net user <username> password /add /logonpasswordchg:yes
To remove a local user
net user <username> /del
OR
Remove-LocalUser <username>
To add a user
sudo useradd <username>
To remove a user
sudo userdel <username>
On Windows, files and directory permissions assigned using Access Control Lists or ACLs. Specifically, we’re going to be working with Discretionary Access Control Lists or DACLs.
Windows files and folders can also have System Access Control Lists or SACLs assigned to them.
Windows allow certain permissions to be set for files and folders.
The Read permission lets you see that a file exists, and allow you to read its contents. It also lets you read the files and directories in a directory.
The Read & Execute permission lets you read files, and if the file is an executable, you can run the file. Read & Execute includes Read, so if you select Read & Execute, Read will be automatically selected.
List folder contents is an alias for Read & Execute on a directory. Checking one will check the other. It means that you can read and execute files in that directory.
The Write permission lets you make changes to a file. It might be surprising to you, but you can have write access to a file without having read permission to that file!
The Modify permission is an umbrella permission that includes read, execute, and write.
A user or group with full control can do anything they want to the file! It includes all the permissions to Modify, and adds the ability to take ownership of a file and change its ALCs
To view file permissions in a CLI, Improved change ACLs command icacls
is used
icacls /? #icacls is a old dos command
icacls <filepath>
There are three different permissions you can have on Linux
To see file permissions
ls -l <filepath>
To modify permissions
icacls <filepath> /grant 'Everyone:(OI)(CI)(R)'
Everyone gives permissions to literally everyone of the computer including guest users, to avoid this
icacls <filepath> /grant 'Authenticated Users:(OI)(CI)(R)'
To remove permissions to everyone group
icacls <filepath> /remove Everyone
To see the given permissions
icacls <filepath>
This is a special type of user that’s allowed to use the computer without a password. Guest users are disabled by default. You might enable them in very specific situations.
The permissions are changed by chmod
command
The owner, which is denoted by a “u”
The group the file belongs to, which is denoted a “g”
Or other users, which is denoted by an “o”
To change execute permission
chmod u+x <filepath>
chmod u-x <filepath>
To add/remove multiple permissions to file
chmod u+rx <filepath>
To change permissions for owner, the group, and others
chmod ugo+r <filepath>
This format of changing permissions is called symbolic format.
Other method is changing permissions numerically, which is faster.
The numerical equivalent of rwx is:
To change permissions numerically
chmod 745 <filepath>
To change ownership of a file
sudo chown <username> <filepath>
To change group of a file
sudo chgrp <username> <filepath>
Simple permissions are actually sets of special, or specific permissions.
When you set the Read permission on a file, you’re actually setting multiple special permissions.
To see special permissions, icacls
command is used
icacls <filepath>
SetUID is a special permission, use to allow a file to be run as the owner of the file.
To apply SetUID
sudo chmod u+s <filepath>
The numerical value for SetUID is 4
sudo chmod 4755 <filepath>
SetGID is a special permission which allow a user to run a particular file in a group member though the user isn’t part of that group.
sudo chmod g+s <filepath>
The numerical value for SetGID is 2.
sudo chmod 2755 <filepath>
Sticky Bit is a special permission, use to allow anyone to write to a file or folder but can’t delete it.
sudo chmod +t <filepath>
The numerical value for Sticky bit is 1.
sudo chmod 1755 <filepath>
Contain instructions for a computer to execute when they’re run.
Used to guide a program called the Windows Installer in the installation, maintenance, and removal of programs on the Windows operating system.
Fedora use Red-Hat package manager package or (.RPM).
Debian uses .deb file.
To install a standalone .deb package
sudo dpkg -i abc.deb
To remove package on Debian
sudo dpkg -r abc.deb
To list .deb Packages
dpkg -l
A central, managed marketplace for app developers to publish and sell mobile apps.
Where you install mobile apps directly, without using an app store.
Mobile apps are standalone software packages, so they contain all their dependencies.
To compress files from CLI
Compress-Archive -Path <filepath: files to be compressed> <filepath: Where to save compressed file>
7-zip
is a popular Windows tools for archives management.Comprised of one or more files that’s compressed into a single file.
The core or source software files that are compressed into one file.
p7zip
is the Linux version of 7-zip.
7z
and the flag e for extract and then the file you want to extract.7z e <filepath>
C:\Windows\WinSxS
Find-Package
, can locate software, along with its dependencies, right from the command line.Counting on other pieces of software to make an application work, since one bit of code depends on another, in order to work.
A way to package a bunch of useful code that someone else wrote.
A name given to Windows PowerShell commands
Come with the works to make package installation and removal easier, including installing package dependencies.
“Makes sure that the process of software installation, removal, update, and dependency management is as easy and automatic as possible.”
Chocolatey
is a third party package manager for Windows.
NuGet is another third party package manager for Windows.
To add Chocolatey as a package source
Register-PackageSource -Name chocolatey -ProvideName Chocolatey -Location https://chocolatey.org/api/v2
To verify package source
Get-PackageSource
To find a package
Find-Package sysinternals -IncludeDependencies
To actually install this package
Install-Package -Name sysinternals
To verify installation
Get-Package -name sysinternals
To uninstall a package
Uninstall-Package -Name sysinternals
/etc/apt/sources.list
A Personal Package Archive or PPA is a software repo for uploading source packages to be built and published as an Advanced Packaging Tool (APT) repo by Launchpad.
Used to help our hardware devices interact with our OS.
/dev/
directory./dev/
directory, but not all of them are physical devices.ls
listing -
in the front represents file, and d
represents directory
, in /dev/
, c
shows block devices, and b
represents block devices.Like a keyboard or a mouse, transmit data character by character.
Like USB drives, hard drives and CDROMs, transfer blocks of data; a data block is just a unit of data storage.
Device nodes on Unix-like OSs, that don’t necessarily have to correspond to physical devices. I.e. /dev/null
, /dev/zero
, /dev/full
etc.
Software that’s meant to fix up a security hole.
For Ubuntu based distros
sudo apt update && sudo apt upgrade
To be on the latest security patches, you need to run and update newer kernels.
To see your kernel version
uname -r
-r
is a flag, to know kernel release, to know kernel version you have.
FAT32 reading and writing data to Windows, Linux, and macOS
The piece of a disk that you can manage.
Tells the OS how the disk is partitioned.
Diskpart
is used.Typing Diskpart
in the CLI, will open an interactive shell.
Next, type list disk
to list out all the storage devices on your computer
Then to select a disk:
select disk <Disk ID>
After to wipe all volumes and files from the disk, type clean
in the interactive shell.
To create blank partition in a disk
create partition primary
Then, to select the newly created partition
select partition 1
To mark it as active, simply type active
.
To format the disk with filesystem:
format FS=NTFS label=<Label the Disk> quick
Cluster (allocation unit size) is the minimum amount of space a file can take up in a volume or drive.
Cluster size is the smallest division of storage possible in a drive. Cluster size is important because a file will take up the entire size of the cluster, regardless of how much space it actually requires in the cluster.
For example, if the cluster size is 4kb (the default size for many formats and sizes) and the file you’re trying to store is 4.1kb, that file will take up 2 clusters. This means that the drive has effectively lost 3.9 Kb of space for use on a single file.
A single accessible storage area with a single file system; this can be across a single disk or multiple.
A logical division of a hard disk that can create unique spaces on a single drive. Generally used for allowing multiple operating systems.
Making something accessible to the computer, like filesystem or a hard disk.
parted
Can be used in both interactive and in command line.To list the devices
sudo parted -l
To run parted in interactive mode on some disk
sudo parted /dev/sdX
You can use help to see different commands used in the interactive mode.
To format the partition with filesystem using mkfs
sudo mkfs -t ext4 /dev/sdXx
To mount the previously formatted disk
sudo mount /dev/sdXx /my_disk/
To unmount the disk
sudo umount /dev/sdXx
To permanently mount a disk, we need to make changes in a fstab
file.
The fstab
configuration table consists of six columns containing the following parameters:
fsck
utility:
fsck
should not run a check on the filesystemfsck
command first.fsck
command after the root file system.Example of an fstab
table:
To get a UUID of a disk
sudo blkid
How our OS provides the physical memory available in our computer (like RAM) to the applications that run on the computer.
fdisk
, parted
, gparted
etc.fstab
file.On Linux, the dedicated area of the hard drive used for virtual memory.
NTFS uses Master File Table or MFT to represent the files.
Every file on the system has at least one entry on the MFT
Shortcut is an MFT entry which takes us to the specific location of a file, which it is a shortcut of.
Other methods to link to files are:
To create a symbolic link:
mklink <Symlink Name> <Original File Name>
To create a hard link:
mklink /H <Hard link Name> <Original File Name>
All the data, other than the file contents.
The NTFS file system contains a file called the master file table or MFT, There is at least one entry in the MFT for every file on an NTFS file system volume, including the MFT itself.
In Linux, metadata and files organize into a structure called an inode.
Inode
doesn’t store filename and the file data.
We store inodes in an inode table, and they help us manage the files on our file system.
Shortcut on Linux, referred to as Softlink.
To create a soft link:
ln -s <File Name> <Softlink Name>
To create a hard link:
ln <File Name> <Hardlink Name>
If you move a file, all the Softlinks, will be broken
To check disk usage, open up, computer management utility.
Disk cleanup is done through CleanManager.exe, to clear out, cache, log file, temporary files, and old file etc.
Another disk health feature is Defragmentation.
For Solid state drives, the system can use the Trim feature to reclaim unused space.
For CLI, disk cleanup du
tool is used
The idea behind disk defragmentation is to take all the files stored on a given disk, and reorganize them into neighboring locations.
To see disk usage:
du -h
du
List file sizes of current directory if no option is specified.
To see free disk space:
df -h
Ejecting a USB drive is necessary, as the file copying/moving might still be running in the background, even after successful copy/move prompt.
When we read or write something to a drive, we actually put it into a buffer, or cache, first.
If you don’t give enough time for data to be moved away from buffer, you may experience a Data corruption.
Power outage, system failure, or some bug in the OS or the program, can also cause data corruption.
NTFS has some advanced feature in the form of Data journaling, which avoid data corruption or even attempts data recovery in case of failure.
Minor errors and data corruptions are self healed by NTFS.
To check self-heal status:
fsutill repair query C:
In case of catastrophic failure, run chkdsk
tool in PowerShell as an admin, by default it will run in read-only mode. So it will only report the error, and not fix it.
chkdsk
To fix the errors
chkdsk /F <Drive Path>
Most of the time, you won’t need to run chkdsk
manually, and OS will handle it for you running it, and then fixing the errors, by looking the at the NTFS Journaling log.
A region of RAM that’s used to temporarily store data while it’s being moved around.
Run fsck
on unmounted drive, otherwise it will damage it.
sudo fsck /dev/sdX
On some systems, fsck
runs automatically on boot.
Allows us to manage multiple machines from anywhere in the world.
A protocol implemented by other programs to securely access one computer from another.
Allows you to connect to a private network, like your work network, over the Internet.
A free, open source software that you can use to make remote connections through several network protocols, including SSH.
To connect via PuTTY in a CLI:
putty.exe -ssh username@ip_address <Port Number> # Port number is 22 by default for SSH connections
To enable remote connection on a pc go-to:
MY PC > Properties > Remote Settings
A command you can use on Linux to copy files between computers on a network.
To copy file from local computer to remote:
scp <filepath> username@ip_address:location
PuTTY comes with PuTTY Secure Copy Client or pscp.exe.
pscp.exe <filepath> username@ip_address:location
To transfer files via PuTTY is a little time-consuming, so Windows came up with the concept of ShareFolders.
To share folders via CLI:
net share <ShareName>=<drive>:<DirectoryPath> /grant:everyone,full
To list currently shared folders on your computer:
net share
A single virtual machine.
A log is a system diary of events happening on the system.
The act of creating log events.
It stores all the events happening on a Windows computer.
/var/log
directory./var/log/syslog
The logs are written in a very standard way, so we don’t need to go through each and every bit of them to troubleshoot problems, all you need to do is look for specific things.
To see real-time logs on Linux:
tail -f /var/log/syslog
Disk cloning tools are used to obtain an image of a computer OS and settings. Some tools are:
Different disk cloning tools offer different methods to clone systems
Let’s use Linux CLI tool dd
to copy files from a disk to make a clone.
To copy from a USB drive, first unmount it:
sudo umount /dev/sdX
Then run dd
:
sudo dd if=/dev/sdX of=~/Desktop/my_usb_image.img bs=100M
The applications that we can run, like the Chrome web browser.
Programs that are running.
When Windows boots up or starts, the first non-kernel user mode that starts is the Session Manager Subsystem or smss.exe.
smss.exe
starts winlogon.exe
along with client/server runtime subsystem or csrss.exe, which handles GUI and CLI.To terminate a process from CLI, taskkill
utility is used, which can find and halt a process.
taskkill
Uses PID to identify the process running.To kill notepad with taskkill
from CLI:
taskkill /pid 5856
To forcefully kill a rogue process:
taskkill /F /PID <PID>
init
Is the parent process for the kernelWhen you start up your computer, the kernel creates a process called init, which has a PID of 1.
Task Manager or taskmgr.exe is one way of obtaining processes information on Windows.
To show all running processes in CLI:
tasklist
The PowerShell command for the same:
Get-Process
To see process running on Linux:
ps -x
The following STAT for ps -x
command are used to show processes current status
To see full list of running processes, even by other users run:
ps -ef #'f' for full
Everything in a Linux is a file, even the processes. So we can view them in /proc
.
ls -l /proc
If we want to close an unresponsive process, we use signals.
The most common is SIGINT or signal interrupt. You can send this signal to a running process with the CTRL+C key combination.
A way to tell a process that something’s just happened.
There are lots of signals on Linux, starting with SIG. I.e. SIGTERM, SIGINT** etc.
A utility, Microsoft created to let IT Support Specialists, system admins and other users to look at running processes.
To terminate a process, kill
command is used.
kill
Without any parameters, sends SIGINT
signal to the program/process to clean is running processes and close them properlyTo kill a process
kill <PID>
To send SIGKILL via kill
command:
kill -KILL <PID>
-KILL
Should the lost resort to stop a process, it doesn’t give time to the process for cleanup, it may cause more harm than good.
To put process on pause instead of killing, SIGSTP or signal stop, is used
kill -TSTP <PID>
To resume from suspend:
kill -CONT <PID>
Resource Monitoring tool is used.
To get resource monitoring from CLI
Get-Process
To get the three most resource heavy processes:
Get-Process | Sort CPU -descending | Select -first 3 -Property ID,ProcessName,CPU
top
is a useful resource monitoring CLI tool:
top
Another useful CLI tool is uptime
, which show info about the current time, how long your computer running, how many users are logged on, and what the load average of your machine is.
When ejecting a USB drive, you get the error “Device or resource busy” though none of the files on the USB drive is in use or opened anywhere, or so you think. Using the lsof
lists open files and what processes are using them.