Linux Administration
Linux Administration
Linux Administration
# pwd
# df -h
# ls
# ls -l
# ls -lhtra
# touch
# rm
# cp
# mv
# cat
Standard Output
Standard Error
# ls %
Redirection
Overwrite:
Append:
Standard input: 'less than symbol''less than symbol' (line by line input)
Examples:
Standard output:
# cat test.txt
# cat text.txt
# cat test.txt
Pipe:
less:
# ls -lhtra | less
find:
grep:
tee:
# cat test2.txt
tr:
# cat test2.txt | tr a A
wc:
number of letters:
# cat test2.txt | wc -l
number of words:
^ with [ ] : The pattern must not contain any character in the set specified
Use ^: The pattern following it must occur at the beginning of each line:
Use ^ with [ ]: The pattern must not contain any character in the set specified:
Use $: The pattern preceding it must occur at the end of each line:
Use \ (backslash): Ignores the special meaning of the character following it:
# whereis ssh
# which ssh
# ssh
# man ssh
# ssh username@ip/hostname/dns-name
# whoami
# exit
# ls -lhtra
# cat ssh-output.log
# runlevel
or
# systemctl get-default
Overview of 6 runlevels.
# useradd user1
# su - user1
Change to root:
# su -
# whoami
# chage -l user1
# chage user1
# sudo command
Archive, compress, unpack, and uncompress files using tar, star, gzip, and bzip2
Gzip and Bzip2:
Compress:
# gzip file
# bzip2 file
Extract:
# gzip -d file
# bzip2 -d file
Tar:
Compress:
List Contents:
Extract:
Star:
Install Star:
Create Archive:
List Contents:
# star -t -f=compressed-file.star
Extract:
# star -x -f=compressed-file.star
# nano filename
Nano is a mode-less editor so you can start typing immediately to insert text. If you are editing a
configuration file like /etc/fstab use the -w switch to disable wrapping on long lines as it might
render the configuration file unparseable by whatever tools depend on it. For example:
# nano -w /etc/fstab
It is very, very important that you use the -w switch when opening a config file. Failure to do so may
keep your system from booting or cause other bad things.
If you want to save the changes you've made, press Ctrl + O. To exit nano, type Ctrl + X. If you ask
nano to exit from a modified file, it will ask you if you want to save it. Just press N in case you don't,
or Y in case you do. It will then ask you for a filename. Just type it in and press Enter.
If you accidentally confirmed that you want to save the file but you actually don't, you can always
cancel by pressing Ctrl + C when you're prompted for a filename.
To cut a single line, you use Ctrl + K (hold down Ctrl and then press K). The line disappears. To paste
it, you simply move the cursor to where you want to paste it and punch Ctrl + U. The line reappears.
To move multiple lines, simply cut them with several Ctrl + K in a row, then paste them with a single
Ctrl + U. The whole paragraph appears wherever you want it.
If you need a little more fine-grained control, then you have to mark the text. Move the cursor to the
beginning of the text you want to cut. Hit Ctrl + 6 (or Alt + A). Now move your cursor to the end of
the text you want to cut: the marked text gets highlighted. If you need to cancel your text marking,
simply hit Ctrl + 6 again. Press Ctrl + K to cut the marked text. Use Ctrl + U to paste it.
Searching for a string is easy as long as you think "WhereIs" instead of "Search". Simply hit Ctrl + W,
type in your search string, and press Enter. To search for the same string again, hit Alt + W.
Note:
In nano's help texts the Ctrl is represented by a caret (^), so Ctrl + W is shown as ^W, and so on. The
Alt key is represented by an M (from "Meta"), so Alt + W is shown as M-W.
Create and edit text files | Part 2 | VI
Open VI:
# vi filename
# vi newfile
To save the edits you have made, but leave vi running and your file open:
Press Esc
Type :w
Press Return
To quit vi, and discard any changes your have made since last saving:
Press Esc
Type :q!
Press Return
Entering text:
k - up one line
x - delete character
nx - delete n characters
dw - delete word
dd - delete line
cw - replace a word
$ - end of line
0 - beginning of line
:w - save file
# touch file
# vi file
# nano file
Make directory:
# mkdir directory
# pwd
Make directory and parent directories:
# mkdir -p directory1/directory2
Remove file:
# rm file
# rm -d directory
# rm -r directory
Copy file:
# cp file-to-copy new-file
# cp -r directory-to-copy new-directory
Move file:
# mv file-to-move new-file
Move directory:
# mv directory-to-move new-directory
# ls -lia
# ln original-file hard-link-name
Create softlink:
# ln -s original-file soft-link-name
# ls -l
First char of the 10 character sequence e.g (-rwxrwxrwx) is used to indicate the file type:
d: a directory
l: a symbolic link
Permissions are either read(r), write(w) or execute(x). chmod (command to change the permissions
assigns numbers to each of these values read(4), write (2) and execute(1). We can combine those
values e.g. giving read and write permissions is 4+2=6.
After the listing of the permissions there is the owner and then the group listed of the file.
User=7|Group=5|Other=5
Ownership of a file or directory is assigned via chown, in the format (user:group, user:, or :group)
Make the file owned by the root user and the root group:
Copying user and group information from another file is achieved using the following:
These are special values that can be assigned to files or directories that allow the file or directory to
be run by a specific user or group essentially escalating privileges.
A good example of this is the passwd binary, this file needs to be executed as the root user even by
standard users as files need to be updated that are owned by root to facilitate a password change.
# ls -l /usr/bin/passwd
Setting setuid:
or
Setting setgid:
or
The use of special permissions can be very useful in some situations, but if not used correctly the can
introduce serious vulnerabilities, so think twice before using them.
The use of special permissions can be very useful in some situations, but if not used correctly the can
introduce serious vulnerabilities, so think twice before using them.
Locate, read, and use system docs including man, info, and files in /usr/share/doc
View command manual (man) page:
# man ls
# whatis ls
# apropos ls
Update man page database:
# mandb
# ls /usr/share/doc
# info ls
# ls /usr/share/info
# locate sysctl.conf
or
# updatedb
# which ls
# whereis ls
# reboot
# systemctl reboot
# shutdown -r now
# init 6
# telinit 6
# halt
# systemctl halt
# shutdown -h now
# init 0
# telinit 0
# poweroff
# systemctl poweroff
Advanced Management
To suspend the system:
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep
# systemctl get-default
Start the system and when BIOS information is displayed, select the option for a boot menu and
select to boot from the installation disk.
Choose Troubleshooting.
Choose Continue which is the default option. At this point you will be promoted for a passphrase if
an encrypted file system is found.
Press OK to acknowledge the information displayed until the shell prompt appears.
# chroot /mnt/sysimage
Enter the passwd command and follow the instructions displayed on the command line to change
the root password.
# passwd
Remove the autorelable file to prevent a time consuming SELinux relabel of the disk:
# rm -f /.autorelabel
Enter the exit command again to resume the initialization and finish the system boot.
Start the system and, on the GRUB 2 boot screen, press the e key for edit.
Remove the rhgb and quiet parameters from the end, or near the end, of the linux16 line, or linuxefi
on UEFI systems.
Add the following parameters at the end of the linux line on 64-Bit IBM Power Series, the linux16
line on x86-64 BIOS-based systems, or the linuxefi line on UEFI systems:
rd.break enforcing=0
Adding the enforcing=0 option enables omitting the time consuming SELinux relabeling process.
The initramfs will stop before passing control to the Linux kernel, enabling you to work with the root
file system.
Note that the initramfs prompt will appear on the last console specified on the Linux line.
With an encrypted file system, a password is required at this point. However the password prompt
might not appear as it is obscured by logging messages. You can press the Backspace key to see the
prompt. Release the key and enter the password for the encrypted file system, while ignoring the
logging messages.
# chroot /sysroot
Enter the passwd command and follow the instructions displayed on the command line to change
the root password.
# passwd
Updating the password file results in a file with the incorrect SELinux security context. To relabel all
files on next system boot, enter the following command:
# touch /.autorelabel
Alternatively, to save the time it takes to relabel a large disk, you can omit this step provided you
included the enforcing=0 option in step 3.
# mount -o remount,ro /
Enter the exit command again to resume the initialization and finish the system boot.
With an encrypted file system, a pass word or phrase is required at this point. However the
password prompt might not appear as it is obscured by logging messages. You can press and hold
the Backspace key to see the prompt. Release the key and enter the password for the encrypted file
system, while ignoring the logging messages.
Note that the SELinux relabeling process can take a long time. A system reboot will occur
automatically when the process is complete.
If you added the enforcing=0 option in step 3 and omitted the touch /.autorelabel command in step
8, enter the following command to restore the /etc/shadow file's SELinux security context:
# restorecon /etc/shadow
Enter the following commands to turn SELinux policy enforcement back on and verify that it is on:
# setenforce 1
# getenforce
Enforcing
# top
* TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a
second.
* NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice
value means lower priority.
# ps -ef
# ps -u root
# pgrep httpd
# pgrep -u username -l
# pgrep -v -u root -l
SIGHUP 1 Hangup
SIGTERM 15 Terminate
# kill -l
To kill a process:
# pidof auditd
or
# kill 745
or
# pkill auditd
# ps -edf
Process Priority
# nice -n 10 ./script.sh
To change the priority (here +5) of an already running process, get its PID (Process ID) through top or
ps (here 789) and type:
# renice +5 789
The scheduler is the kernel part that decides which executable process will be executed by the CPU
next. The Linux scheduler offers three different scheduling policies, one for normal processes and
two for real-time applications.
1. SCHED_OTHER – the default universal time-sharing scheduler policy used by most processes.
2. SCHED_FIFO or SCHED_RR – intended for special time-critical applications that need precise
control over the way in which executable processes are selected for execution
Scheduling Algorithm:
chrt:
chrt command is part of util-linux package – low-level system utilities that are necessary for a Linux
system to function. It is installed by default under Debian / Ubuntu / CentOS / RHEL / Fedora and
almost all other Linux distributions.
# chrt -p 112
# chrt -p 1
Any user can retrieve the scheduling information. No special privileges required.
How do I use chrt command to set real time attributes of a Linux process (already running
processes)?
# chrt -p 1025
# chrt -p 55 1025
# chrt -p 1025
Before setting new scheduling policy, you need to find out minimum and maximum valid priorities
for each scheduling algorithm, enter:
# chrt -m
# chrt -b -p 0 {pid}
# chrt -b -p 0 1024
# chrt -f -p 50 1024
# chrt -p 1024
# chrt -o -p 0 {pid}
# chrt -o -p 0 1024
# chrt -p 1024
To set scheduling policy to SCHED_RR, enter:
# chrt -r -p 20 1024
# chrt -p 1024
To manage tuned daemon use tuned-adm, to check the currently active profile being used on the
system:
# tuned-adm active
# tuned-adm list
Confirm is active:
# tuned-adm active
# tuned-adm recommend
# tuned-adm off
/var/log/audit/audit.log
SystemD application has been added to allow us to analyse the boot process.
# systemd-analyze
To get the time spent by each task during the boot process, type:
# systemd-analyze blame
In addition, Systemd handles the system event log, a syslog daemon is not mandatory any more.
# journalctl
To get all the events related to the crond process in the journal, type:
# journalctl /sbin/crond
Note: You can replace /sbin/crond by which crond.
Altenatively, to get all the events related to the crond process, you can also type:
# journalctl -u crond
# journalctl -b
To get all the events that appeared today in the journal, type:
# journalctl --since=today
# journalctl -p err
To get the 10 last events and wait for any new one (like tail -f /var/log/messages), type:
# journalctl -f
By default, Journald logs are stored in the /run/log/journal directory and disappear after a reboot.
# mkdir /var/log/journal
Note: Setting the SystemMaxUse variable is necessary because otherwise 10 percent of the
filesystem where the /var/log/journal directory is stored may be used at maximum by the journal.
# mkdir /var/log/journal
By default this would use up to 10% of the total space on that mount point. To manage that set the
value of "SystemMaxUse" to a value in KB (K), MB (M) or GB (G).
Note in RHEL 6 and below it was used as following, compatability is maintained for now.
Note: In addition, a service can be restarted with the restart option or only reloaded with the reload
option.
disabled
To transfer files from a remote host to the local host (current directory):
# scp root@10.0.2.6:/tmp/sshd_config .