├── LICENSE ├── README.md └── minecraft@.service /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Klaus Frank 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MinecraftSystemdUnit 2 | Systemd Unit file for Minecraft Server 3 | 4 | ## Installation 5 | 6 | 1. Connect to the server or if you want to run the server on your machine, open a terminal 7 | 1. Become root using `su` or `sudo` 8 | - To validate that you're now root, run `id` 9 | 1. Next install the necessary packages 10 | ```bash 11 | apt-get install -y openjdk-11-jre-headless curl screen nano bash grep 12 | ``` 13 | 1. Create the `/opt` folder if it doesn't already exist 14 | ```bash 15 | mkdir /opt 16 | ``` 17 | 1. Now you need to create the user for the service 18 | ```bash 19 | adduser --system --shell /bin/bash --home /opt/minecraft --group minecraft 20 | chmod +t /opt/minecraft 21 | ``` 22 | 1. Create the Systemd Unit file: 23 | ```bash 24 | nano /etc/systemd/system/minecraft@.service 25 | # paste the contents of minecraft@.service 26 | ``` 27 | or 28 | ```bash 29 | curl https://raw.githubusercontent.com/agowa338/MinecraftSystemdUnit/master/minecraft%40.service > /etc/systemd/system/minecraft@.service 30 | ``` 31 | 32 | ## Setup Instance 33 | 34 | Each server has it's own subdirectory of `/opt/minecraft`. This means that when a new server is being added, the following steps need to be followed: 35 | 36 | 1. Create a subdirectory of `/opt/minecraft` for your Minecraft server, we'll be using `feed-the-beast` as an example 37 | - It is recommended to use a name which only includes lowercase characters, spaces should be replaced with dashes 38 | ```bash 39 | cd /opt/minecraft 40 | mkdir feed-the-beast 41 | ``` 42 | 1. Upload your files to the `/opt/minecraft/feed-the-beast` directory 43 | - **Note**: All uploaded files should be owned by the `minecraft` user 44 | ```bash 45 | chown -R minecraft:minecraft /opt/minecraft/feed-the-beast 46 | ``` 47 | 1. Accept the Minecraft server's EULA 48 | ```bash 49 | echo 'eula=true' > eula.txt 50 | 51 | ### RAM allocation 52 | 53 | With `minecraft@.service` it's possible to specify the RAM allocation per server. This can be done using a `server.conf` file in the server's directory. 54 | 55 | **Example** 56 | 57 | ```properties 58 | # /opt/minecraft/ftb-beyond/server.conf 59 | MCMINMEM=512M 60 | MCMAXMEM=2048M 61 | ``` 62 | 63 | ### Feed the Beast 64 | 65 | Some Feed the Beast packs include a `FTBInstall.sh`. This script needs to be executed before starting the server. 66 | 67 | ```bash 68 | cd /opt/minecraft/ftb-beyond 69 | su -c 'bash /opt/minecraft/ftb-beyond/FTBInstall.sh' minecraft 70 | ``` 71 | 72 | ## Usage 73 | 74 | ### Enable auto-start Minecraft server on boot 75 | 76 | ```bash 77 | systemctl enable minecraft@ftb-beyond 78 | ``` 79 | 80 | ### Disable auto-start Minecraft server on boot 81 | 82 | ```bash 83 | systemctl disable minecraft@ftb-beyond 84 | ``` 85 | 86 | ### Start Minecraft server manually 87 | 88 | ```bash 89 | systemctl start minecraft@ftb-beyond 90 | ``` 91 | 92 | ### Stop Minecraft server manually 93 | 94 | ```bash 95 | systemctl stop minecraft@ftb-beyond 96 | ``` 97 | 98 | ### Connecting to the Minecraft server console 99 | 100 | To enter the console, [`screen`](https://linux.die.net/man/1/screen) is used. 101 | 102 | All screen sessions are owned by the `minecraft` user and are prefixed with `mc-`. 103 | 104 | ```bash 105 | # This will attach to the Minecraft server called `ftb-beyond` 106 | su -c 'screen -r mc-ftb-beyond' minecraft 107 | ``` 108 | 109 | **Note**: To detach (exit) from the session, press CTRL + A followed by D. 110 | -------------------------------------------------------------------------------- /minecraft@.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Minecraft Server %i 3 | After=network.target 4 | 5 | [Service] 6 | WorkingDirectory=/opt/minecraft/%i 7 | 8 | PrivateUsers=true 9 | # Users Database is not available for within the unit, only root and minecraft is available, everybody else is nobody 10 | 11 | User=minecraft 12 | Group=minecraft 13 | 14 | ProtectSystem=full 15 | # Read only mapping of /usr /boot and /etc 16 | 17 | ProtectHome=true 18 | # /home, /root and /run/user seem to be empty from within the unit. It is recommended to enable this setting for all long-running services (in particular network-facing ones). 19 | 20 | ProtectKernelTunables=true 21 | # /proc/sys, /sys, /proc/sysrq-trigger, /proc/latency_stats, /proc/acpi, /proc/timer_stats, /proc/fs and /proc/irq will be read-only within the unit. It is recommended to turn this on for most services. 22 | # Implies MountFlags=slave 23 | 24 | ProtectKernelModules=true 25 | # Block module system calls, also /usr/lib/modules. It is recommended to turn this on for most services that do not need special file systems or extra kernel modules to work 26 | # Implies NoNewPrivileges=yes 27 | 28 | ProtectControlGroups=true 29 | # It is hence recommended to turn this on for most services. 30 | # Implies MountAPIVFS=yes 31 | 32 | # Set default memory values 33 | Environment="MCMINMEM=512M" "MCMAXMEM=1024M" "SHUTDOWN_DELAY=5" "POST_SHUTDOWN_DELAY=10" 34 | # Change memory values in environment file 35 | EnvironmentFile=-/opt/minecraft/%i/server.conf 36 | 37 | # Uncomment this to fix screen on RHEL 8 38 | #ExecStartPre=+/bin/sh -c 'chmod 777 /run/screen' 39 | 40 | ExecStart=/bin/sh -c \ 41 | 'find -L . \ 42 | -maxdepth 1 \ 43 | -type f \ 44 | -iregex ".*/\\(mohist\\|FTBServer\\|craftbukkit\\|spigot\\|paper\\|forge\\|minecraft_server\\).*jar" \ 45 | -print0 \ 46 | -quit \ 47 | | xargs -0 -I{} \ 48 | /usr/bin/screen -DmS mc-%i \ 49 | /usr/bin/java \ 50 | -server \ 51 | -Xms${MCMINMEM} \ 52 | -Xmx${MCMAXMEM} \ 53 | -XX:+UseG1GC \ 54 | -XX:ParallelGCThreads=2 \ 55 | -XX:MinHeapFreeRatio=5 \ 56 | -XX:MaxHeapFreeRatio=10 \ 57 | -Dlog4j2.formatMsgNoLookups=True \ 58 | -jar {} \ 59 | nogui' 60 | 61 | ExecReload=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "reload"\\015' 62 | 63 | ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."\\015' 64 | ExecStop=/bin/sh -c '/bin/sleep ${SHUTDOWN_DELAY}' 65 | ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\\015' 66 | ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\\015' 67 | ExecStop=/bin/sh -c '/bin/sleep ${POST_SHUTDOWN_DELAY}' 68 | 69 | Restart=on-failure 70 | RestartSec=60s 71 | 72 | [Install] 73 | WantedBy=multi-user.target 74 | 75 | ######### 76 | # HowTo 77 | ######### 78 | # 79 | # Create a directory in /opt/minecraft/XX where XX is a name like 'survival' 80 | # Add minecraft_server.jar into dir with other conf files for minecraft server 81 | # 82 | # Enable/Start systemd service 83 | # systemctl enable minecraft@survival 84 | # systemctl start minecraft@survival 85 | # 86 | # To run multiple servers simply create a new dir structure and enable/start it 87 | # systemctl enable minecraft@creative 88 | # systemctl start minecraft@creative 89 | # 90 | # To change specific server memory assignment, create file /opt/minecraft/XX/server.conf (where XX is your server name) and add below lines: 91 | # MCMINMEM=512M 92 | # MCMAXMEM=2048M 93 | 94 | --------------------------------------------------------------------------------