March 2025
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Categories

March 2025
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  

CentOS / RHEL 7 : How to configure serial getty with systemd

With SysV init, by default, getty processes are started on the first six virtual consoles. They can be accessed with the Ctrl+Alt+F1 to Ctrl+Alt+F6 key combination. systemd starts the getty processes only when needed. That means, only after you change to, for instance, the second virtual terminal by pressing Ctrl+Alt+F2 is the getty process started in that terminal.

The /usr/lib/systemd/system/getty@.service file is responsible for the virtual terminals (/dev/tty[X]). /usr/lib/systemd/system/serial-getty@.service is responsible for all other terminals, such as a serial terminal on /dev/ttyS0.

Systemd provides a template unit file for serial getty. Template file can be found here

/lib/systemd/system/serial-getty@.service
Steps

1. First copy the template:

# cp /usr/lib/systemd/system/serial-getty@.service /etc/systemd/system/serial-getty@ttyS0.service
2. Then edit the file and modify the agetty line:

[Service]
ExecStart=-/sbin/agetty –keep-baud 115200,38400,9600 %I $TERM <-- Change this parameter Type=idle 3. Create a symlink: # ln -s /etc/systemd/system/serial-getty@ttyS0.service /etc/systemd/system/getty.target.wants/ 4. Reload the daemon and start the service: # systemctl daemon-reload # systemctl start serial-getty@ttyS0.service # systemctl enable serial-getty@ttyS0.service

CentOS / RHEL 7 : How to change runlevels (targets) with systemd

Systemd has replaced sysVinit as the default service manager in RHEL 7. Some of the sysVinit commands have been symlinked to their RHEL 7 counterparts, however this will eventually be deprecated in favor of the standard systemd commands in the future.

SysVinit V/s systemd runlevels

Here is a comparison between SysVinit runlevels V/s systemd targets.

SYSVINIT RUNLEVEL SYSTEMD TARGET FUNCTION
0 runlevel0.target, poweroff.target System halt/shutdown
1, s, single runlevel1.target, rescue.target Single-user mode
2, 4 runlevel2.target, runlevel4.target, multi-user.target User-defined/Site-specific runlevels. By default, identical to 3.
3 runlevel3.target, multi-user.target Multi-user, non-graphical mode, text console only
5 runlevel5.target, graphical.target Multi-user, graphical mode
6 runlevel6.target, reboot.target Reboot
emergency emergency.target Emergency mode
Changing runlevels with systemd

The runlevel target can be changed by using the systemctl isolate command :

# systemctl isolate multi-user.target
To view what targets are available you can issue the list-units option with the type target

# systemctl list-units –type=target
Run level 3 is emulated by multi-user.target. This is done by symbolic link and can be used interchangeably

# systemctl isolate multi-user.target
# systemctl isolate runlevel3.target
# ls -l /usr/lib/systemd/system/runlevel3.target
lrwxrwxrwx 1 root root 17 Oct 18 11:41 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
Run level 5 is emulated by graphical.target. This is also done by symbolic link and can be used interchangeably

# systemctl isolate graphical.target
# systemctl isolate runlevel5.target
# ls -l /usr/lib/systemd/system/runlevel5.target
lrwxrwxrwx 1 root root 16 Oct 18 11:41 /usr/lib/systemd/system/runlevel5.target -> graphical.target
Changing the default runlevel

The default runlevel can be changed by using the set-default option.

# systemctl set-default multi-user.target
To get the currently set default, you can use the get-default option.

# systemctl get-default
The default runlevel in systemd can also be set using the below method (not recommended though).

# ln -sf /lib/systemd/system/[desired].target /etc/systemd/system/default.target
The default target can also be set in the kernel line during boot by adding the following option :

systemd.unit=multi-user.target

CentOS / RHEL 7 : systemd-analyze command to find booting time delays

Question : My system is taking a lot of time to boot. How can I find out which services are taking long time to start?

Answer :

systemd-analyze command can be utilized to find out information about how much each service took to start. systemd-analyze time can provide overall information about how long it took system to start. Here is a command out which clearly shows the time taken by kernel, initrd and userspace while booting.

# systemd-analyze time
Startup finished in 1.267s (kernel) + 6.798s (initrd) + 1min 2.139s (userspace) = 1min 10.205s
To find out, how much time each unit took to start, run systemd-analyze blame.

# systemd-analyze blame
24.728s dev-mapper-centos\x2droot.device
15.135s kdump.service
14.670s plymouth-quit-wait.service
14.210s firewalld.service
9.835s accounts-daemon.service
7.383s ModemManager.service
7.259s libvirtd.service
7.257s systemd-logind.service
7.177s ksm.service
7.081s gssproxy.service
7.067s avahi-daemon.service
7.062s rsyslog.service
7.039s abrt-ccpp.service
As you see the output is sorted according to the time taken by each unit, you can easily find out which service is taking more time during booting and can dig down deeper to analyze the issue.

At certain steps, the boot cannot proceed until all dependencies for unit are satisfied. To see units at these critical points run systemd-analyze critical-chain.

# systemd-analyze critical-chain
The time after the unit is active or started is printed after the “@” character.
The time the unit takes to start is printed after the “+” character.

graphical.target @1min 2.102s
??multi-user.target @1min 2.102s
??abrt-vmcore.service @1min 1.228s +872ms
??kdump.service @46.090s +15.135s
??remote-fs.target @46.086s
??remote-fs-pre.target @46.083s
??iscsi-shutdown.service @45.951s +99ms
??network.target @45.944s
??network.service @44.959s +975ms
??NetworkManager.service @38.653s +689ms
??firewalld.service @24.439s +14.210s
??basic.target @23.850s
??sockets.target @23.849s
??cups.socket @23.847s
??sysinit.target @23.618s
??systemd-update-utmp.service @23.603s +13ms
??auditd.service @22.959s +643ms
??systemd-tmpfiles-setup.service @22.726s +230ms
??rhel-import-state.service @22.431s +294ms
??local-fs.target @22.428s
??boot.mount @19.675s +2.126s
??dev-disk-by\x2duuid-7de2053c\x2d44d7\x2d4f33\x2db522\x2d81dee2f6b69b.device @19.652s
SVG graphic image can be plot which contains detailing about system services start time, highlighting the time they spent on initialization. Make sure you have enabled graphical display mode or have x-windows enabled in order to see the plot.

# systemd-analyze plot > plot.svg
# eog plot.svg
Here is a snip from sample plot on my CentOS 7 machine. Zoom in to check the waterfall clearly.

CentOS / RHEL 7 : How to open the Firewall port for Samba server using FirewallD

Question : I cannot reach my Samba server after starting the service. How do I open the port to be able to connect to my Samba server?

Solution :

If running FirewallD, it is mandatory to open the ports used by the Samba server in order for it to properly accept clients. To begin with check the firewalld status using the systemctl command :

# systemctl status firewalld
? firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2016-10-29 21:47:04 IST; 1 weeks 4 days ago
Main PID: 1055 (firewalld)
CGroup: /system.slice/firewalld.service
??1055 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid

Oct 29 21:46:50 localhost.localdomain systemd[1]: Starting firewalld – dynamic firewall daemon…
Oct 29 21:47:04 localhost.localdomain systemd[1]: Started firewalld – dynamic firewall daemon.
As seen in the output above the firewall service is active and running. So we are good to go further.

Check the list of open ports and services using the commands below :

# firewall-cmd –list-ports
# firewall-cmd –list-services
Once you have confirmed that the samba ports are not open on the system, open the Samba port using the following FirewallD command:

# firewall-cmd –add-service=samba
The above command will change the runtime, opening the port temporarily. Make the changes permanent using the following command (this will persist through reboot):

# firewall-cmd –add-service=samba –permanent
Verify

To verify if the samba service is added to the firewalld use :

# firewall-cmd –list-services
dhcpv6-client samba ssh
As you can see samba service is now added to the firewall and samba ports are now open.

CentOS / RHEL 7 : Unable to start/enable iptables

When trying to start/enable the iptables daemon you receive the errors:

# systemctl enable iptables
Failed to issue method call: Access denied
# systemctl start iptables
Failed to start iptables.service: Unit iptables.service failed to load: No such file or directory.
Solution:

Starting with RHEL 7, firewalld is introduced and by default the iptables package is not installed on the system. This is done to avoid conflict in running both iptables and firewalld.

Make sure you have the iptables-services package installed. This legacy package provides the systemd scripts for the previous iptables invocation. This package is not always installed, depending on your installation choices when you installed (or upgraded).

The package “iptables-services” needs to be installed before you are able to start the service.

# yum install iptables-services

CentOS / RHEL 7 : Never run the iptables service and FirewallD service at the same time!

By default, RHEL 7 uses the FirewallD service to provide network security. FirewallD must be stopped and disabled when using the iptables service:

# systemctl stop firewalld.service
# systemctl disable firewalld.service
# systemctl enable iptables.service
# systemctl start iptables.service
The iptables service is now provided by a separate package called iptables-services:

# yum info iptables-services
Name : iptables-services
Arch : x86_64
Version : 1.4.21
Release : 13.el7
Size : 23 k
Repo : installed
From repo : anaconda
Summary : iptables and ip6tables services for iptables
URL : http://www.netfilter.org/
License : GPLv2
Description : iptables services for IPv4 and IPv6
:
: This package provides the services iptables and ip6tables that have been split
: out of the base package since they are not active by default anymore.
The iptables-services package may need to be installed 1st:

# systemctl -a|grep iptables
? iptables.service not-found inactive dead iptables.service
Stop and disable the firewalld service first.

# systemctl stop firewalld.service
# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
If you try to enable the iptables service, it would fail.

# systemctl enable iptables.service
Failed to execute operation: No such file or directory
Install the iptables-services package.

# yum install iptables-services -y
Enable the iptables service :

# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
Start the iptables service :

# systemctl start iptables.service
#

CentOS / RHEL 7 : How to open a port in the firewall with firewall-cmd?

Question: How to open a port in RHEL 7 using the firewall-cmd command?

Solution:

To begin with check the firewalld status using the systemctl command :

# systemctl status firewalld
? firewalld.service – firewalld – dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2016-10-29 21:47:04 IST; 1 weeks 4 days ago
Main PID: 1055 (firewalld)
CGroup: /system.slice/firewalld.service
??1055 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid

Oct 29 21:46:50 localhost.localdomain systemd[1]: Starting firewalld – dynamic firewall daemon…
Oct 29 21:47:04 localhost.localdomain systemd[1]: Started firewalld – dynamic firewall daemon.
Execute these commands to add a port to the firewall:
The command below will open the port effective immediately, but will not persist across reboots:

# firewall-cmd –add-port=[YOUR PORT]/tcp
For example, to open TCP port 2222 :

# firewall-cmd –add-port=2222/tcp
The following command will create a persistent rule, but will not be put into effect immediately:

# firewall-cmd –permanent –add-port=[YOUR PORT]/tcp
For Example, to open TCP port 2222 :

# firewall-cmd –permanent –add-port=2222/tcp

CentOS / RHEL : Configure yum automatic updates with yum-cron service

yum-cron is an optional package starting from Red Hat Enterprise Linux 6, this is a plugin for yum. From man page of yum-cron :

yum-cron is a simple way to call yum commands from cron. It provides configuration to keep repository metadata up to date, and to check for, download, and apply updates.
The yum-cron package provides a convenient way to check for, download and apply updates automatically. The cron jobs from the yum-cron package are active immediately after installing the package and there’s no extra configuration necessary. The job will be run when your normal daily cron jobs are set to run.

Installation

To install yum-cron package

# yum -y install yum-cron
# chkconfig yum-cron on
Configuration for RHEL 6

Then edit /etc/sysconfig/yum-cron to set MAILTO=[email address] for email notifications.

MAILTO=sys@example.com
To exclude packages using yum-cron edit the /etc/sysconfig/yum-cron to have the packeges to be excluded in the automatic uodate :

YUM_PARAMETER=kernel* mysql*
To start yum-cron service after editing configuration file.

# service yum-cron start
Configuration for RHEL 7

Then edit /etc/yum/yum-cron.conf to set email_to=[email address] for email notifications.

email_to=sys@example.com
To exclude packages using yum-cron edit the /etc/yum/yum-cron.conf to have the packeges to be excluded in the automatic uodate :

exclude=kernel* mysql*
In RHEL 7 you can use following options in yum-cron configuration file, to install security updates.

[commands]
# What kind of update to use:
# default = yum upgrade
# security = yum –security upgrade
# security-severity:Critical = yum –sec-severity=Critical upgrade
# minimal = yum –bugfix upgrade-minimal
# minimal-security = yum –security upgrade-minimal
# minimal-security-severity:Critical = –sec-severity=Critical upgrade-minimal
update_cmd = default
To start yum-cron service after editing configuration file.

# systemctl start yum-cron

CentOS / RHEL : How to install and start the Apache httpd service

Question: How to install Apache and configure Apache httpd to run as a service?

Answer:

Installing the Apache package

If you have the apache package downloaded, you can install it using rpm command as root user.

# rpm -ivh httpd
If you have yum repository configured, use the recommended way of installing Apache httpd, i.e. with “yum install” command.

# yum install httpd
NOTE: To get a specific version replace httpd with the version you would like. Example: httpd-2.2.14-1.2.6.jdk6.ep5.el5.x86_64
Installing httpd as a service

Enable the http service on startup with the below command. This will enable it for runlevels 2,3,4 & 5 :

# chkconfig httpd on
To start the httpd service :

# service httpd start
For RHEL 7

With RHEL 7 you use the systemctl command to enable the service:

# systemctl enable httpd.service
With RHEL 7 you use the systemctl command to start the service:

# systemctl start httpd.service

Why CentOS / RHEL 7 have many tmpfs (6 tmpfs filesystem), and what is their purpose

Question: Why CentOS 7 / RHEL 7 have many tmpfs (6 tmpfs filesystem), are these tmpfs mandatory for OS? What are the mount /run/user/1000, /run/user/0, /run/user/45, /sys/fs/cgroup for?

Answer:

If you run df -hP in RHEL 7 system you would find many tmpfs file systems mounted.

# df -hP
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_os-lv_root 296G 33G 248G 12% /
devtmpfs 126G 0 126G 0% /dev
tmpfs 126G 631M 126G 1% /dev/shm
tmpfs 126G 1.3G 125G 2% /run
tmpfs 126G 0 126G 0% /sys/fs/cgroup
/dev/sda1 976M 112M 797M 13% /boot
tmpfs 26G 0 26G 0% /run/user/0
tmpfs 26G 0 26G 0% /run/user/5006
tmpfs 9.5G 68K 9.5G 1% /run/user/1000
tmpfs is a temporary filesystem that resides in memory and/or your swap partition(s), depending on how much you fill it up.
Mounting directories as tmpfs can be an effective way of speeding up accesses to their files, or to ensure that their contents are automatically cleared upon reboot.

/dev

/dev contains device files which are created and removed automatically by the udev daemon, as hardware is added or removed etc.
(devtmps is just a tmpfs that was created specially by the kernel early in the boot process, which contains the core devices pre-created so that the boot process has something to work with before udevd is loaded.)

/dev/shm

/dev/shm is used by the POSIX shared memory facilities.

/run

/run contains resource locks and PID files etc. which are relevant to currently-running daemons. /var/run and /var/lock are symlinks back to /run for compatibility reasons.

/media

/media contains the mount-points of removable media (e.g. optical discs and USB drives), which are created and removed automatically.

/sys/fs/cgroup

/sys/fs/cgroup contains details for the cgroup system, which is used (mainly by systemd) to divide processes into groups for resource sharing etc.