How to Run Any Specific Command or Script on Linux Startup

1. Using cron:

The cron method is convenient for running commands or scripts at startup. The @reboot directive in the crontab allows you to specify tasks to be run when the system starts.

Open the crontab file

crontab -e

Add the following line:

@reboot /path/to/your/script.sh

Save and exit the editor. This ensures that your script will run each time the system reboots.

2. Using rc.local:

The /etc/rc.local file is traditionally used to run commands at the end of the system boot process.

Open the rc.local file

sudo nano /etc/rc.local

Add your command or script just before the exit 0 line:

/path/to/your/script.sh

Save and exit. Make sure the file is executable:

sudo chmod +x /etc/rc.local

This method may not be available on all distributions, as some are moving away from using rc.local in favor of systemd.

3. Using systemd:

Systemd is a modern init system used by many Linux distributions. You can create a systemd service to execute your script at startup.

Create a new service file, for example, /etc/systemd/system/myscript.service:

[Unit]
Description=My Startup Script

[Service]
ExecStart=/path/to/your/script.sh

[Install]
WantedBy=default.target

Reload systemd and enable/start the service:

sudo systemctl daemon-reload
sudo systemctl enable myscript.service
sudo systemctl start myscript.service

This method provides more control and flexibility and is widely used in modern Linux distributions.

4. Using ~/.bashrc or ~/.bash_profile (for user-specific commands):

If you want a command or script to run when a specific user logs in, you can add it to the ~/.bashrc or ~/.bash_profile file.

Open the .bashrc file

nano ~/.bashrc

Add your command or script at the end of the file:

/path/to/your/script.sh

Save and exit the editor. This method is user-specific and will run the script when the user logs in.

Remember to replace /path/to/your/script.sh with the actual path to your script or command in each case. The appropriate method may vary depending on your distribution and system configuration.

FortiGate Auto Backup to SFTP configuration

Hello everyone, today I am going to show you how to automatically back up your FortiGate configuration. As you know, backing up the configuration is crucial for every network engineer. Sometimes, network engineers forget to download backups of their configurations. If you follow along with me in this video, your firewall configuration will be automatically backed up every day. Additionally, every time an admin user logs in to the FortiGate, it will also generate the configuration and upload it to SFTP.

Step 1: Access the FortiGate Web Interface

  1. Open a web browser and enter the IP address or hostname of your FortiGate device to access its web interface.

Step 2: Log in 2. Log in to the FortiGate web interface with administrative credentials.

Step 3: Configure the SFTP Server

a. Navigate to System > Config > Features. b. Locate the “Backup” section and ensure that “Enable SFTP” is enabled. This allows the FortiGate device to communicate with the SFTP server for backup purposes.

Step 4: Create a Backup Profile

a. Go to System > Admin > Settings. b. Under Backup, you’ll find the “Backup Profiles” section. Click on the “Create New” button to create a new backup profile.

Step 5: Configure the Backup Profile

a. In the “Create New Backup Profile” window, provide a descriptive name for the profile. This name will help you identify the backup profile later. b. Select the frequency at which you want backups to occur. You can choose from options like daily, weekly, or monthly. c. Specify the time of day when the backup should be initiated. Choose a time that is convenient and doesn’t disrupt your network operations. d. Under the “Backup Location” section, select “SFTP Server” as the backup destination.

Step 6: Configure SFTP Server Settings

a. After selecting “SFTP Server,” you’ll need to enter the following details for your SFTP server: – Server IP Address or Hostname: This is the address of your SFTP server where backups will be sent. – Port: Typically, SFTP uses port 22, but ensure it matches your SFTP server’s configuration. – Username: Provide the SFTP username for authentication. – Password: Enter the password associated with the SFTP username. – Directory: Specify the directory on the SFTP server where you want to store the FortiGate backups.

Step 7: Schedule the Backup

a. After configuring the SFTP server settings, go to System > Config > Backup. b. Click on “Create New” to create a new backup schedule. c. In the “Create New Backup Schedule” window: – Select the backup profile you created in the previous step from the dropdown menu. – Choose the days of the week for backups (for weekly backups) or the day of the month (for monthly backups).

Step 8: Review and Apply Configuration

a. Review your backup configuration to ensure that all settings are accurate and complete. b. Click “Apply” or “OK” to save and apply the changes.

With these detailed steps, your FortiGate device is now configured to automatically back up its configuration to the specified SFTP server at the scheduled time and frequency you defined. Regularly verify the backups to ensure they are functioning correctly and provide a reliable safeguard for your firewall’s settings.