shutdown -h nowshutdownshutdown -r nowreboot
Boot sequence:
-
POST (PowerOn Self Test) -> Find disk -> Inside disk there's bootloader -> bootloader load kernel -> kernel load init process
-
Systemd is the default init process in CentOS
-
Systemd starts services. Last service started will be a shell
Systemd
-
Previous versions of Red Hat Enterprise Linux, which were distributed with SysV init or Upstart, implemented a predefined set of runlevels that represented specific modes of operation. These runlevels were numbered from 0 to 6 and were defined by a selection of system services to be run when a particular runlevel was enabled by the system administrator. In CentOS and Red Hat Enterprise Linux 7, the concept of runlevels has been replaced with systemd targets.
-
Systemd targets are represented by target units. Target units end with the .target file extension and their only purpose is to group together other systemd units through a chain of dependencies.
-
Systemd units are the objects that systemd knows how to manage. These are basically a standardized representation of system resources that can be managed by the suite of daemons and manipulated by the provided utilities.
-
Systemd units in some ways can be said to similar to services or jobs in other init systems. However, a unit has a much broader definition, as these can be used to abstract services, network resources, devices, filesystem mounts, and isolated resource pools.
-
Systemd was designed to allow for better handling of dependencies and have the ability to handle more work in parallel at system startup.
Systemd commands:
-
systemctl get-defaultIt shows default target
-
systemctl list-units --type target --allIt shows all available targets
-
systemctl set-default multi-user.targetSet multi-user target as default
Change target at boot time
-
If during boot ESC is pressed the grub2 prompt will be showed
-
Highlight a voice and press 'e'
-
Now is it possible to modify the boot parameter used to load the kernel.
NOTE: the changes are not persistent
E.g
systemd.unit=emergency.targetcan be added to boot system in emergency mode. NOTE: in this modality disk is mounted read only, to mount it read/write, after boot executemount-o remount,rw / -
When the parameter change is end, press 'Ctrl + x' to boot system
References:
- https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-targets
- https://en.wikipedia.org/wiki/Power-on_self-test
- https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files)
-
The default bootloader is Grub2.
-
The to change bootloader configuration edit /etc/default/grub
vi /etc/default/grub -
The configuration information can be found with:
-
info -f grub -n 'Simple configuration' -
man 7 bootparamIt shows the kernel boot parameter
-
-
check the firmware before compilation
ls -larth /sys/firmware -
if its efi then
grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfgelse
grub2-mkconfig -o /boot/grub2/grub.cfg -
if no errors during compilation then reboot otherwise kernel might enter panic state and wont reboot
reboot now
mpstat
-
yum -y install sysstat -
mpstat -P ALL -u 2 3CPU usage statistics.
-PIndicate the processor number for which statistics are to be reported, ALL for all cpu-uReport CPU utilization2 3Display three reports at two second intervals.
ps
-
psProcesses of which I'm owner -
ps auxAll processes.It will print:
-
user - user owning the process
-
pid - process ID of the process
- It is set when process start, this means that provide info on starting order of processes
-
%cpu - It is the CPU time used divided by the time the process has been running.
-
%mem - ratio of the process’s resident set size to the physical memory on the machine
-
VSZ (virtual memory) - virtual memory usage of entire process (in KiB)
-
RSS (resident memory) - resident set size, the non-swapped physical memory that a task has used (in KiB)
-
tty - On which process is running.
- NOTE: ? means that isn't connect to a tty
-
stat - process state
-
start- starting time or date of the process
-
time - cumulative CPU time
-
command - command with all its arguments
- Those within [ ] are system processes or kernel thread
-
-
ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu-eshow same result ofaux-ochose columns to show--sortsort by provided parameterppidparent process id -
ps -e -o pid,args --forest--forestshow a graphical view of processes tree -
In /proc/[pid]
There is a numerical subdirectory for each running process; the subdirectory is named by the process ID.
-
/proc/[pid]/fd
This is a subdirectory containing one entry for each file which the process has open, named by its file descriptor, and which is a symbolic link to the actual file. Thus, 0 is standard input, 1 standard output, 2 standard error, and so on.
-
lsof -p pidLists open files associated with process id of pid
Background processes
-
End a command with
&execute a process in backgroundsleep 600 & -
jobsList processes in background
-
fg pidTo return a process in foreground
Process priority
-
ps -e -o pid,nice,commandnice (NI) is the process priority
-
More priorities and more CPU time will be assigned to process
-
nice value can be between -20 and 90
-
-20 is highest and 90 is lowest
-
NOTE: only root can assign negative values
-
nice -n value command &It will execute command in background with nice equal to value
-
reniceri-assign priority to a processrenice -n value pid
Signals
-
kill pidSend a SIGTERM to process with pid equal to pid
-
kill -9 pidSend a SIGKILL to process with pid equal to pid
-
kill -number pidSend a signal that correspond to number to process with pid equal to pid
-
kill -lList all available signal and corresponding number
References:
- https://superuser.com/questions/117913/ps-aux-output-meaning
- http://man7.org/linux/man-pages/man5/proc.5.html
-
Usually log files are stored in
/var/log -
In Centos many tools use
rsyslogto manage logs. -
rsyslogis a daemon that permit the logging of data from different types of systems in a central repository/etc/rsyslog.confconfiguration file of rsyslogsystemctl status rsyslogto check execution status of rsyslog
References:
- Daemon that schedule tasks, called jobs, to run at a set date and time is cron
- The schedule of various tasks depend by configuration contained in below files/directories:
- /etc/crontab
- Normally isn't edited
- NOTE: It's content can be used as remainder of cron files syntax
- Each row is a task that must be executed in a scheduled way
- A special syntax indicates the schedule of each commands
- Normally isn't edited
- /etc/cron.d
- It contains files with same syntax of /etc/crontab
- Normally it used by software packages installed in system
- /var/spool/cron
- It contains tasks for users
- Contents can be edited using
crontabcommand
- /etc/cron.hourly
- Each script in this directory will be executed every hour
- Exact time isn't specified but execution is granted, with a combination of deamon cron and anacron
- /etc/cron.daily
- Each script in this directory will be executed every day
- Exact time isn't specified but execution is granted, with a combination of deamon cron and anacron
- /ect/cron.weekly
- Each script in this directory will be executed every week
- Exact time isn't specified but execution is granted, with a combination of deamon cron and anacron
- /etc/cron.monthly
- Each script in this directory will be executed every month
- Exact time isn't specified but execution is granted, with a combination of deamon cron and anacron
- /etc/crontab
To modify cron jobs:
-
crontab -eIt is used by user to modify his jobs -
crontab -e -u userIt is used by root to modify user's jobs -
Both commands will create a file in /var/spool/cron
-
crontab -u user -lprint user's jobs or better show content of file in /var/spool/cron
Cron syntax:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute#this line is a comment*always1 0 * * * /commandcommand will be executed one minute past midnight (00:01) every day1-30 * * * * /commandcommand will be executed every day, every hour at minutes 1 to 30*/10 * * * * /commandcommand will be executed every 10 minutes, or rather when minutes are 00, 10, 20, 30, 40 and 50.00 */2 15 * * /commandcommand will be executed the fifteenth day of every month, every two hours00 1-9/2 1 5 * /commandcommand will be executed on 1st May at 1,00 - 3,00 - 5,00 - 7,00 - 9,00, or rather every two hours from 1,00 to 9,0000 13 2,8,14 * * /commandcommand will be executed second, eighth and fourteenth day of each month at 13.00
at
-
yum -y install at -
NOTE: it require that atd demon will be in execution
systemctl start atdsystemctl enable atd
-
at 11:00open a shell in which inserted commands that will be executed at 11:00ctrl+dclose shell
-
atqshows scheduled activities identified by an activity ID -
atrm IDwill remove from schedule activity with activity ID equal to ID
References:
-
Cron will send an email to internal mail spool
-
Enable the logging of crond events
-
Edit the /etc/rsyslog.conf and remove comment from this line:
# Log cron stuff
cron.* /var/log/cronsystemctl restart rsyslogit will restart rsyslog server
yum update- Yum also offers the upgrade command that is equal to update with enabled
obsoletesconfiguration option. By default, obsoletes is turned on in/etc/yum.conf, which makes these two commands equivalent. - The
obsoletesoption enables the obsoletes process logic during updates.When one package declares in its spec file that it obsoletes another package, the latter package is replaced by the former package when the former package is installed. Obsoletes are declared, for example, when a package is renamed
References:
/usr/lib/rpm/rpmdb_verify /var/lib/rpm/PackagesIt will verify the integrity of rpm database
-
systemctl status processnameIt will show the status of process with name processname- The las rows are the recent logs generated by daemon
-
Other command to check processes status:
pspgrepmpstat
-
In /proc/sys are contained kernel tunables, parameters that are used to customize the behavior of system
-
Example
-
cd /proc/sys/net/ipv6/conf/all -
echo 1 > /proc/sys/net/ipv6/conf/alldisable_ipv6Will disable IPv6
-
NOTE: This is a runtime change, not permanent
-
NOTE: With this files
vicannot be used
-
-
Alternative method:
sysctl -w net.ipv6.conf.all.disable_ipv6=1 -
sysctl -ashows all parameters that can be configured
To make configuration permanent
cd /etc/sysctl.decho net.ipv6.conf.all.disable_ipv6 = 1 > ipv6.conf- NOTE: the only request is that file will end with
.conf
- NOTE: the only request is that file will end with
sysctl -preload permanent configuration. Alternative: reboot system
Some parameters changed commonly:
-
net.ipv4.ip_forward=0 disable packet forwarding
-
fs.file-max -> massimo numero di file gestibili
-
kernel.sysrq -> abilita printscreen key
-
net.ipv4.icmp_echo_ignore_all -> ignora ping
Bash shell script:
-
#!/bin/bashmust be first row -
A Bash script is a plain text file which contains a series of commands or/and typical constructs of imperative programming
-
It is convention to give files that are Bash scripts an extension of .sh
-
chmod +x nomefile.shmust be executable -
./nomefile.shexecute nomefile.sh
References:
-
systemctlcommand used to manage servers. In Linux servers often are called daemons -
systemctl status processnameIt will show the status of process with name processnameActiveprocess status eg. inactive, activeLoadedunit file name- unit file name; enable - This means that daemon will be executed automatically at the next reboot
- unit file name; disabled This means that daemon won't be executed automatically at the next reboot
- The las rows are the recent logs generated by daemon
-
systemctl start sshdIt will start sshd daemon -
systemctl stop sshdIt will stop sshd daemon -
systemctl restart sshdIt will restart sshd daemon- NOTE: A restart must be executed each time a daemon configuration file is changed
-
systemctl disable sshdDisable the execution of service at bootstrap -
systemctl enable sshdEnable the execution of service at bootstrap -
systemctl is-enabled sshdCheck if daemon is enable or disabled in bootstrap sequence -
systemctl list-unit-filesList all systemd units object available
References:
-
In computer security, mandatory access control (MAC) refers to a type of access control by which the operating system constrains the ability of a subject or initiator to access or generally perform some sort of operation on an object or target. In practice, a subject is usually a process or thread; objects are constructs such as files, directories, TCP/UDP ports, shared memory segments, IO devices, etc. Subjects and objects each have a set of security attributes. Whenever a subject attempts to access an object, an authorization rule enforced by the operating system kernel examines these security attributes and decides whether the access can take place. Any operation by any subject on any object is tested against the set of authorization rules (aka policy) to determine if the operation is allowed.
-
In CentOS as MAC is used SELinux
-
SELinux can be in three states:
- enforcing: Actions contrary to the policy are blocked and a corresponding event is logged in the audit log
- permissive: Actions contrary to the policy are only logged in the audit log
- disabled: The SELinux is disabled entirely
- The status can be configured in file
/etc/sysconfig/selinux. Changing to this file will be read only after reboot - When state is set to enforcing can be switched to permissive and vice versa without reboot system
- When the state is set to disable the only way to re-enable SELinux is to change
/etc/sysconfig/selinuxand reboot
-
getenforceshow the SELinux state -
setenforce Permissiveset the state to permissive -
setenforce Enforcingset the state to enforcing -
On systems running SELinux, all processes and files are labeled in a way that represents security-relevant information. This information is called the SELinux context.
-
Normally SELinux context is showed with
-Zoption -
ls -lZshow SELinux context of file -
ps auxZshow SELinux context of processes -
A SELinux context has the form user:role:type
-
type indicate the type of object
-
unconfined_t are object not limited by SELinux
-
References
yum
-
packet manager that use RPM packet manager
-
yum search keywordThis is used to find packages when you know something about the package but aren't sure of it's name. By default search will try searching just package names and summaries, but if that "fails" it will then try descriptions and url.
-
Repository: collections of software packages used by yum. They are configured in
/etc/yum.repos.d -
yum info packageInformation on package- If package is installed Repo will be equal to "installed"
-
yum install packageInstall package -
yum provides */fileSearch package that contain file -
yum remove packageRemove package -
yum autoremove packageRemove package plus unused dependencies -
yumdownloader packagedownload the RPM package- NOTE: require
yum -y install yum-utils
- NOTE: require
RPM
rpm -i file.rpmInstall file.rpmrpm -U file.rpmUpgrade file.rpmrpm -qaList all installed RPMrpm -qf fileTells to what RPM package file belong
-
yum provides */fileSearch package that contain file -
ldd path/commandShow all libraries used by command -
This info is contained in a library cache
-
The library cache can be re-build using
ldconfing -
The library cache is in /etc/ld.so.cache
-
The info for cache are in /etc/ld.so.cache.d/
-
The cache is normally re-build each time a new package is installed