rc.local only executing after connecting to ethernet?

5,490

By default, /etc/rc.local runs after the network connection is established. This is specified in the drop-in file /lib/systemd/system/rc-local.service.d/debian.conf:

$ sudo systemctl status rc.local
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (exited) since Sat 2017-02-11 00:31:50 EET; 2h 33min ago
    Tasks: 0
   Memory: 0B
      CPU: 0

Feb 11 00:31:50 libellio systemd[1]: Starting /etc/rc.local Compatibility...
Feb 11 00:31:50 libellio systemd[1]: Started /etc/rc.local Compatibility.

$ cat /lib/systemd/system/rc-local.service.d/debian.conf
[Unit]
# not specified by LSB, but has been behaving that way in Debian under SysV
# init and upstart
After=network-online.target

# Often contains status messages which users expect to see on the console
# during boot
[Service]
StandardOutput=journal+console
StandardError=journal+console

While this default setting is fine for the kind of services which are usually started from rc.local, for your specific use-case you may want to change that setting.

Copy the service file /lib/systemd/system/rc-local.service to /etc/systemd/system. Then opy the drop-in configuration file to /etc/systemd/system/rc-local.service.d/ (make the directory if needed), then edit the copy and comment out the line After=network-online.target.

Share:
5,490

Related videos on Youtube

Hector Muñoz H
Author by

Hector Muñoz H

Updated on September 18, 2022

Comments

  • Hector Muñoz H
    Hector Muñoz H over 1 year

    I am new to ubuntu but I decided to add an instruction to rc.local so it changes my brightness settings, because it is always starting on max. brightness.

    Get this: it works only when I am connected to the internet via ethernet. If I turn on my laptop without the ethernet cable, it will remain at the max brightness, it will only execute rc.local once I connect the ethernet cable, or if it is connected before turning on my computer the rc.local will execute normally and change my brightness level to the desired value.

    Here is my rc.local

     #!/bin/sh -e
     #
     # rc.local
     #
     # This script is executed at the end of each multiuser runlevel.
     # Make sure that the script will "exit 0" on success or any other
     # value on error.
     #
     # In order to enable or disable this script just change the execution
     # bits.
     #
     # By default this script does nothing.
    
     echo 1466 > /sys/class/backlight/intel_backlight/brightness
    
     exit 0
    
  • deltab
    deltab over 7 years
    Note that the files under /lib shouldn't be edited there: make a copy in the corresponding place under /etc and edit that instead; you can give it a different name to make it clear what it's for.
  • AlexP
    AlexP over 7 years
    @deltab: Thanks for reminding. I have edited the answer.
  • deltab
    deltab over 7 years
    It might be better to make it a separate service, using ExecStart and Type=oneshot; or, as it's just writing to a file, maybe a tmpfiles.d entry.
  • Hector Muñoz H
    Hector Muñoz H over 7 years
    Hey thanks a lot for your comments! I've done what you told me ( create /etc/systemd/system/rc-local.service.d/ and then cp the debian.conf into that directory, finally commenting the After = network-online.target). But I am guessing I missed something because it is not working yet, maybe I have to invalidate /lib/systemd/system/rc-local.service.d/debian.conf ?? So that the system will only do what I wrote in /etc/systemd/system/rc-local.service.d/debian.conf?
  • AlexP
    AlexP over 7 years
    @HectorMuñozH: Please show sudo systemctl status rc.local.
  • Hector Muñoz H
    Hector Muñoz H over 7 years
    Ok I've edited the question to show that output. I know it may have a simple solution but I can't see it :( thanks for your time.
  • AlexP
    AlexP over 7 years
    @HectorMuñozH: Sorry, I forgot to tell you to also copy the service file itself /lib/systemd/system/rc-local.service to /etc/systemd/system. (The drop-in files need to be in the same directory as the unit file.) Then do a sudo systemctl daemon-reload to update the configuration without rebooting.
  • Hector Muñoz H
    Hector Muñoz H over 7 years
    Hey, so I did it, copied rc-local.service to /etc/systemd/system, and then I also commented there the line with After = network.target and it's working now! But I just want to be sure that I have to comment that line in both documents (rc-local.service and also in debian.conf ). If that is the case then the question was answered completely!
  • AlexP
    AlexP over 7 years
    @HectorMuñozH: You will notice that there is a subtle difference between them: one says After=network.target and the other After=network-online.target. I think that you want rc-local.service to run after network.target; for certain after local-fs.target. Those targets are essentially points where some functionality is known to be available; for example, saying After=local-fs.target ensures that all local filesystems declared in /etc/fstab are mounted.
  • Hector Muñoz H
    Hector Muñoz H over 7 years
    Well, commenting both After=network.target and After= network-online.target did the trick for me. Because if only After= network-online.target is commented, the rc.local wont execute, unless the ethernet cable is plugged in. My question is that if my rc.local is only about setting a brightness setting, can I comment both After=network and online.target and nothing bad will happen in long term?
  • AlexP
    AlexP over 7 years
    @HectorMuñozH: The system itself does not use rc.local. It is create as a for the system administrator to write local initialization scripts; that is, it is entirely under your control.
  • Hector Muñoz H
    Hector Muñoz H over 7 years
    then thanks a lot, I hope I could do more to give you reputation points or something, I am very grateful!