Linux Indepth- #3

Issue:

You need to know if your Linux uses systemd or something else.

 Solution:  Look for the /run/systemd/system/ directory. If this exists, then your init system is systemd.

 This example confirms that the init is systemd:
$ stat /sbin/init
File: /sbin/init -> /lib/systemd/systemd
[...]
On a system using SysV init, it has no symlink:

$ stat /sbin/init
File: /sbin/init
[...]
The /proc pseudofilesystem is an interface to your Linux kernel, and contains the current state of a running system. It is called a pseudofilesystem because it exists only in memory and not on disk. In this example, /proc/1/exe is symlinked to the systemd executable:

$ sudo stat /proc/1/exe
File: /proc/1/exe -> /lib/systemd/systemd
[...]
On a SysV system, it links to init:

$ sudo stat /proc/1/exe
File: /proc/1/exe -> /sbin/init
[...]
The /proc/1/comm file reports your active init system:

$ cat /proc/1/comm
systemd
On a SysV system, it reports init:

$ cat /proc/1/comm
init
The command attached to process ID (PID) 1 is your init. PID 1 is the first process launched at startup, which then starts all other processes. You can see this with the ps command:

$ ps -p 1
  PID TTY          TIME CMD
    1 ?        00:00:00 systemd
When the init is SysV, it looks like this:

$ ps -p 1
  PID TTY          TIME CMD
    1 ?        00:00:00 init 

----------------------------------------------------------------------------------------------------------------------------

Issue:
You want to list all services installed on your system, and you want to know the states of the services: whether they are running, not running, or in an error state.

Solution:
systemctl, the systemd manager command, tells all. Run it with no options to see a detailed list of all loaded units. A systemd unit is any related batch of processes defined in a unit configuration file and managed by systemd:

$ systemctl

$ systemctl > /tmp/systemctl-units.txt
Treat yourself to more information overload by listing all units, active and inactive:

$ systemctl --all

$ systemctl list-unit-files
UNIT FILE                                      STATE
proc-sys-fs-binfmt_misc.automount              static
-.mount                                        generated
mount                                          generated
dev-hugepages.mount                            static
home.mount                                     generated

$ systemctl list-unit-files --type=service
UNIT FILE                                  STATE
accounts-daemon.service                    enabled
acpid.service                              disabled
alsa-state.service                         static
alsa-utils.service                         masked
anacron.service                            enabled

 

List only enabled services:
$ systemctl list-unit-files --type=service --state=enabled
UNIT FILE                                  STATE
accounts-daemon.service                    enabled
anacron.service                            enabled
apparmor.service                           enabled
autovt@.service                            enabled
avahi-daemon.service                       enabled

$ systemctl list-unit-files --type=service --state=disabled
UNIT FILE                            STATE
acpid.service                        disabled
brltty.service                       disabled
console-getty.service                disabled
mariadb@.service                     disabled

List only static services:
$ systemctl list-unit-files --type=service --state=static
UNIT FILE                              STATE
alsa-restore.service                   static
alsa-state.service                     static
apt-daily-upgrade.service              static
apt-daily.service                      static

List only masked services:
$ systemctl list-unit-files --type=service --state=masked
UNIT FILE                    STATE
alsa-utils.service           masked
bootlogd.service             masked
bootlogs.service             masked
checkfs.service              masked

--------------------------------------------------------------------------------------------------------------------------

Issue: You want to know the status of one service or a few specific services. 

Solution:  systemctl status provides a nice little bundle of useful status information. The following example queries the CUPS service.

$ systemctl status cups.service
----------------------------------------------------------------------------------------------------------------------------
 

Issue:

You want to stop and start services with systemd.

So;ution:

This is a job for systemctl. The following examples use the SSH service to demonstrate service management.

Start a service:

$ sudo systemctl start sshd.service

Stop a service:

$ sudo systemctl stop sshd.service

Stop and then restart a service:

$ sudo systemctl restart sshd.service

Reload the service’s configuration. For example, you made a change to sshd_config and want to load the new configuration without restarting the service:

$ sudo systemctl reload sshd.service
--------------------------------------------------------------------------------------------------------------------------

Issue:

You want a service or services to automatically start at boot, or you want to prevent a service from starting at boot, or to disable it completely.

Solution

Enabling a service configures it to automatically start at boot.

Disabling a service stops it from starting at boot, but it can be started and stopped manually.

Masking a service disables it so that it cannot be started at all.

The following example enables the sshd service:

$ sudo systemctl enable sshd.service
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service →
/usr/lib/systemd/system/sshd.service

The output shows that enabling a service means creating a symlink from the service file in /lib/systemd/system/ to /etc/systemd/system/. This does not start the service. You can start the service with systemctl start, or enable and start the service in one command with the --now option:

$ sudo systemctl enable --now sshd.service

This command disables the sshd service. It does not stop the service, so you must stop it manually after disabling it:

$ sudo systemctl disable sshd.service
Removed /etc/systemd/system/multi-user.target.wants/sshd.service
$ sudo systemctl stop sshd.service

Or, disable and stop it with one command:

$ sudo systemctl disable --now sshd.service
--------------------------------------------------------------------------------------------------------------------------


Issue:

You want to know how to stop troublesome processes. A certain service may be unresponsive or running away, spawning forks and causing your system to hang. Your normal stop command is not working. What do you do?

Solution

Stopping a process is called killing the process. On Linux systems with systemd, you should use systemctl kill. On systems without systemd, use the legacy kill command.

systemctl kill is preferable because it stops all processes that belong to a service and leaves no orphan processes, nor any processes that might restart the service and continue to make trouble. First, try it with no options other than the service name, then check the status:

$ sudo systemctl kill mariadb

The service has cleanly stopped. If this does not work, then try the nuclear option:

$ sudo systemctl kill -9 mariadb

The legacy kill command does not recognize service or command names, but rather requires the PID of the offending process:

$ sudo kill 1234

If this does not stop it, use the nuclear option:

$ sudo kill -9 1234












Comments

  1. Really useful information too practice and gets hands on experience.

    ReplyDelete

Post a Comment

Popular posts from this blog

Linux Indepth #4

How To Rebuild GRUB Configuration file

DevOps Project Contest.