Linux
Notes for Professionals
Linux®
Notes for Professionals
50+ pages
of professional hints and tricks
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial Linux® group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
...................................................................................................................................................................................
1
Chapter 1: Getting started with GNU/Linux
.....................................................................................................
2
..........................................................................................................................................
..................................................................................................................
.................................................................................................................................................
....................................................................................................................................
Section 1.1: Useful shortcuts
Section 1.2: File Management Commands
Section 1.3: Hello World
Section 1.4: Basic Linux Utilities
Section 1.5: Searching for files by patterns in name/contents
Section 1.6: File Manipulation
Section 1.7: File/Directory details
........................................................................................................................................
.................................................................................................................................
.................................................................................
6
Chapter 2: Detecting Linux distribution name and version
..................................................................
11
Section 2.1: Detect what debian-based distribution you are working in
Section 2.2: Detect what systemd-based distribution you are using
Section 2.3: Detect what RHEL / CentOS / Fedora distribution you are working in
Section 2.4: Uname - Print information about the current system
Section 2.5: Detect basic information about your distro
Section 2.6: Using GNU coreutils
Section 2.7: Find your linux os (both debian & rpm) name and release number
...............................................................
....................................................................
............................................
12
........................................................................
........................................................................................
................................................................................................................................
................................................
Chapter 3: Getting information on a running Linux kernel
...................................................................
15
Section 3.1: Getting details of Linux kernel
................................................................................................................
15
Chapter 4: Shell
...........................................................................................................................................................
16
Section 4.1: Changing default shell
Section 4.2: Basic Shell Utilities
Section 4.3: Create Your Own Command Alias
Section 4.4: Locate a file on your system
............................................................................................................................
..................................................................................................................................
........................................................................................................
18
.................................................................................................................
18
Chapter 5: Check Disk Space
................................................................................................................................
19
Section 5.1: Investigate Directories For Disk Usage
Section 5.2: Checking Disk Space
.................................................................................................
19
..............................................................................................................................
21
Chapter 6: Getting System Information
.........................................................................................................
23
Section 6.1: Statistics about CPU, Memory, Network and Disk (I/O operations)
Section 6.2: Using tools like lscpu and lshw
Section 6.3: List Hardware
Section 6.4: Find CPU model/speed information
Section 6.5: Process monitoring and information gathering
..............................................................................................................
..........................................................................................................................................
.....................................................................................................
..................................................................................
..................................................
23
Chapter 7: ls command
...........................................................................................................................................
28
Section 7.1: Options for ls command
Section 7.2: ls command with most used options
.........................................................................................................................
28
....................................................................................................
28
Chapter 8: File Compression with 'tar' command
......................................................................................
30
Section 8.1: Compress a folder
Section 8.2: Extract a folder from an archive
Section 8.3: List contents of an archive
Section 8.4: List archive content
Section 8.5: Compress and exclude one or multiple folder
Section 8.6: Strip leading components
...................................................................................................................................
..........................................................................................................
....................................................................................................................
................................................................................................................................
......................................................................................................................
31
....................................................................................
31
Chapter 9: Services
....................................................................................................................................................
32
Section 9.1: List running service on Ubuntu
Section 9.2: Systemd service management
..............................................................................................................
32
.............................................................................................................
32
2
3
5
5
7
8
11
11
13
13
13
14
16
17
23
24
25
26
Publicité
30
30
30
31
Chapter 10: Managing Services
...........................................................................................................................
33
Section 10.1: Diagnosing a problem with a service
Section 10.2: Starting and Stopping Services
Section 10.3: Getting the status of a service
..................................................................................................
33
...........................................................................................................
.............................................................................................................
Chapter 11: Modifying Users
..................................................................................................................................
35
Section 11.1: Setting your own password
Section 11.2: Setting another user's password
Section 11.3: Adding a user
Section 11.4: Removing a user
Section 11.5: Removing a user and its home folder
Section 11.6: Listing groups the current user is in
Section 11.7: Listing groups a user is in
...................................................................................................................
..........................................................................................................
..........................................................................................................................................
....................................................................................................................................
.................................................................................................
35
.....................................................................................................
......................................................................................................................
Chapter 12: LAMP Stack
...........................................................................................................................................
36
Section 12.1: Installing LAMP on Arch Linux
Section 12.2: Installing LAMP on Ubuntu
Section 12.3: Installing LAMP stack on CentoOS
...............................................................................................................
...................................................................................................................
.......................................................................................................
38
Chapter 13: tee command
......................................................................................................................................
40
Section 13.1: Write output to stdout, and also to a file
Section 13.2: Write output from the middle of a pipe chain to a file and pass it back to the pipe
Section 13.3: write the output to multiple files
Section 13.4: Instruct tee command to append to the file
..........................................................................................................
.......................................................................................
.............................................................................................
40
.....................
40
Chapter 14: Secure Shell (SSH)
............................................................................................................................
42
Section 14.1: Connecting to a remote server
Section 14.2: Installing OpenSSH suite
Section 14.3: Configuring an SSH server to accept connections
Section 14.4: Passwordless connection (using a key pair)
Section 14.5: Generate public and private key
Section 14.6: Disable ssh service
.............................................................................................................
.......................................................................................................................
............................................................................
43
......................................................................................
.........................................................................................................
................................................................................................................................
Chapter 15: SCP
............................................................................................................................................................
45
Section 15.1: Secure Copy
Section 15.2: Basic Usage
............................................................................................................................................
45
...........................................................................................................................................
45
Chapter 16: GnuPG (GPG)
........................................................................................................................................
46
Section 16.1: Exporting your public key
Section 16.2: Create and use a GnuPG key quickly
......................................................................................................................
46
..................................................................................................
46
..................................................................................................................
..............................................................................................................................
..................................................................................................................
........................................................................
Chapter 17: Network Configuration
Section 17.1: Local DNS resolution
Section 17.2: Configure DNS servers for domain name resolution
Section 17.3: See and manipulate routes
Section 17.4: Configure a hostname for some other system on your network
Section 17.5: Interface details
Section 17.6: Adding IP to an interface
Chapter 18: Midnight Commander
.....................................................................................................................................
......................................................................................................................
.....................................................................................................................
....................................................
48
Section 18.1: Midnight Commander function keys in browsing mode
Section 18.2: Midnight Commander function keys in file editing mode
....................................................................
.................................................................
52
Chapter 19: Change root (chroot)
......................................................................................................................
54
Section 19.1: Requirements
Section 19.2: Manually changing root in a directory
Section 19.3: Reasons to use chroot
Chapter 20: Package Managers
...............................................................................................
54
..........................................................................................................................
..........................................................................................................................
.........................................................................................................................................
54
33
34
35
35
35
35
35
35
36
37
40
40
42
42
43
43
43
47
47
47
47
49
50
52
Publicité
52
55
56
Section 20.1: How to update packages with the apt package manager
Section 20.2: How to install a package with the pacman package manager
Section 20.3: How to update packages with the pacman package manager
Section 20.4: How to update packages with yum
..............................................................
......................................................
.....................................................
56
56
56
...................................................................................................
57
Chapter 21: Compiling the Linux kernel
...........................................................................................................
58
Section 21.1: Compilation of Linux Kernel on Ubuntu
...............................................................................................
58
Credits
..............................................................................................................................................................................
59
You may also like
........................................................................................................................................................
61
About
Please feel free to share this PDF with anyone for free,
latest version of this book can be downloaded from:
https://goalkicker.com/LinuxBook
This Linux® Notes for Professionals book is compiled from Stack Overflow
Documentation, the content is written by the beautiful people at Stack Overflow.
Text content is released under Creative Commons BY-SA, see credits at the end
of this book whom contributed to the various chapters. Images may be copyright
of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not
affiliated with official Linux® group(s) or company(s) nor Stack Overflow. All
trademarks and registered trademarks are the property of their respective
company owners
The information presented in this book is not guaranteed to be correct nor
accurate, use at your own risk
Please send feedback and corrections to [email protected]
GoalKicker.com – Linux® Notes for Professionals
1
Chapter 1: Getting started with GNU/Linux
Section 1.1: Useful shortcuts
Using The Terminal
The examples in this document assume that you are using a POSIX-compliant (such as bash, sh, zsh, ksh)
shell.
Large portions of GNU/Linux functionality are achieved using the terminal. Most distributions of Linux include
terminal emulators that allow users to interact with a shell from their desktop environment. A shell is a command-
line interpreter that executes user inputted commands. Bash (Bourne Again SHell) is a common default shell
among many Linux distributions and is the default shell for macOS.
These shortcuts will work if you are using Bash with the emacs keybindings (set by default):
Open terminal
Ctrl + Alt + T or Super + T
Cursor movement
Ctrl + A Go to the beginning of the line you are currently typing on.
Ctrl + E Go to the end of the line you are currently typing on.
Ctrl + XX Move between the beginning of the line and the current position of the cursor.
Alt + F Move cursor forward one word on the current line.
Alt + B Move cursor backward one word on the current line.
Ctrl + F Move cursor forward one character on the current line.
Ctrl + B Move cursor backward one character on the current line.
Text manipulation
Ctrl + U Cut the line from the current position to the beginning of the line, adding it to the clipboard. If
you are at the end of the line, cut the entire line.
Ctrl + K Cut the line from the current position to the end of the line, adding it to the clipboard. If you
are at the beginning of the line, cut the entire line.
Ctrl + W Delete the word before the cursor, adding it to the clipboard.
Ctrl + Y Paste the last thing from the clipboard that you cut recently (undo the last delete at the
current cursor position).
Alt + T Swap the last two words before the cursor.
Alt + L Make lowercase from cursor to end of word.
Alt + U Make uppercase from cursor to end of word.
Alt + C Capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word).
Alt + D Delete to end of word starting at cursor (whole word if cursor is at the beginning of word).
Alt + . Prints the last word written in previous command.
Ctrl + T Swap the last two characters before the cursor.
History access
Ctrl + R Lets you search through previously used commands.
Ctrl + G Leave history searching mode without running a command.
Ctrl + J Lets you copy current matched command to command line without running it, allowing you to
GoalKicker.com – Linux® Notes for Professionals
2
make modifications before running the command.
Alt + R Revert any changes to a command you’ve pulled from your history, if you’ve edited it.
Ctrl + P Shows last executed command, i.e. walk back through the command history (Similar to up
arrow).
Ctrl + N Shows next executed command, i.e. walk forward through the command history (Similar to
down arrow).
Terminal control
Ctrl + L Clears the screen, similar to the clear command.
Ctrl + S Stop all output to the screen. This is useful when running commands with lots of long output.
But this doesn't stop the running command.
Ctrl + Q Resume output to the screen after stopping it with Ctrl+S.
Ctrl + C End currently running process and return the prompt.
Ctrl + D Log out of the current shell session, similar to the exit or logout command. In some commands,
acts as End of File signal to indicate that a file end has been reached.
Ctrl + Z Suspends (pause) currently running foreground process, which returns shell prompt. You can
then use bg command allowing that process to run in the background. To again bring that process to
foreground, use fg command. To view all background processes, use jobs command.
Tab Auto-complete files and directory names.
Tab Tab Shows all possibilities, when typed characters doesn't uniquely match to a file or directory
name.
Special characters
Ctrl + H Same as Backspace.
Ctrl + J Same as Return (historically Line Feed).
Ctrl + M Same as Return (historically Carriage Return).
Ctrl + I Same as Tab.
Ctrl + G Bell Character.
Ctrl + @ Null Character.
Esc Deadkey equivalent to the Alt modifier.
Close Terminal
Ctrl + Shift + W To close terminal tab.
Ctrl + Shift + Q To close entire terminal.
Alternatively, you can switch to the vi keybindings in bash using set -o vi. Use set -o emacs to switch back to the
emacs keybindings.
Section 1.2: File Management Commands
Linux uses some conventions for present and parent directories. This can be a little confusing for beginners.
Whenever you are in a terminal in Linux, you will be in what is called the current working directory. Often your
command prompt will display either the full working directory, or just the last part of that directory. Your prompt
could look like one of the following:
user@host ~/somedir $
user@host somedir $
user@host /home/user/somedir $
which says that your current working directory is /home/user/somedir.
GoalKicker.com – Linux® Notes for Professionals
3
In Linux .. represents the parent directory and . represents the current directory.
Therefore, if the current directory is /home/user/somedir, then cd ../somedir will not change the working
directory.
The table below lists some of the most used file management commands
Directory navigation
Command
Utility
pwd
cd -
Get the full path of the current working directory.
Navigate to the last directory you were working in.
cd ~ or just cd Navigate to the current user's home directory.
cd ..
Go to the parent directory of current directory (mind the space between cd and ..)
Listing files inside a directory
Command
ls -l
Utility
Publicité
List the files and directories in the current directory in long (table) format (It is recommended to
use -l with ls for better readability).
ls -ld dir-name List information about the directory dir-name instead of its contents.
ls -a
ls -F
ls -lt
ls -lh
ls -lR
tree
List all the files including the hidden ones (File names starting with a . are hidden files in Linux).
Appends a symbol at the end of a file name to indicate its type (* means executable, / means
directory, @ means symbolic link, = means socket, | means named pipe, > means door).
List the files sorted by last modified time with most recently modified files showing at the top
(remember -l option provides the long format which has better readability).
List the file sizes in human readable format.
Shows all subdirectories recursively.
Will generate a tree representation of the file system starting from the current directory.
File/directory create, copy and remove
Command
Utility
cp -p source destination
Will copy the file from source to destination. -p stands for preservation. It
preserves the original attributes of file while copying like file owner, timestamp,
group, permissions etc.
cp -R source_dir
destination_dir
mv file1 file2
rm -i filename
Will copy source directory to specified destination recursively.
In Linux there is no rename command as such. Hence mv moves/renames the
file1 to file2.
Asks you before every file removal for confirmation. IF YOU ARE A NEW USER
TO LINUX COMMAND LINE, YOU SHOULD ALWAYS USE rm -i. You can specify
multiple files.
rm -R dir-name
Will remove the directory dir-name recursively.
rm -rf dir-name
rmdir dir-name
Will remove the directory dir recursively, ignoring non-existent files and will
never prompt for anything. BE CAREFUL USING THIS COMMAND! You can
specify multiple directories.
Will remove the directory dir-name, if it's empty. This command can only remove
empty directories.
mkdir dir-name
Create a directory dir-name.
mkdir -p dir-name/dir-name
Create a directory hierarchy. Create parent directories as needed, if they don't
exist. You can specify multiple directories.
touch filename
Create a file filename, if it doesn't exist, otherwise change the timestamp of the
file to current time.
File/directory permissions and groups
Command
Utility
GoalKicker.com – Linux® Notes for Professionals
4
chmod <specification> filename
Change the file permissions. Specifications = u user, g group, o other, + add
permission, - remove, r read, w write,x execute.
chmod -R <specification> dir-
name
Change the permissions of a directory recursively. To change permission of
a directory and everything within that directory, use this command.
chmod go=+r myfile
chmod a +rwx myfile
chmod go -r myfile
Add read permission for the owner and the group.
Allow all users to read, write or execute myfile.
Remove read permission from the group and others.
chown owner1 filename
Change ownership of a file to user owner1.
chgrp grp_owner filename
Change primary group ownership of file filename to group grp_owner.
chgrp -R grp_owner dir-name
Change primary group ownership of directory dir-name to group grp_owner
recursively. To change group ownership of a directory and everything within
that directory, use this command.
Section 1.3: Hello World
Type the following code into your terminal, then press Enter :
echo "Hello World"
This will produce the following output:
Hello World
Section 1.4: Basic Linux Utilities
Linux has a command for almost any tasks and most of them are intuitive and easily interpreted.
Getting Help in Linux
Command
Usability
man <name>
Read the manual page of <name>.
man <section> <name>
Read the manual page of <name>, related to the given section.
man -k <editor>
man -K <keyword>
apropos <editor>
help
help <name>
info <name>
dpkg -l
Output all the software whose man pages contain <editor> keyword.
Outputs all man pages containing <keyword> within them.
Output all the applications whose one line description matches the word editor.
When not able to recall the name of the application, use this command.
In Bash shell, this will display the list of all available bash commands.
In Bash shell, this will display the info about the <name> bash command.
View all the information about <name>.
Output a list of all installed packages on a Debian-based system.
dpkg -L packageName
Will list out the files installed and path details for a given package on Debian.
dpkg -l | grep -i <edit>
Return all .deb installed packages with <edit> irrespective of cases.
less /var/lib/dpkg/available Return descriptions of all available packages.
whatis vim
List a one-line description of vim.
<command-name> --help
Display usage information about the <tool-name>. Sometimes command -h also
works, but not for all commands.
User identification and who is who in Linux world
Command
hostname
Display hostname of the system.
Usability
GoalKicker.com – Linux® Notes for Professionals
5
hostname -f Displays Fully Qualified Domain Name (FQDN) of the system.
passwd
whoami
who
w
last
Change password of current user.
Username of the users logged in at the terminal.
List of all the users currently logged in as a user.
Display current system status, time, duration, list of users currently logged in on system and other
user information.
Who recently used the system.
last root When was the last time root logged in as user.
lastb
chmod
Shows all bad login attempts into the system.
Changing permissions - read,write,execute of a file or directory.
Process related information
Command
top
ps
List all processes sorted by their current system resource usage. Displays a continually updated
display of processes (By default 3 seconds). Use q key to exit top.
List processes currently running on current shell session
Usability
Publicité
ps -u root List all of the processes and commands root is running
ps aux
List all the processes by all users on the current system
Section 1.5: Searching for files by patterns in name/contents
A common and task of someone using the Linux Command Line (shell) is to search for files/directories with a
certain name or containing certain text. There are 2 commands you should familiarise yourself with in order to
accomplish this:
Find files by name
find /var/www -name '*.css'
This will print out the full path/filename to all files under /var/www that end in .css. Example output:
/var/www/html/text-cursor.css
/var/www/html/style.css
For more info:
man find
Find files containing text
grep font /var/www/html/style.css
This will print all lines containing the pattern font in the specified file. Example output:
font-weight: bold;
font-family: monospace;
Another example:
grep font /var/www/html/
GoalKicker.com – Linux® Notes for Professionals
6
This doesn't work as you'd hoped. You get:
grep: /var/www/html/: Is a directory
You need to grep recursively to make it work, using the -R option:
grep -R font /var/www/html/
Hey nice! Check out the output of this one:
/var/www/html/admin/index.php: echo '<font color=red><b>Error: no dice</b></font><br/>';
/var/www/html/admin/index.php: echo '<font color=red><b>Error: try again</b></font><br/>';
/var/www/html/style.css: font-weight: bold;
/var/www/html/style.css: font-family: monospace;
Notice that when grep is matching multiple files, it prefixes the matched lines with the filenames. You can use the -
h option to get rid of that, if you want.
For more info:
man grep
Section 1.6: File Manipulation
Files and directories (another name for folders) are at the heart of Linux, so being able to create, view, move, and
delete them from the command line is very important and quite powerful. These file manipulation commands allow
you to perform the same tasks that a graphical file explorer would perform.
Create an empty text file called myFile:
touch myFile
Rename myFile to myFirstFile:
mv myFile myFirstFile
View the contents of a file:
cat myFirstFile
View the content of a file with pager (one screenful at a time):
less myFirstFile
View the first several lines of a file:
head myFirstFile
View the last several lines of a file:
tail myFirstFile
Edit a file:
GoalKicker.com – Linux® Notes for Professionals
7
vi myFirstFile
See what files are in your current working directory:
ls
Create an empty directory called myFirstDirectory:
mkdir myFirstDirectory
Create multi path directory: (creates two directories, src and myFirstDirectory)
mkdir -p src/myFirstDirectory
Move the file into the directory:
mv myFirstFile myFirstDirectory/
You can also rename the file:
user@linux-computer:~$ mv myFirstFile secondFileName
Change the current working directory to myFirstDirectory:
cd myFirstDirectory
Delete a file:
rm myFirstFile
Move into the parent directory (which is represented as ..):
cd ..
Delete an empty directory:
rmdir myFirstDirectory
Delete a non-empty directory (i.e. contains files and/or other directories):
rm -rf myFirstDirectory
Make note that when deleting directories, that you delete ./ not / that will wipe your whole filesystem.
Section 1.7: File/Directory details
The ls command has several options that can be used together to show more information.
Details/Rights
The l option shows the file permissions, size, and last modified date. So if the root directory contained a dir called
test and a file someFile the command:
GoalKicker.com – Linux® Notes for Professionals
8
user@linux-computer:~$ ls -l
Would output something like
-rw-r--r-- 1 user users 70 Jul 22 13:36 someFile.txt
drwxrwxrwx 2 user users 4096 Jul 21 07:18 test
The permissions are in format of drwxrwxrwx. The first character represents the file type d if it's a directory -
otherwise. The next three rwx are the permissions the user has over the file, the next three are the permissions the
group has over the file, and the last three are the permissions everyone else has over the file.
The r of rwx stands for if a file can be read, the w represents if the file can be modified, and the x stands for if the
file can be executed. If any permission isn't granted a - will be in place of r, w, or x.
So from above user can read and modify someFile.txt but the group has only read-only rights.
To change rights you can use the chmod ### fileName command if you have sudo rights. r is represented by a
value of 4, w is represented by 2, and x is represented by a 1. So if only you want to be able to modify the contents
to the test directory
Owner rwx = 4+2+1 = 7
Group r-x = 4+0+1 = 5
Other r-x = 4+0+1 = 5
So the whole command is
chmod 755 test
Now doing a ls -l would show something like
drwxr-xr-x 2 user users 4096 Jul 21 07:20 test
Readable Size
Used in conjunction with the l option the h option shows file sizes that are human readable. Running
user@linux-computer:~$ ls -lh
Would output:
total 4166
-rw-r--r-- 1 user users 70 Jul 22 13:36 someFile.txt
drwxrwxrwx 2 user users 4.0K Jul 21 07:18 test
Hidden
To view hidden files use the a option. For example
user@linux-computer:~$ ls -a
Might list
GoalKicker.com – Linux® Notes for Professionals
9
.profile
someFile.txt
test
Total Directory Size
To view the size of the current directory use the s option (the h option can also be used to make the size more
readable).
user@linux-computer:~$ ls -s
Outputs
total 4166
someFile.txt test
Recursive View
Lets say test directory had a file anotherFile and you wanted to see it from the root folder, you could use the R
option which would list the recursive tree.
user@linux-computer:~$ ls -R
Outputs
.:
someFile.txt test
./test:
anotherFile
GoalKicker.com – Linux® Notes for Professionals
10
Chapter 2: Detecting Linux distribution
name and version
Section 2.1: Detect what debian-based distribution you are
working in
Just execute lsb_release -a.
On Debian:
$ lsb_release -a
<...