how to distinguish between user services and system service in systemd?

According to this documentation, one can distinguish the unit file by its path.

For instance; if the unit file is in the

/etc/systemd/system
/usr/lib/systemd/system
/run/systemd/system

directories, this unit belongs to system.

If it is in the

~/.config/systemd/user/*
/etc/systemd/user/*
$XDG_RUNTIME_DIR/systemd/user/*
/run/systemd/user/*
~/.local/share/systemd/user/*
/usr/lib/systemd/user/*

directories, it belongs to user.


According to the systemd(1) man page for system unit directories:

The systemd system manager reads unit configuration from various directories. Packages that want to install unit files shall place them in the directory returned by:

pkg-config systemd --variable=systemdsystemunitdir

Other directories checked are:

/usr/local/lib/systemd/system
/usr/lib/systemd/system

User configuration always takes precedence.

pkg-config systemd --variable=systemdsystemconfdir

returns the path of the system configuration directory. Packages should alter the content of these directories only with the enable and disable commands of the systemctl(1) tool. Full list of directories is provided in systemd.unit(5).

Then for user unit directories:

Similar rules apply for the user unit directories. However, here the XDG Base Directory specification[6] is followed to find units. Applications should place their unit files in the directory returned by:

pkg-config systemd --variable=systemduserunitdir

Global configuration is done in the directory reported by:

pkg-config systemd --variable=systemduserconfdir

The enable and disable commands of the systemctl(1) tool can handle both global (i.e. for all users) and private (for one user) enabling/disabling of units. Full list of directories is provided in systemd.unit(5).

Note that these are systemd's rules instructing packagers of the proper locations to place different files and it's up to the packager to properly follow these rules.