Thursday, 26 March 2020

Systemd - ITPRO TV - PERCIPIO


Managing services with systemd:
Powerful technology from 2008. How it works, what it does we can see here.

How are services controlled under Linux?
Man system uses init system. When system boots up it calls Kernel which is wrote by Linus Torvalds. It itself don't do much. After kernel start it starts init. Init will start various other services. It is used in Unix we call sysvinit or System Five.

Drawbacks of SysVinit
It runs in serial order. Like script 1, script 2, and script 3. If script 3 takes long time then it delays starting script 4.
In current situation, we constantly plug-in new devices like thumb drive. In that case we need to re-run the scripts to look for missing hardware. This is problem with sysvinit.

What SystemD does?
It is a system daemon which means any program that runs in the background. It always stays online unlike SysVinit, so that it can detect hardware changes. It can do things in parallel. It is faster and more stable. It does lot of stuff in binary, while SysVinit allows you to change configuration in a text file which many Linux users prefer.

SystemD reaches out to hard drive and find boots files, loads up the Linux kernel. Linux Kernel launches init, in our case it is systemd. SystemD will start everything else we interact with our system

How do we know whether we are using SystemD?
Major distro use SystemD by default. Sometimes people use upstart which is modified version of SysVinit. Distro like Slackware, Devaun (variant of Debian) still uses sysvinit.

How to verify if I have systemd or sysvinit?
Init is our very first process. You can try firing ps aux if you scroll up see the first process it shows what process it started first, it should be systemd. Sometime it says /sbin/init which is SysVinit but you need to dig deeper to check. ls -l /sbin/init this may have symbolic link to systemD. In these cases the ps output will still show init but under the hood it is systemD. /sbin/init should be always present in the Linux system as per the standard. So all of the recent distro would have symlinked to systemD.

SystemD starts right after the Kernel, what happens next?
It uses unit file to start other services. It is stored in /lib/systemd/system. There will be ton of service file with .service and .target extension. .target is for GUI. .socket files are for network which allows two application talk to each other via a network. .mount file contains hard drive partition. We will still have /etc/fstab.

Example exploring sshd.service:
It starts with block [Unit]

After -> will have dependencies. For sshd we need network services. So it will first start network.services
Wants -> Says what else to start along with sshd.service.

[Service] -> This block will specify actual service to be started.
EnvironmentFile -> Where we have environment variables
ExecStart -> Actual command that will start the service
ExecReload -> Usually kill command
KillMode -> Default is process
Restart -> Says when to restart (on-failure) is default
RestartSec -> Wait time after failure

[Install] -> Specifies target
WantedBy=multi-user.target

When command line starts, it starts sshd. Most of the times it is created.


How to modify the Unit files?
It is not modified directly. Don't anything manually inside /lib. Go to /etc/systemd/system.
Sometime the folder won't exists we might need to create it. Again in this folder you will see service, wants and target file. This will override the files in /lib/systemd

SystemD binary is located @ /lib/systemd

Kernel -> Do all hardware interactions
SystemD -> Do all software interactions



Do we have to create new files for Unit services?
Most of the software installation will come with Unit Files. So while installing we get and while uninstalling it get removed.

If we want the service to start when system reboots we can enable using below command:
sudo systemctl enable httpd

This will just create symlink to /etc folder because it takes more precedence than /lib. You can also disable using below command:

sudo systemctl disable httpd

The command only disable starting of service after reboot. You can still start manually.

How systemD keps track of which service to run?
it is done using target files. .target files keep track of what needs to run next. For example: If we need command line then multi-user.target will get triggered. Then it will see all the entries inside the file which has other dependent target files.

If we edit, then it won't recognize unless we reload.

Another example: To Turn Off GUI

sudo systemctl isoloate multi-user.target


 To Turn on GUI
sudo systemctl isoloate graphical.target

No comments:

Post a Comment

Golang - Email - Secure code warrior

 package mail import ( "net/smtp" "gobin/config" ) var ( emailConfig config.Email ) type Mail struct { Destinati...