Init script-ek, kill -HUP, runlevel-ek, boot loader

A Linux rendszert induláskor egy ún. boot loader tölti be, helye fizikailag a rendszerben konfigurált boot-sorrendben megtalálható “merevlemez” – HDD vagy SSD -, USB stick, CD vagy DVD, illetve hasonlóak lehetnek (a jövőre nézve). Ezeken van egy ún. MBR – Master Boot Record, ahol egy apró információt tárol el a boot loader – régebben LILO, ma inkább GRUB -, tudniillik valahova “lépnie” kell, hogy “beolvassa” az operációs rendszer file-jait, így betölti a kernel-t (a “rendszermag”, leírása később), a modulokat, és belép egy előre beállított ún. runlevel-be.

Debian 10 alatt alapban az 5-ösfutási szint” van beállítva. Ez többfelhasználós, grafikus mód. A runlevel paranccsal ellenőrizhetjük. 🙂

root@gergo1:~# runlevel
N 5

Ezen a runlevel-en a “/etc/rc5.d” könyvtár tartalma hajtódik végre. Ez a könyvtár szimbolikus linkeket (hivatkozásokat) tartalmaz a “/etc/init.d” könyvtárban lévő ún. init script-ekre.

root@gergo1:~# l /etc/rc5.d/
total 0
lrwxrwxrwx 1 root root 29 Aug 28 2017 K01apache-htcacheclean -> ../init.d/apache-htcacheclean
lrwxrwxrwx 1 root root 27 Aug 26 2017 K01speech-dispatcher -> ../init.d/speech-dispatcher
lrwxrwxrwx 1 root root 15 Aug 26 2017 S01acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 17 Aug 26 2017 S01anacron -> ../init.d/anacron
lrwxrwxrwx 1 root root 17 Aug 28 2017 S01apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 22 Aug 26 2017 S01avahi-daemon -> ../init.d/avahi-daemon
lrwxrwxrwx 1 root root 19 Aug 26 2017 S01bluetooth -> ../init.d/bluetooth
lrwxrwxrwx 1 root root 26 Aug 26 2017 S01console-setup.sh -> ../init.d/console-setup.sh
lrwxrwxrwx 1 root root 14 Aug 26 2017 S01cron -> ../init.d/cron
lrwxrwxrwx 1 root root 14 Aug 26 2017 S01dbus -> ../init.d/dbus
lrwxrwxrwx 1 root root 14 Aug 26 2017 S01gdm3 -> ../init.d/gdm3
lrwxrwxrwx 1 root root 13 Sep 4 07:07 S01gpm -> ../init.d/gpm
lrwxrwxrwx 1 root root 17 Jun 19 03:53 S01haveged -> ../init.d/haveged
lrwxrwxrwx 1 root root 18 Aug 26 2017 S01iptables -> ../init.d/iptables
lrwxrwxrwx 1 root root 20 Oct 23 2017 S01irqbalance -> ../init.d/irqbalance
lrwxrwxrwx 1 root root 18 Sep 1 2017 S01libvirtd -> ../init.d/libvirtd
lrwxrwxrwx 1 root root 24 Sep 1 2017 S01libvirt-guests -> ../init.d/libvirt-guests
lrwxrwxrwx 1 root root 23 Sep 1 2017 S01lvm2-lvmpolld -> ../init.d/lvm2-lvmpolld
lrwxrwxrwx 1 root root 19 Aug 26 2017 S01minissdpd -> ../init.d/minissdpd
lrwxrwxrwx 1 root root 15 Aug 28 2017 S01mysql -> ../init.d/mysql
lrwxrwxrwx 1 root root 25 Aug 26 2017 S01network-manager -> ../init.d/network-manager
lrwxrwxrwx 1 root root 14 Jun 7 02:39 S01nmbd -> ../init.d/nmbd
lrwxrwxrwx 1 root root 18 Jun 19 03:53 S01plymouth -> ../init.d/plymouth
lrwxrwxrwx 1 root root 15 Aug 26 2017 S01rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 17 Aug 26 2017 S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 21 Jun 7 02:39 S01samba-ad-dc -> ../init.d/samba-ad-dc
lrwxrwxrwx 1 root root 15 Aug 26 2017 S01saned -> ../init.d/saned
lrwxrwxrwx 1 root root 14 Jun 7 02:39 S01smbd -> ../init.d/smbd
lrwxrwxrwx 1 root root 13 Sep 15 21:42 S01ssh -> ../init.d/ssh
lrwxrwxrwx 1 root root 14 Jun 19 03:52 S01sudo -> ../init.d/sudo
lrwxrwxrwx 1 root root 29 Aug 26 2017 S01unattended-upgrades -> ../init.d/unattended-upgrades
lrwxrwxrwx 1 root root 15 Aug 26 2017 S01uuidd -> ../init.d/uuidd
lrwxrwxrwx 1 root root 18 Sep 1 2017 S01virtlogd -> ../init.d/virtlogd

Nézzünk meg egy példát. Vegyük a Secure Shell Server-t (démont). Az init script a “/etc/init.d/ssh” file-ban található. Ez egy executable – végrehajtható – shell script. Jól látható a 3 db “x” flag.

root@gergo1:~# l /etc/init.d/ssh
-rwxr-xr-x 1 root root 3939 Apr 8 11:51 /etc/init.d/ssh

Nézzük meg a tartalmából az elindításhoz releváns részt.

case "$1" in
start)
check_privsep_dir
check_for_no_start
check_dev_null
log_daemon_msg "Starting OpenBSD Secure Shell server" "sshd" || true
if start-stop-daemon --start --quiet --oknodo --chuid 0:0 --pidfile /run/sshd.pid --exec /usr/sbin/sshd -- $SSHD_OPTS; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
stop)
log_daemon_msg "Stopping OpenBSD Secure Shell server" "sshd" || true
if start-stop-daemon --stop --quiet --oknodo --pidfile /run/sshd.pid --exec /usr/sbin/sshd; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
reload|force-reload)
check_for_no_start
check_config
log_daemon_msg "Reloading OpenBSD Secure Shell server's configuration" "sshd" || true
if start-stop-daemon --stop --signal 1 --quiet --oknodo --pidfile /run/sshd.pid --exec /usr/sbin/sshd; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
..
status)
status_of_proc -p /run/sshd.pid /usr/sbin/sshd sshd && exit 0 || exit $?
;;
*)
log_action_msg "Usage: /etc/init.d/ssh {start|stop|reload|force-reload|restart|try-restart|status}" || true
exit 1
esac

Jól látható, hogy indítását, leállítását, a konfigurációs file-ok “újraolvasását”, továbbá az újraindítását – ez utóbbi nincsen itt forráskódban – mind a “/sbin/start-stop-daemon” rendszerbináris, azaz parancs végzi.

Felhívnám a figyelmet a “reload|force-reload” case ágra. Itt van egy “start-stop-daemon –stop –signal 1” rész. Ez nem más, mint egy olyan “signal” a szerverprocessznek, hogy olvassa újra a konfigurációs file-jait. Ez régebben a UNIX-okon és Linux-okon a “kill -HUP <pid>” volt. Ez a megoldás ezzel ekvivalens. 🙂

Nézzük meg a POSIX signal-okat. 🙂

First the signals described in the original POSIX.1-1990 standard.
Signal Value Action Comment
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no
readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at terminal
SIGTTIN 21,21,26 Stop Terminal input for background process
SIGTTOU 22,22,27 Stop Terminal output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

Bővebb – angol nyelvű – leírást az alábbiakban olvashatunk.

https://www.linux.com/news/introduction-services-runlevels-and-rcd-scripts/

Ha beírod root-ként (rendszergazdaként), hogy “runlevel 6“, akkor reboot-ol (újraindul) a Linux-od. 🙂

Ha ki szeretnéd “dobni” az összes felhasználót – és a konzolnál ülsz, hogy nehogy kizárd magad -, akkor a “runlevel 1” – single-user mode (egy felhasználós mód) – lesz a megfelelő parancs. 🙂

Tűzfal (firewall)

A Linux gépeken már régi téma a tűzfal. A tűzfal arra való, hogy védelmet nyújtson a magánhálózatoknak illetve a számítógépeknek az Interneten garázdálkodó rosszakaratú támadók ellen. Éppen ezért beszélhetünk hálózat-biztonságról – erről nálam itthon a router-en lévő tűzfal gondoskodik -, illetve hoszt-biztonságról, ami a Linux notebookom-on van telepítve. Ma már általában egyszerűek a beállítások, mind hálózati, mind hoszt szinten. Debian 10 alatt már automatikus az alap tűzfal-konfiguráció.

Régebben az ipchains volt, ma már az ún. iptables van használatban. Nézzük meg a Linux-unkat!

root@gergo1:~# iptables -L -v
Chain INPUT (policy DROP 590 packets, 93015 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh
256 20230 ACCEPT all -- lo any anywhere anywhere
6008 2038K ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:netbios-ns
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:netbios-dgm
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:netbios-ssn
0 0 ACCEPT tcp -- any any 192.168.0.0/24 anywhere state NEW tcp dpt:microsoft-ds
0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-request
6 336 ACCEPT tcp -- any any anywhere anywhere tcp dpt:http
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 6272 packets, 1041K bytes)
pkts bytes target prot opt in out source destination
256 20230 ACCEPT all -- any lo anywhere anywhere

Látható, hogy a bemeneten (INPUT) az SSH szerver, a Samba szerver (Microsoft hálózathoz) és a HTTP szerver van engedélyezve + az ICMP ping (leírása később), továbbá természetesen a már létrejött kapcsolatok is. Forward szinten semmi sincs “átengedve”. Nem továbbítunk adatot hálózatok között. Kimeneten (OUTPUT) pedig mindent “kiengedünk”.

Hogy hol van beállítva? Van egy pici konfigurációs file a /etc alatt.

root@gergo1:~# cat /etc/network/iptables.up.rules
# Generated by iptables-save v1.6.0 on Sat Aug 26 03:40:19 2017
*filter
:INPUT DROP [9:1026]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [22:3482]
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
#
# added by gvamosi on 2019-06-07
#
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 137 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 138 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 139 -j ACCEPT
-A INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 445 -j ACCEPT
-A INPUT -p icmp --icmp-type echo-request -j ACCEPT
#
# added by gvamosi on 2019-09-22
#
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
#
COMMIT
# Completed on Sat Aug 26 03:40:19 2017

És hogyan “töltődik be”? Az ún. init script-ek között kell keresnünk a választ (leírásuk később).

root@gergo1:~# cat /etc/init.d/iptables
#!/bin/sh
echo -n "Applying firewall rules.."
/sbin/iptables-restore < /etc/network/iptables.up.rules
echo "done."

Ez a kicsi init script tölti be a rendszer elindulásakor a tűzfal konfigurációját az iptables-restore parancs segítségével. Ennyi az egész. 🙂

Írhatnánk még az ún. DMZ (Demilitarizált Zóna) fogalmáról is. Ez egy olyan terület egy cégnél pl., ahonnan szerverek “kifelé”, a nagyvilágba, azaz az Internet felé is látszódhatnak. Határzóna a belső, védett, és a külső, védtelen hálózat között.