Skip to content

Instantly share code, notes, and snippets.

@wonderbeyond
Last active December 10, 2020 02:54
Show Gist options
  • Save wonderbeyond/dfa7051d7e1f0ea419e8e15bc0a66177 to your computer and use it in GitHub Desktop.
Save wonderbeyond/dfa7051d7e1f0ea419e8e15bc0a66177 to your computer and use it in GitHub Desktop.
Let systemd auto start user instances

Reference:

Tips

1. Let systemd keep user instances after last session closes

$ sudo loginctl enable-linger $USER

2. WantedBy=default.target

Don't use WantedBy=multi-user.target.

Example - User service to auto start SSH dynamic port forwarding

  1. Create a unit file ~/.config/systemd/user/dynamic-1086-proxy.service

    [Unit]
    Description=Establishes a sock5 proxy on port 1086 (ssh -D 1086)
    
    [Service]
    ExecStart=/usr/bin/ssh -CND 1086 YOUR-PROXY-SERVER-HOST
    Restart=always
    RestartSec=5
    
    [Install]
    WantedBy=default.target

    Notice: replace YOUR-PROXY-SERVER-HOST with your own proxy/jump server.

  2. enable and start service

    systemctl --user enable --now dynamic-1086-proxy.service
    
    # to check service status
    systemctl --user status dynamic-1086-proxy
  3. Then, if you have SwitchyOmega installed in Chrome or Firefox, you can add a proxy Profile and fill like below:

    SOCKS5 127.0.0.1 1086

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment