Question sent in by Nathan from Quebec.
Q: I just recently installed CentOS 7 Linux and am so confused. How do I restart services like sshd and crond?
A: Red Hat 7 and CentOS 7 have now moved to systemd as their default system management daemon. Systemd is different from the old default init system in too many ways to describe here. But let’s get to the meat of your question.
The new way to control system daemons is with the systemctl command. If you are used to the old init scripts (e.x. /etc/init.d/sshd) the new syntax can be slightly confusing. For example, this is how we used to restart SSHD with the old upstart init scripts:
/etc/init.d/sshd restart
or
service sshd restart
In systemd (Fedora 18 or above, RHEL 7, and CentOS 7) we need to use the systemctl command.
To restart a service using systemctl
systemctl restart sshd.service
To start a service using systemctl
systemctl start sshd.service
To stop a service using systemctl
systemctl stop sshd.service
To check the status of a service using systemctl
systemctl status sshd.service
The old /etc/init.d/ scripts are still available for some services for legacy support and backward compatibility. But you should get into the habit of using the new systemctl command because they can be removed in future updates or releases.
For information on how to make sure your service starts at boot see Start Services on Boot in Red Hat 7 or CentOS 7.
NOTE: You will need to use root or sudo since these commands require elevated privileges.
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
13 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
- Uncategorized (1)
Thanks a lot!
Thank you.
where is the fracking scripts? I need to change somethings.. god I hate when CentOs changes just like that. :_( 12 years of GNU/Linux already - Im getting too old for this c&¨%@P.
it is not the Centos changing just like that the new systemd followed by most linux and it has its own pros and cons and some are even saying it is completely new path the linux has take, before it was one package that does one thing exceptionally good and now read https://en.wikipedia.org/wiki/Systemd
Exactly, too old for this sh*t. Give me back my Init.
Agreed!
The init system was far easier in that you did not need to know the exact spelling of the service, you just /etc/init.d/ss tab tab and if it was ssh or sshd or as now sshd.service it just appeared. I made a link up to the init.d dir in the / dir called apps and all I had to do was /apps/ss tab tab.
I guess I can make an alias that would work something like that once I get my head around this. yep definitely getting to old for change but I suppose I will survive
Tab still works with the systemctl command. For example systemctl start ssh will bring up sshd.
I am not impressed either by RH/Cents choice of leaving the standard for years for stopping and starting services. I am 40yr Unix vet I choose Linux because of training and serviceability. We will not be migrating future servers from 6.8.
WOW, what a bunch of dinosaurs. First of all, systemd was brought to you thanks to Red Hat (RH employees created it). The CentOS team has nothing to do with package selection and versioning, they release whatever RH does. Sure systemd is new and you have to manage services differently than the old SYS V init, but is it really that big of a deal? No need to migrate from RHEL/CentOS 6.8, just remember to turn them off on 11/30/2020 when they go EOL, no need to have more attack vectors connected to the internet than necessary. Oh, I here the 1990's in the background, they want your flip-phone back too 😉
I have beedn rolling unix, for over 30 years and have developed AIX device drivers as well as done other internal work and I say systemd blows, plain and simple, and it means some stuff has to change beyond simply using new commands either. Unless you are doing vanilla stuff. And far as attack vectors you could turn unused services of easy enough just as you can with systemd crap. On and BTW, flip phones are pretty secure.
Agree with this post in general. Redhat (and linux in general) has gotten a habit of including everything that gets created by anyone anywhere in the world. Even if someone created something cool that's used to 50% of the people, I will only want that as optional, not included in Linux. systemd definitely falls in that category.
Automating the restart of services in Red Hat 7 / CentOS 7 using `systemctl` can be done using various methods, such as shell scripts, cron jobs, or systemd timers. Here's how you can do it using a simple systemd timer:
Step 1: Create a Service Unit File
1. Create a service unit file (`.service` extension) in the `/etc/systemd/system` directory. Replace `my-service` with the actual name of the service you want to restart.
```bash
sudo nano /etc/systemd/system/my-service-restart.service
```
2. Add the following content, modifying the `Description` and `ExecStart` lines as needed:
```plaintext
[Unit]
Description=Restart My Service
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart my-service
```
3. Save and close the file.
Step 2: Create a Timer Unit File
1. Create a timer unit file (`.timer` extension) in the same directory as above:
```bash
sudo nano /etc/systemd/system/my-service-restart.timer
```
2. Add the following content, modifying the `OnUnitActiveSec` line to specify the interval for restarting the service. For example, to restart every day at midnight:
```plaintext
[Unit]
Description=Restart My Service Daily
[Timer]
OnCalendar=daily
AccuracySec=1s
[Install]
WantedBy=timers.target
```
3. Save and close the file.
Step 3: Start and Enable the Timer
1. Start the timer:
```bash
sudo systemctl start my-service-restart.timer
```
2. Enable the timer to run at boot:
```bash
sudo systemctl enable my-service-restart.timer
```
The timer unit is now set to restart the specified service at the defined interval. You can adjust the timer settings according to your needs. You can also manage the timer using `systemctl` commands:
- To check the status of the timer: `systemctl status my-service-restart.timer`
- To stop the timer: `sudo systemctl stop my-service-restart.timer`
- To disable the timer: `sudo systemctl disable my-service-restart.timer`
Remember to replace `my-service` with the actual name of the service you want to restart. Also, ensure that the service you're restarting is safe to be restarted automatically without causing disruptions.