You are not logged in.

#1 2018-03-02 09:45:28

Icarus_Radio
Member
Registered: 2017-02-28
Posts: 45

[SOLVED] Need help with service management

I use NetworkManager to acquire DNS automatically, but it always gives me a wrong DNS, so I want to create a new service to disable the wrong DNS

The unit file is "FDD.service"

[unit]
Description=Fix Dead DNS

[Service]
ExecStart=/usr/bin/FixDeadDNS
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
After=NetworkManager.service, dhcpcd.service

And the script is

#!/bin/bash

grep -q "nameserver 10.13.6.2" "/etc/resolv.conf" && (grep -q "#nameserver 10.13.6.2" "/etc/resolv.conf" || sed -i 's/nameserver 10.13.6.2/#nameserver 10.13.6.2/' "/etc/resolv.conf")

However, every time I start my computer, when I check the status of FDD.service, it always tells me an error has occurred

$ systemctl status FDD.service
FDD.service
   Loaded: loaded (/usr/lib/systemd/system/FDD.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2018-03-02 17:32:52 CST; 22s ago
  Process: 330 ExecStart=/usr/bin/FixDeadDNS (code=exited, status=1/FAILURE)
 Main PID: 330 (code=exited, status=1/FAILURE)

Mar 02 17:32:52 icarusradio systemd[1]: Started FDD.service.
Mar 02 17:32:52 icarusradio systemd[1]: FDD.service: Main process exited, code=exited, status=1/FAILURE
Mar 02 17:32:52 icarusradio systemd[1]: FDD.service: Failed with result 'exit-code'.

I can manually start it after startup

# systemctl start FDD.service
$systemctl status FDD.service
FDD.service
   Loaded: loaded (/usr/lib/systemd/system/FDD.service; enabled; vendor preset: disabled)
   Active: active (exited) since Fri 2018-03-02 17:33:46 CST; 8min ago
  Process: 1255 ExecStart=/usr/bin/FixDeadDNS (code=exited, status=0/SUCCESS)
 Main PID: 1255 (code=exited, status=0/SUCCESS)

Mar 02 17:33:46 icarusradio systemd[1]: Started FDD.service.

What should I do so that I can make it start automatically?

Last edited by Icarus_Radio (2018-03-03 00:16:39)

Offline

#2 2018-03-02 10:01:38

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,435

Re: [SOLVED] Need help with service management

Your script runs before Networkmanager creates the connection and thus before NetworkManager changes the file, the grep fails, returns a non 0 exit code and thus the service fails. If you really feel there's no other way, you want to have Wants=network-online.target and After=network-online.target in the [Unit] section and

systemctl enable NetworkManager-wait-online.service

- ref: https://bbs.archlinux.org/viewtopic.php … 9#p1665179 the last service config.

However it rather reads like you really actually want to just handle DNS yourself, in which case you should follow https://wiki.archlinux.org/index.php/Re … ifications

Also really questionable, why do you have After=dhcpcd.service ? Do you have that enabled, while also having NetworkManager enabled? That might be the original cause for your issue, don't enable multiple services handling your network, what's the output of

 find /etc/systemd

Last edited by V1del (2018-03-02 11:28:12)

Offline

#3 2018-03-02 11:55:47

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: [SOLVED] Need help with service management

That script is also ... a bit odd.  Why two checks with grep?  Just anchor the first one to the start of the line.

Better yet, don't bother with grep at all, just use sed.  There is no need to check if the line is there to be commented, just tell sed to comment it regardless, if it is not there this will have no effect:

sed -i '/^nameserver 10.13.6.2/s/^/#/' /etc/resolv.conf

Or, even simpler, as I see no point in leaving it there commented out:

sed -i '/nameserver 10.13.6.2/d' /etc/resolv.conf

However, in any case, I suspect properly configuring NetworkManager would be a far better solution than letting NM get it wrong then trying to patch it up.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2018-03-02 17:19:24

cafe
Member
From: Brasil
Registered: 2014-03-20
Posts: 165

Re: [SOLVED] Need help with service management

You may need to investigate why your router is providing a wrong DNS.

Offline

#5 2018-03-02 22:50:28

Icarus_Radio
Member
Registered: 2017-02-28
Posts: 45

Re: [SOLVED] Need help with service management

Here's the answer for the DNS thing: the network at campus is managed by the IT department, they did something bad (dead DNS) and never fix it even though we have complained.
Also I cannot modify NM since I sometimes connect to other wifi and the current DNS surely will not work. Imo, letting NM get it wrong and patch it up is much more convenient.

I have modify the unit file according to V1del and the script according to Trilby (latter one), but nothing has changed at all sad

The modified unit file:

[unit]
Description=Fix Dead DNS
Wants=network-online.target 
After=network-online.target 

[Service]
ExecStart=/usr/bin/FixDeadDNS
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

I also noticed why the DNS never changed as I made a copy of resolv.conf before the edit command. Surprisingly, resolv.conf is empty when I try to modify it. I just wondered why.

P.S. just another irrelevant question: even though the dead DNS is at the first line, it does not affect something like browsing the Internet. Also, when I am using Windows, nothing happened. However, when I do command line jobs in linux, such as ping or pacman update, it just stucks.

Last edited by Icarus_Radio (2018-03-02 22:59:19)

Offline

#6 2018-03-02 23:08:08

loqs
Member
Registered: 2014-03-06
Posts: 19,113

Re: [SOLVED] Need help with service management

What is the contents of /usr/bin/FixDeadDNS now?
What is the output of

$ systemctl list-unit-files --state=enabled

Offline

#7 2018-03-02 23:28:28

Icarus_Radio
Member
Registered: 2017-02-28
Posts: 45

Re: [SOLVED] Need help with service management

/usr/bin/FixDeadDNS

#!/bin/bash
cp /etc/resolv.conf /home/icarus/a.conf #I want to see why
sed -i '/nameserver 10.13.6.2/d' /etc/resolv.conf

And the output of that is

UNIT FILE                                   STATE  
autovt@.service                             enabled
bumblebeed.service                          enabled
dbus-org.freedesktop.NetworkManager.service enabled
dbus-org.freedesktop.nm-dispatcher.service  enabled
display-manager.service                     enabled
FDD.service                                 enabled
gdm.service                                 enabled
getty@.service                              enabled
NetworkManager-dispatcher.service           enabled
NetworkManager-wait-online.service          enabled
NetworkManager.service                      enabled
remote-fs.target                            enabled

12 unit files listed.

Last edited by Icarus_Radio (2018-03-02 23:30:47)

Offline

#8 2018-03-02 23:36:17

loqs
Member
Registered: 2014-03-06
Posts: 19,113

Re: [SOLVED] Need help with service management

What is the status of FDD.service, NetworkManager.service and NetworkManager-wait-online.service?
Is /etc/resolve.conf empty?  What is the output of `stat /etc/resolv.conf`
Edit:
duplicate FDD.service should be NetworkManager-wait-online.service

Last edited by loqs (2018-03-02 23:36:58)

Offline

#9 2018-03-02 23:49:32

Icarus_Radio
Member
Registered: 2017-02-28
Posts: 45

Re: [SOLVED] Need help with service management

$ systemctl status FDD.service
● FDD.service
   Loaded: loaded (/usr/lib/systemd/system/FDD.service; enabled; vendor preset: disabled)
   Active: active (exited) since Sat 2018-03-03 07:41:46 CST; 1min 44s ago
  Process: 327 ExecStart=/usr/bin/FixDeadDNS (code=exited, status=0/SUCCESS)
 Main PID: 327 (code=exited, status=0/SUCCESS)

$ systemctl status NetworkManager.service
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/NetworkManager.service.d
           └─NetworkManager-ovs.conf
   Active: active (running) since Sat 2018-03-03 07:41:46 CST; 1min 45s ago
     Docs: man:NetworkManager(8)
 Main PID: 326 (NetworkManager)
    Tasks: 3 (limit: 4496)
   CGroup: /system.slice/NetworkManager.service
           └─326 /usr/bin/NetworkManager --no-daemon

Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3288] dhcp4 (wlp5s0): state changed unknown -> bound
Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3322] device (wlp5s0): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'managed')
Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3334] device (wlp5s0): state change: ip-check -> secondaries (reason 'none', sys-iface-state: 'managed')
Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3339] device (wlp5s0): state change: secondaries -> activated (reason 'none', sys-iface-state: 'managed')
Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3342] manager: NetworkManager state is now CONNECTED_LOCAL
Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3385] manager: NetworkManager state is now CONNECTED_SITE
Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3387] policy: set 'ShanghaiTech' (wlp5s0) as default for IPv4 routing and DNS
Mar 03 07:41:51 icarusradio NetworkManager[326]: <info>  [1520034111.3413] device (wlp5s0): Activation: successful, device activated.
Mar 03 07:41:52 icarusradio NetworkManager[326]: <info>  [1520034112.2678] manager: startup complete
Mar 03 07:42:19 icarusradio NetworkManager[326]: <info>  [1520034139.0881] manager: NetworkManager state is now CONNECTED_GLOBAL

$systemctl status NetworkManager-wait-online.service
● NetworkManager-wait-online.service - Network Manager Wait Online
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager-wait-online.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:nm-online(1)

$ stat /etc/resolv.conf
  File: /etc/resolv.conf
  Size: 75        	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 395003      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-03-03 07:41:51.345304146 +0800
Modify: 2018-03-03 07:41:51.338304146 +0800
Change: 2018-03-03 07:41:51.340304146 +0800
 Birth: -

When we run FixDeadDNS, the resolv.conf is only one line "# Generated by NetworkManager"
After I entered GNOME, the DNS is

# Generated by NetworkManager
nameserver 10.13.6.2
nameserver 10.15.44.202

And I only want the second one.

Offline

#10 2018-03-03 00:03:20

progandy
Member
Registered: 2012-05-17
Posts: 5,322

Re: [SOLVED] Need help with service management

As an alternative to this service I think you can configure networkmanager to use openresolv and then set its name_server_blacklist  option.
https://wiki.archlinux.org/index.php/Ne … resolvconf
https://manned.org/resolvconf.conf.5


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#11 2018-03-03 00:16:23

Icarus_Radio
Member
Registered: 2017-02-28
Posts: 45

Re: [SOLVED] Need help with service management

progandy wrote:

As an alternative to this service I think you can configure networkmanager to use openresolv and then set its name_server_blacklist  option.
https://wiki.archlinux.org/index.php/Ne … resolvconf
https://manned.org/resolvconf.conf.5

Thanks, that's very helpful!

Offline

Board footer

Powered by FluxBB