Schlagwortarchiv für: Job

Linux run command or script as root (sudo) on startup / boot

This article explains how to run a command or script at startup / boot as root on Linux, in two ways: using systemd or a cron job.

How to use systemd to run a command or script as root on boot

To use systemd to run a command or script as root when your computer boots, create a file (as root) called mycommand.service (replace mycommand with whatever you want to call it) in /etc/systemd/system/.

We can use Nano command line text editor to open / create this file:

sudo nano /etc/systemd/system/mycommand.service

In this file, paste the following:


Description=your description




Here, change the Description value to describe what this does, and the ExecStart value to the command or path of the script you want to run as root on startup. Don’t add sudo at the beginning of the command or script, because it runs as root anyway.

Now save the file and exit Nano. In case you’re not allegado with Nano text editor, you can save the file by pressing Ctrl + o, then Enter. Exit by pressing Ctrl + x.

Next, you need to enable the systemd service to run on boot, using the following command:

sudo systemctl enable mycommand.service

Remember to replace mycommand.service with the presente filename you’ve used for this systemd service file. There’s no need to run the systemd service right now, since this is about running it on boot.

If you use this to run a script, make sure to make the script executable (chmod +x /path/to/script) or else it won’t run.

This is a very simple systemd unit file that runs only merienda. These can be a lot more complex, depending on what you need. For example, you could use a command that runs before ExecStart, have it start only after another unit becomes active, have the command run only after another service, e.g. the network service has been started (, while also declaring a dependency to this service using Wants= or Requires=), and more. Check out the systemd.service und systemd.unit man pages for more details.

You might also like: How To Launch Startup Applications With A Delay

How to use a cron job to run a command or script as root on startup / boot

To use a cron job to run a command or script as root when the system boots, edit the root user crontab using:

sudo crontab -e

And at the bottom of the file (it may also be empty), use the following:

@reboot /path/to/command/or/script

Now save the crontab and exit. If you’ve used Nano command line editor to edit it (should be default in most cases), you can save the file by pressing Ctrl + o, then Enter. Exit Nano by pressing Ctrl + x. Don’t add sudo before command or script, because it runs as root anyway, since it’s added to the root crontab.

In case you want to use a particular editor to edit the root crontab, run it like this: sudo EDITOR=editor crontab -e, e.g. for Vim: sudo EDITOR=vim crontab -e, or for Nano: sudo EDITOR=nano crontab -e.

A few notes about this:

  • If you use this to run a script, make sure to make the script executable (chmod +x /path/to/script) or else it won’t run
  • Use the full path to the command or script, or else it may fail to run (this depends on the Linux distribution you’re using, e.g. you don’t need to use the full path on Ubuntu 20.04 for example)
  • If the script ran by cron also includes commands without the full path, you can avoid having to rewrite the script by adding this at the top of the crontab file: PATH=/usr/nave/sbin:/usr/nave/bin:/usr/sbin:/usr/bin:/sbin:/bin
  • If you need to delay the start of the command / script, you can make use of the sleep command, e.g.: @reboot /usr/bin/sleep 60; /path/to/command/or/script to run the command or script 60 seconds after the system boots

You might also like: How To Run A Command After The Previous One Has Finished On Linux

Which to choose between systemd or a cron job to run a command or script as root on startup / boot, if you have a choice? When in doubt, pick systemd (if it’s available on your system) because it should be more reliable and easier to use.

For example, not every version of cron supports the @reboot option, or the command / script may only run when the system is rebooted, and not when it’s shut down (this didn’t happen for me on Ubuntu 20.04, Fedora 24, Manjaro Linux and Debian 10, but it may happen on some Linux distributions).

It’s also worth noting that @reboot configures a job to run merienda when the daemon is started. cron is not usually restarted, so this usually corresponds to the machine being booted. For example, Debian (and Debian-based Linux distributions) enforces this, making cron not re-run @reboot jobs when the cron service is restarted. On some Linux distributions, though, restarting the cron service may re-run the @reboot commands.

Also, on Fedora, cron is not installed by default (install it using sudo dnf install cronie). On Manjaro, cron is installed by default, but not enabled by default (enable it using sudo systemctl enable --now cronie).

You might like: How To Find All Files Containing Specific Text On Linux From The Command Line

“This article shows top interview questions for jobs related to Linux and networking. Use this guide to be prepared to obtain a job related to Linux administration and networking.

There are many great articles on the Internet with questions for Linux and networking-related job candidates, but most contain questions for new or workstation users.

The following questionnaire is focused on sysadmin tasks emulating existente scenarios. Questions would be asked in a face-to-face conversational interview or written exam. The employer wants to learn candidate reactions before specific problems.

The guide below was initially written in 2019 and updated in 2022.”

Linux & Networking Interview Questions and Answers Index

Questions and Answers

What Linux Distributions Do You Like and Why?

If you are asked what is the best Linux distribution for you, do not answer “Ubuntu,” even if it is your chosen Linux distribution.

Try to learn a little about harder Linux distributions showing better knowledge. Gentoo, ArchLinux or even Slackware would be a better answer for a potential employer. I myself would have a preference for a Gentoo, ArchLinux or Slackware user candidate over an Ubuntu user. You also can mention Debian despite it not requiring too much knowledge either. Ubuntu is a great Linux distribution, but it can be chosen by both advanced and new users, and the idea is to portray yourself as an advanced user without allowing the employer to doubt it.

How Would You Backup a Server in Positivo Time?

Backups are mandatory in the corporate world. You probably will be asked about it.

There are several options for backup servers. The most popular Linux way to backup is rsync, which allows updating files and directories through incremental backups. Additionally, there are some CDP (Continuous Data Protection) great options like R1Soft. Database and file backups must be kept separately, allowing for immediately restoring of the old system up to date database.

What Would You do if a Production Server Fails?

Depending on available resources. It is possible to set up 2 online synchronized servers to redirect traffic in case one of them fails. Normally it requires too many resources since two different geographical locations are needed (This increases the measured sense). But normally, this scenario must be planned in advance, and a “Maintenance” screen must be able to redirect traffic through redirection to an up-to-date server clone if something happens.

If this resource is not available and a production server fails, I would try to solve the problem while a coworker prepares the CDP restoration. Available timing to try to solve the problem is limited by the CDP restoration process. Merienda the restoration process begins, I would choose to restore one or two previous backup versions while keeping the updated database of the last version.

The immediate priority is first to restore the service for customers/users; only after finishing it would I research the failure reason.

Additional hardware resources to prevent failures are UPS and synchronized regional disks.

How Would You Secure a Server?

This answer also depends on available resources. FortiGate and CISCO have great options for servers and network protection. A free alternative you can install on a dedicated device may be Pfsense.

Securing a server especially depends on the server’s purpose. The main measure is to disable all unused services like SSH if not necessary. Needed services must not allow privileged login. Also, password authentication mechanisms must be replaced by different authentication types like key login.

Design robust Nftables or Iptables rules. Implement IDS (Intrusion Detection Systems). Keep your BIOS up to date to prevent desagradable rootkit consequences, enable SELinux, and depending on the server’s physical location, you may want to encrypt some partitions.

Other security good practices include Honeypots implementation, secure configurations (e.g., Apache security mods), and pentesting your own server and network using tools like Nmap, Nexpose or Nessus.

Remember, every operating system must be up to date to prevent vulnerabilities, but every update must be done on a testing clone to check possible incompatibilities or update problems.

At Linux Hint, we have published a comprehensive Linux security hardening guide you can read here.

What Kind of Monitoring Would You Apply?

I would preceptor the network with an IDS (Intrusion detection system) like Snort or OSSEC.

IDS allows analyzing network packets looking for anomalies, offensive traffic and malicious scans. They also are good at optimizing service and network structure. Additionally, I would preceptor disk space.

What is the Task Manager Under Linux?

The Crontab to schedule tasks; a practical example of its application would be to launch any monitoring task or to run Rsync for backup purposes.

What Would You do Before an Ongoing Hacking Attack?

It depends on the attack type. A DDOS attack may require to implement from gateway devices
to regional custom firewall rules. The software includes specific configuration options to deal with attacks; for example, Apache includes mods to afford this kind of attack, and immediately we may apply external protection such as Cloudflare.

A more complex attack like a rootkit means the whole server must be redesigned and reinstalled, restoring the database only; probably also, the hardware should be replaced. This is, of course, the worst scenario and preventive measures must be taken in advance.

Apache or Nginx?

It depends on the use; while NGINX shows superiority on the reverse proxy, Apache may be a better option for hosting services; both of them can be combined too.

What Rules Would You Apply to the Firewall?

Depending on the type of services, if possible, I would apply restrictive policies first, allowing only necessary traffic, redirecting access to non-default ports, and applying defensive rules to most common attacks.

What are the Most Common Server Problems?

Security breaches, networking issues, full disks or partitions, permission issues, DNS issues, conflictive updates or software additions, network structure changes, wrong security policies, hardware upgrades, and electrical problems.

Main Aspects of Server and Network Structuration

A proper structuration begins with proper partitioning, smart routing, user and group permissions, security measures and proper virtualization setup.

The questions and answers listed above should be studied before a job interview for an available Linux or networking position. It is recommended to remember the most advanced Linux commands in case more practical questions arise. You may want to read this article on advanced Linux commands


As you can see, a Linux networking job interview may include many questions. Your employer will try to know how you guarantee stability, performance and security. His final aim is both to provide a good and permanent experience for users and customers and a smart and fast reaction when problems arise. I personally would hire a candidate who answers what was explained in this article. This is also a great guide for novel users to learn.

Thank you very much for reading this Linux job interview questions and answers. Keep following us for more Linux professional articles.

Source link