How to Set Up a Minecraft Server on Your VPS

This tutorial will show you how to set up and configure your own Minecraft server on a VPS.

We will be installing SpigotMC, a highly optimized Minecraft server, written in Java, with integrated support for plugins. In the end, you will have a Minecraft server always ready for you and your friends to play on.

What We Are Going to Cover

  • How to install Java runtime and Git for Ubuntu/Debian and CentOS,
  • How to compile Spigot,
  • Connecting to the server and verifying that everything works correctly
  • An overview of some must-have plugins

Prerequisites

  • A clean Linux VPS running Ubuntu 16.04 and up, Debian, or CentOS 7
  • root user access
  • 1Gb of RAM memory for the Minecraft server itself, to support up to 4 players
  • Add 1Gb of RAM per additional 5 players
  • CentOS uses a lot of memory for itself, so plan on adding even more RAM on that operating system.
  • At least 2 CPU cores (for up to 4 players). More powerful CPUs with more cores will be required in case of higher loads.

Step 1: Installing Java Runtime and Git

Once you are logged in as root, you can create a new user account that you’ll use from now on for running Spigot. The root user is not needed for running a Minecraft server.

We’ll call the new user spigotuser. On both Ubuntu / Debian and CentoOS the first step is the same:

adduser spigotuser

Further instructions differ:

Setting Up Java and Git on Ubuntu / Debian

On Debian and Ubuntu, you need to add spigotuser to the sudo group, so that you can run commands as sudo:

usermod -aG sudo spigotuser

You will be immediately asked to enter a password for the new user, so provide it.

Log in as the new user:

su spigotuser

Run the following commands to refresh apt cache and install Git, Java and wget:

sudo apt update
sudo apt install -y git openjdk-8-jdk wget

Setting Up Java and Git on CentOS

On CentOS, to be able to run commands as sudo, you’ll need to add spigotuser to the wheel group:

usermod -aG wheel spigotuser

In contrast to Ubuntu, CentOS will not ask you to enter a password, so you’ll need to set it up manually with the following command:

passwd spigotuser

Then, log in as the new user:

su spigotuser

Use yum to install Java and wget:

sudo yum install -y java-1.8.0-openjdk-devel wget

You’ll need to install Git separately, because CentOS comes with an outdated version that practically won’t work. Add repository which maintains the latest Git to yum:

sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm

Then, install it:

sudo yum -y install git2u-all

Now you have all the reqired dependencies installed. Next, you’ll build Spigot.

Step 2: Building Spigot

Create a folder to house the binaries which we will soon generate:

mkdir ~/spigot-binaries

Navigate to it:

cd ~/spigot-binaries

Because of legal concerns, Spigot does not distribute binaries directly. Instead, you’ll have to download and run Build Tools, a specialized program that generates the final Spigot server binary.

Download the latest Build Tools version by running:

wget https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar

Before actually running Build Tools, you’ll need to configure Git so that it does not automatically convert line endings:

git config --global --unset core.autocrlf

You can now run Build Tools to get fresh server binaries for the latest available server version:

java -jar BuildTools.jar --rev latest

Instead of latest, which may be unsuitable for production use, you can also pass in a specific Minecraft version. At the time of this writing, the latest stable Spigot version was 1.13.2. If you wanted this specific version, run this command:

java -jar BuildTools.jar --rev 1.13.2

The compilation will take a long time, especially on slower machines, so be patient. You’ll need to have at least 512Mb RAM free for Build Tools to use.

When it finishes compiling, you’ll have a JAR file in the directory, called spigot-rev.jar, where rev is the version of Minecraft. For the command above, it will be spigot-1.13.2.jar.

Step 3: Starting Up the Server

You should separate compilation files from server files, so create a new directory for the server:

mkdir ~/spigot-server

Copy the JAR file to the new directory:

cp spigot-1.13.2.jar ~/spigot-server/spigot.jar

Navigate to the directory:

cd ~/spigot-server

Before running the server for the first time, you’ll need to create a file called eula.txt to signify that you agree with the Mojang Account EULA. Create it using nano:

nano eula.txt

Add the following line:

eula=true

Save and close the file. You’ll now create a script that will be used for starting your Spigot Minecraft server. Create a file called start.sh:

nano start.sh

Add the following lines:

#!/bin/sh

java -Xms1G -Xmx1G -XX:+UseConcMarkSweepGC -jar spigot.jar

The command runs Spigot with 1Gb allocated (as denoted by 1G in the command). You must change this in both places to the amount of RAM you can give Spigot on your server – otherwise, it won’t run.

When you are done, save and close the file.

Make it executable:

chmod +x start.sh

Now, run Spigot server for the first time, which will generate configuration files and folders you can later customize:

./start.sh

Allow it some time to initialize the worlds: the Overworld, the Nether and the End.

When it says Done!, type

stop

into the console to shut the server down. If you now run ‘ls’, you’ll see that Spigot has generated its data structures and written them to the disk:

Internal Configuration

Spigot uses .yml and .json files to store its internal configuration. The folders are used for storing the following:

  • logs – Spigot runtime logs
  • plugins – initially empty. This is the folder where you’ll place compatible plugins
  • world – Minecraft world Overworld
  • world_nether – Minecraft world Nether
  • world_the_end – Minecraft world End

server.properties configures gameplay-related settings. Here is what its default version looks like:

A complete list of parameters can be found here, along with their explanations. These are the most important ones:

  • gamemode – sets the game mode: 0 is survival, 1 is hardcore, and 2 is creative mode
  • spawn-monsters – true by default; controls whether monsters spawn
  • pvp – true by default; enables Player vs Player fights
  • max-players – sets the maximum numbers of players who can play on your server at the same time. Set to 20 by default; you should edit this according to system resources allocated to Spigot
  • server-port – the port of the server. You should leave the default value (25565)
  • spawn-npcs – true by default; controls whether villagers spawn
  • allow-flight – set this to true to allow players to fly
  • level-name – set to world, which is the default world Spigot automatically generates. It is located in a folder with the same name. You can put your custom world in a folder and enter its name here to play on it
  • spawn-animals – true by default; controls whether animals spawn
  • white-list – false by default; set this to true to enforce the whitelist. Allowed UUIDs (usernames) can be set in the file white-list.txt, one per line
  • generate-structures – true by default; controls whether the world generator spawns structures in biomes
  • online-mode – if true, only paid Minecraft accounts can connect to the server
  • motd – the text that will be shown underneath the server name in Minecraft. Supports Unicode characters, but they must first be escaped

You can edit server.properties with the following command:

nano server.properties

When you are done, start the server again:

./start.sh

Keeping the Server Turned On

When you close you current SSH connection, your server will shut down. To prevent this, you can run it in a separate terminal using screen. Create a new file, called start-screen.sh, for editing:

nano start-screen.sh

Add the following line:

screen -S "mc-server" -U -m -d sh start.sh

Save and close the file.

This command starts a screen with the name mc-server in detached mode, which means that it will run in the background on the VPS. When it launches, it runs the command sh start.sh, which in turn starts the server. This way, even after you close your SSH connection, your Minecraft server will always be available for you and your friends to play on.

Step 4: Connecting to Your Server

When the server loads, open Minecraft on your local machine. Click on Multiplayer, then press on Add Server. Enter a name of your choice and for the server address, enter :. You can omit the part if you left the port setting intact (25565).

Press on Done and double click on your server in the list.

Wait a while for it to connect, and then you and your friends will be able to play on your own Minecraft server!

If you get the following error when connecting to your server:

that means that you have tried to connect with online-mode set to true in server.properties. Set it to false and try again.

Step 5: Learning About Spigot Plugins

Right now, your server will offer a vanilla experience. You can install plugins from the Spigot’s website by downloading the JAR files and then uploading them into the plugins directory. The easiest way to upload files to your server is via FTP, but setting that up is out of scope of this tutorial.

The server will load plugins on every start, and report any errors (most likely arising from version incompatibilities) immediately. When you add new plugins, restart your Minecraft server by joining it from the game and issuing the /restart command.

Here are some must-have plugins that will greatly enhance your player’s experience:

  • Vault – a plugin that all others depend on. It greatly simplifies plugin development
  • EssentialsX – this plugin adds over 100 convenience commands that your players will expect to find
  • WorldEdit – an in-game world editor. It allows for massive & easy block manipulation
  • WorldGuard – depends on WorldEdit. It allows you to define in-game access rules, for example to prevent players from griefing your property
  • LuckPerms – every server should have a permissions plugin, and LuckPerms is fast, reliable and maintained. It also offers a web editor, and is very easy to use

Dusko Savic is a technical writer and Flutter developer.

DuskoSavic.com

The post How to Set Up a Minecraft Server on Your VPS appeared first on Low End Box.

Package Management Walkthrough: apt, yum, dnf, pkg

In this tutorial, we will give you an overview of package management utilities on Ubuntu and Debian, Centos, Fedora, and FreeBSD. Once you install, say, Apache and Nginx, PHP, a MySQL database, all these systems start behaving similarly. The main difference is located in the tools for package management and that is how most newcomers to VPS boxes decide which server to run their sites and applications on.

What We Are Going To Cover

  • apt utility for Debian and Ubuntu
  • yum for Centos
  • DNF for Fedora
  • pkg for FreeBSD

and for each of these will show how to

  • find help / read manual,
  • install a package,
  • delete it,
  • find package in a repository,
  • eventually check for vulnerabilitiesand so on.

Prerequisites

If you want to try out the commands, you will need

  • root user access as well as the ability to
  • SSH into the VPS.

Basic Functions of Package Managers

Windows users download their applications from web sites and each app is then responsible for the upgrades and security patches. Linux is different: all applications wait in so-called repositories to be downloaded and installed. Programs that download and install the packages are called package managers and each Linux distribution will have its own repository and the corresponding package manager. For more popular systems, such as Ubuntu, there are repositories for each major version, say, 14.04, 16.04 and so on. The so-called backport repositories may exist as well, when a newer version of the package, say from Ubuntu version 16.04, is ported “back” to the repository for Ubuntu version 14.04.

Unlike Windows users, on Linux you would not even care which version is installed. The package manager would choose the proper version and see to the rest of the process. Some software companies deliver installation files directly, on Debian for example, but there are lower level commands to handle such cases as well.

Package manager can search the repository so that you can see whether the package you want to install is there. It is also possible to test what would change in the system if you installed a package, which may be of vital importance in certain cases.

The same package manager can remove the installed package from system cache, and can also deinstall the app, completely. If there is a newer version in the repository then in the VPS, the package can be upgraded on the run.

APT for Debian / Ubuntu

For Debian-like systems, including Ubuntu, the basic high level package manager is called apt. Its lower level counterpart, for dealing with particular files is called dpkg. File type is .deb.

apt is a relatively new service, and gained prominence with Ubuntu 16.04. The name of the package manager that was used previously is apt-get. To make things even more confusing, the set of tools that Debian uses to manage packages is called APT — which is not the apt that we are talking here about.

apt was introduced to simplify package management for normal users. It has more default values and is easier to use than apt-get. apt-get, however, is not going anywhere and can still be used without problems. In this tutorial, we are going to use apt only.

How To Get Help About apt Package Manager

The command to read documentation about apt is

apt help

You will have the most popular commands at a glance:

How To Upgrade the Server

Package managers read files from the repository into the systems cache. The command is:

sudo apt update

Run that on Debian box. Dozens of lines of output will be produced, ending with

Notice it says there are 3 packages to be upgraded. Run

apt list --upgradable

to see the complete list.

Running the same upgrade command on Ubuntu 19.0 will produce a different outcome:

Under Ubuntu, there will be 65 packages to upgrade.

The entire server version will be updated with:

sudo apt upgrade

The command

sudo apt full-upgrade

will perform full upgrade, which means that the conflicting package dependencies will be upgraded to the newest version, removing older or unused dependencies at the same time.

Update and Upgrade In One Line

Following along recipes for installation of software for your VPS, you will frequently see the following line:

sudo apt update && sudo apt upgrade

which both updates and upgrades the server. That way, you are guaranteed to have the latest version of software for the installation.

Installing Software

You can search for the software to install:

sudo apt search packageName

The result may not be that usable, as it may consist of dozens of lines of available packages. You may want to copy the contents of the terminal window into a text editor and there search for the package name.

To install software, the command is:

sudo apt install packageName

For example, running

sudo apt install perl

on Debian let me install one new module for programmins language perl, while on Ubuntu nothing needed to be installed as

perl is already the newest version (5.28.1-6).

apt cannot install software from a URL, but the lower level package RPM can. Installing from the repositories is always safer, though.

Removing Packages With apt

To remove a package:

sudo apt remove packageName

apt will remove unused dependencies, but in case some still remain, here’s another command:

sudo apt autoremove

How to list all installed packages:

apt list --installed

Yum Package Manager On Centos

yum is the package manager for Centos, a security oriented version of Red Hat Linux. File format is .rpm. yum installs or upgrades any package dependencies, which is the main benefit from using it. yum wil download packages from the official repositories, but you can also set up your own repositories on systems that do not have Internet access.

How To Get Help About yum Package Manager

Run

yum help

The first screen lists all commands:

The second screen shows available parameters for yum:

How To Upgrade the Server With Yum

The update command of Yum both upgrades and updates the system:

sudo yum update

Even if your server installation is just one day old, as is the case here, there will be something new to upgrade and install:

It will ask you for approval, once or twice; answer Y if you want the installation to proceed.

WARNING: For longer transactions, it may appear that the terminal is dead (nothing happens in its window). Be patient and wait it out. Otherwise, when another transaction is set in motion, Centos will ask you to finish the previous transaction first. Or, you can clean up with the command such as:

yum-complete-transaction --cleanup-only

Installing Software With Yum

To search for an installable package:

sudo yum search packageName

Install a package with the following command:

sudo yum install packageName

NOTE: yum will ALWAYS install the latest version of the kernel.

Removing Packages with yum

Remove a package with:

sudo yum remove packageName

yum includes three commands for removing the packages:

  • autoremove will completely erase any traces of the previous configuration.
  • remove will maintain a local copy of any configuration files/directories that were changed from the default values during the installation.
  • erase is the same as remove.

DNF Package Manager on Fedora

While Centos is a free version of Red Hat Linux, Fedora is like a laboratory for research and development for those two systems. As of Fedora version 22, DNF has replaced yum as the official package manager and it is likely that one day the same will happen on Centos. DNF should serve as the improved version of yum so the commands will be similar.

With DNF, maintaining groups of machines is made easier, so you do not need to manually update each one using rpm. It also

  • supports multiple repositories
  • uses depsolving technology for dependency calculations
  • runs faster and takes less memory than yum
  • treats .RPM files consistently
  • is written in Python and runs on both Python 2 and Python 3
  • has its own plugins, which can modify its behavious and introduce new commands.

How To Get Help About DNF Package Manager

The command is:

dnf help

Here is a list of the main commands that it supports:

And this is the list of plugins and a partial list of optional arguments:

How To Upgrade the Server With DNF

To update and upgrade all software:

sudo dnf update

As usual, answer with Y when asked whether you want to proceed with the installation.

There also is a command

dnf check-update

but you do not need to do this as DNF updates its cache automatically before performing transactions.

Installing Packages with DNF

Search for an installable package:

sudo dnf search packageName

To install a package:

sudo dnf install packageName

Removing Packages with DNF

To remove a package:

sudo dnf remove packageName

The autoremove command will search across the system and remove unused dependencies:

sudo dnf autoremove 

This is the warning it sent me:

so be sure to always add the name of the package you want autoremoved:

sudo dbf autoremove packageName

Differences Between apt and BNF Commands

Here is a comparison between apt and DNF commands:

Ubuntu command Fedora command
apt update dnf check-update
apt upgrade dnf upgrade
apt dist-upgrade dnf system-upgrade
apt install dnf install
apt remove dnf remove
apt purge N/A
apt-cache search dnf search

Package Management With PKG On FreeBSD

FreeBSD has two ways of installing software:

  • packages, which is similar to installing .deb packages on Ubuntu / Debian and .rpm on Centos / Fedora, and
  • ports which is making software from source, and is not further explained in this tutorial.

The main difference is that pkg will install only the binary packages.

How To Get Help About pkg Package Manager

The command is

pkg help

How To Upgrade the Server With pkg

freebsd-update fetch install
pkg update && pkg upgrade

Press Y when asked whether to install.

Installing Packages with pkg

Search for a package:

pkg search packagename

Install a package:

pkg install packagename

Here is what installing nginx looks like:

List installed packages:

pkg info

Upgrade from remote repository:

pkg upgrade

Delete an installed package:

pkg delete packageName

Checking Dependencies

Check for missing dependencies:

pkg check -d -a

Remove unneeded dependencies:

pkg autoremove

Automatic And Non-automatic Packages

List non-automatic packages:

pkg query -e '%a =    0' %o

List automatic packages:

pkg query -e '%a =    1' %o

Change a package from automatic to non-automatic, which will prevent autoremove from removing it:

pkg set -A 0 packageName

Change a package from non-automatic to automatic, which will make autoremove it be removed once nothing depends on it:

pkg set -A 1 packageName

Security Advisories

Audit installed packages for security advisories:

pkg audit

And here is the result:

The audit command goes to the vulnerability database for FreeBSD and reads from there. Here is what it’s readable form looks like:

We should now wait for the upgraded version to appear and then use the upgrade command to patch it.

Dusko Savic is a technical writer and programmer.

duskosavic.com

The post Package Management Walkthrough: apt, yum, dnf, pkg appeared first on Low End Box.

How to Edit Files From Your Linux VPS Terminal

There is a constant need to read, edit and change text files on any VPS, mostly after the installation of new packages. Learning, practicing and gaining proficiency with command line text editors can mean a world of difference in your experience and results with VPS.

If your server runs into a trouble, the only resource will be access to system console and the only way to use it will be through a text editor. About the only editor that is guaranteed to exist everywhere is Vi or its descendant, Vim. In this tutorial, we will study nano and Vim, two popular text editors that you can run from a VPS terminal.

Vi Editor

A problem for most newcomers to VPS is that vi, the only text editor that is by default present on any Linux distribution, is neither WYSISWYG nor intuitive. If you expect it to obey commands from Windows Notepad or MacOS TextEdit editors, you’ll be frustrated and disappointed in no time. Let’s say you are following a script to install some widely used program, say Nginx, and that it contains a command such as

sudo vim /etc/nginx/nginx.conf

If this is your first contact with vi, you will discover that

A) you cannot type into the editor (unless you by chance press i on the keyboard) and that

B) you have no idea how to save the file and leave the editor (until you find out that :wq will do the trick).

Vim Editor

In time, an enhancement of vi called vim, became very popular with power users. It is an almost strict superset of vi but with dozens of additional features sucs as protocols, plugins, tasks automation, working with several files at once, using its internal language VimScript or more formal Python, Ruby, Perl, or Tcl as scripting languages, and so on.

Vim is difficult to learn completely, but well worth the effort.

Nano Editor

Problems that newcomers have with Vi / Vim is the reason why another editor, nano, is so popular. It will show the available commands as a menu on screen, and when you start typing, you will see text filling in. No wonder the first thing many users do after installing a “droplet”, an “instance” or a “server” on their VPS boxes, will be to install nano.

What We Are Going To Cover

For nano and Vi / Vim we shall demonstrate how to:

  • Install the editor on Debian / Ubuntu and Centos
  • Explain the philosophical approach to editing
  • Get help
  • Create new or open an existing file
  • Show important commands within the editor
  • Save file and exit

Prerequisites

  • Debian / Ubuntu or Centos systems
  • Ability to SSH into the server
  • Since Vim is a superset of Vi, we are going to concentrate on Vim only

Installing Editors on Ubuntu 16.04 and Debian 9

Vi should be present on all servers, however, on some it may actually envoke Vim. We are showing commands for installation on all systems as there may be differences amongst hosting providers and server versions provided.

First update and upgrade the current version of Ubuntu / Debian:

sudo apt update
sudo apt upgrade

Depending on the version of Debian, the sudo command may not be recognized immediately after the server is installed. If that is the case, just omit sudo from the commands.

On Ubuntu only, if you get the following prompt:

just press Tab on the keyboard and then Enter.

You may then install updates, if any:

sudo apt dist-upgrade 

Then, install Nano:

sudo apt install nano

We see that it is already installed on Ubuntu 16.04:

Let’s now install Vim:

sudo apt install vim

It comes preinstalled as well.

Installing Editors on Centos 7

The commands are:

sudo yum update
sudo yum install nano
sudo yum install vim

How to Start the Editor

To start an editor, you invoke it by its name at the command prompt. If you add a file name, two things can happen. If there is no file at that address, the editor will create an empty file, and if the file exists, it will be opened in the editor.

Nano

When To Use Nano

This is the ideal case for using nano: you are (fairly) new to VPS servers, but you have just acquired one in order to run one specific program on it, or a couple of sites. So you snoop around and find a good recipe for installing your software of choice, you follow along and you reach a point when a config file needs to be changed. Nano is an ideal editor for such small, quick and dirty jobs, where you add a line or two, say, tweak site name for Nginx and so on, then save and close the file.

Now move to a neutral folder such as /tmp and open a file called sometext.txt in it:

cd /tmp
nano sometext.txt

We can start typing new text right away or we can paste a text we already have in clipboard. If it’s a terminal window under Windows (such as Kitty or Putty), you will paste text with the right click of the mouse and if it’s under MacOS, a simple Cmd-V should do.

I have copied the above paragraph and this is what I’ve got:

The text is all in there but you see only its very end. Nano by default does not wrap text as that is the expected behaviour when you edit configuration files. To change to wrapped text, press Esc, release it, and then press $:

That was soft wrapping, hard wraps are turned on or off with Esc + L.

The normal way to issue commands in nano is through Ctrl key plus a letter, which is denoted as ^G – meaning press Ctrl and then pres key g while still holding Ctrl. In the menus, nano uses upper-case G so it seems you have to press the Shift key and then g but actually, you do not have to press Shift at all.

Instead of Esc followed by a key press, you can also use key Alt with a simultaneous key press. So hard wrap would mean pressing Alt, holding it, then pressing Shift, holding it, then pressing l. In general, Alt and Esc are called meta keys and – depending on your keyboard – some other key may be assigned that role.

Help Screen in nano

Ctrl-G will get you main nano help text on screen:

Scroll down with combinations of ^Y and ^V or, if on a normal PC / Mac keyboard, just use PageUp and PageDown keys. You can also scroll with cursor keys, up and down.

We’ll now study keyboard combinations a bit more:

Commands starting with M, for instance, M-(, call for pressing the Meta key, which, as we have seen already, can be Esc, Alt or something else. So you can go to the beginning of the paragraph by pressing Esc, release pressure, then press on Shift and 9 at the same time. Or, you can press Alt, keep the key pressed, press on Shift, hold both keys pressed and then press on 9.

Press on Ctrl-X to leave the help screen.

Saving File With nano

To save file with nano, use Ctrl-O. The menu at the bottom changes to:

To save the file with changes, just press Enter.

Pressing on Ctrl-X will leave nano if the file is saved; if not, it will ask for confirmation with Y.

From the main menu, we see that Ctrl-W will find text, _Ctrl-_ is for the replace operation and so on.

Vim Editor

Learning Vim With Vimtutor

The best way to learn Vim is to start a special version of Vim called vimtutor:

vimtutor

You are in Vim right away, and in its “normal” mode. It means pressing keys on the keyboard will not enter text but will be interpreted as interactive commands. To move cursor press on keys h, j, k, and l. Pressing j, for instance, will move cursor one row below. Keep pressing j until you see the second screen in vimtutor:

and then proceed with learning from there.

Running Vim

Run the following command

vim

and you’ll get this window:

It’s easy to create or open a file with Vim:

vim anothertext.txt

Three Modes of Operation in Vim

Vim has several modes of operations but these three are the most important:

  • NORMAL MODE – cursors, moving through text, text buffers, text manipulation etc.
  • INSERT MODE – inserting text: typing and editing.
  • VISUAL MODE – mode in which larger blocks of text can be defined, cut out, pasted and so on.

Press Esc twice to return to the normal mode.

INSERT MODE In Vim

If you want to type text, press i in the normal mode to ignite the Insert mode. You’ll see the word — INSERT — in the lower left corner of the window and you’ll be able to enter text.

In insert mode, I copied the above paragraph and right clicked on the mouse – the text appeared in Vim editor.

The Command Mode in Vim

You start issuing commands by typing a colon. For example, type :h for help and the following help screen will show up:

Type :q to get back to the screen with text.

To leave the file and exit the editor, enter:

:x[return]

To quit vim without saving the file, enter:

:q![return]

Here are the basic commands:

  • save: :w
  • save and exit: :wq
  • exit: :q
  • force: ! (example :w! :q!)
  • copy: y
  • copy a line: yy
  • paste: p
  • cut: d
  • cut a line: dd

Typing a number in front of command will execute that command that number of times. That means that if w is a command to move cursor to the beginning of the next word, 15w will move cursor 15 words to the right.

VISUAL MODE In Vim

Typing V in normal mode will turn on the Visual mode.

Note the label VISUAL LINE in the lower left corner of terminal window.

In this mode pressing keys such as j and k will move cursor up and down but will also mark a white block of text. Since we have pressed V, entire lines with cursor will be marked. Press v to enable character- based visual selection of text, in which case, the lower left corner will show only the word VISUAL.

Now, to delete text, press d (delete) and press y (yank) to copy the marked region. Then move the cursor to the desired paste location; pressing p will then paste after the cursor, while P will paste before it.

Press c to change text… and for further commands be sure to devote more time to learning Vim than you expected!

What Can You Do Next

We have shown basic editing capabilities of two almost universally present text editors, nano and Vi or Vim. They may not be your cup of tea at all, but you should at least learn enough of Vi / Vim to read a file, edit and then save it. Other famous editors we have no space to do justice here are Emacs, Micro, NE, and there are many others to choose from for your VPS!

Dusko Savic is a technical writer and programmer.

duskosavic.com

The post How to Edit Files From Your Linux VPS Terminal appeared first on Low End Box.

How To Set Up Cron On Your VPS

In this tutorial, we will see how to set up repetitive tasks on your VPS. You may want to automate system maintenance or administration so that you

  • download email every day,
  • download weekly new songs from the Internet,
  • erase files that you do not need any more,
  • daily backup your databases or data,
  • update your system with the latest security patches,
  • check your disk space usage,
  • send emails

and so on. The command that will do all that for you is cron and the tasks are called cron jobs. Many popular software packages, will either ask you to fill in cron details or, like Drupal and Magento, will install cron jobs on their own.

What We Are Going To Cover

  • Definition of crontab
  • How to add cron command to a crontab file
  • Saving the output of cron jobs
  • Structure of crontab commands
  • Start, stop, restart cron on Centos & Ubuntu / Debian
  • Editing crontab file in Ubuntu, Debian, and Centos
  • Crontab restrictions
  • General format of cron commands
  • Crontab variables
  • Examples of cron jobs

Prerequisites

  • You will need ability to SSH into your VPS as a root.
  • You should also have one non-root user with sudo capabilities, at your disposal.
  • We use nano as our text editor. It will come preinstalled on Ubuntu and Debian. On Centos, install it with this command:
yum install nano

Press y twice to finish the installation.

What Is crontab

crontab, which is short for cron table, is a configuration file that defines shell commands to run periodically on a given schedule.

Cron may be system-wide or on a single user basis so there will be two kinds of crontabs, one for root user and the others for individual users. System-wide crontab will contain column for a particular user name, while crontabs for individual users will not.

On all three systems, the system-wide crontab is at /etc/crontab. You can edit cron jobs directly from this file but you actually shouldn’t. True automation will come only if each user has its own set of crontab files.

On CentOS, crontab files are stored in the /var/spool/cron directory while on Debian and Ubuntu, crontab files are stored in the /var/spool/cron/crontabs directory.

Crontab File Locations in Centos

Here are the locations for cron jobs:

/etc/crontab
/etc/cron.d/
/etc/cron.daily/
/etc/cron.hourly/
/etc/cron.monthly/
/etc/cron.weekly/
/var/spool/cron/

/var/spool/cron/ contains a crontab file for each user who is using crontab.

Log files for cron runs can be found at the /var/log/cron.

The Contents of a System-wide crontab File on Ubuntu

Use this command to quickly see the contents of the crontab file:

cat /etc/crontab

Lines starting with # are comments.

Let’s analyse the contents of crontab file.

Crontab Variables

Lines such as

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

contain cron variables.

SHELL – it is possible to choose the shell that cron will run from. The default is /bin/sh.

PATH command, to denote where the system should look up when executing cron commands (see below). If PATH is blank, the entire path to the command must be explictly stated in the command itself.

HOME Normally, cron would execute the command from the user’s home directory. You can change the HOME variable to point to another directory.

MAILTO For each event, cron sends email notifications to the owner of the crontab. Specify a comma separated list of all the email addresses you want the notifications to be sent to. Put MAILTO=”” to deliver no mail at all.

Here is an example of a cron job with all these variables:

HOME=/opt
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/zsh
MAILTO=email@example.com

*/1 * * * * command

This command would execute every minute.

Cron Commands Explained

Columns in cron commands have the following meaning:

  • m – minutes during the hour, from 0 to 59. Number 17 in this column means crontab will perform an action on every 17th minute of the hour.
  • h – hours during the day, from 0 to 23. Number 6 here would mean some action would be performed every six hour during the day.
  • dom – date of month, from 0 to 31.
  • mon – month in year, from 0 to 11. Instead of numbers we can use jan, feb, mar, apr etc.
  • dow – day of week, from 0 to 6, where 0 is Sunday and 6 is Saturday. On some systems, 7 can mean Sunday as well.

It is also possible to use

Operator Values In Columns

* Asterisk means any value or always. An asterisk symbol in the Minutes field will perform the each minute.

, Comma specifies a list of values for repetition. For example, if you have 1,13,15 in the Hour field, the task will be run at 1 am, 1 pm and 3 pm.

Hyphen specifies a range of values. If you have 1-5 in the Month field, the task will run every month from January to May.

/ Slash specifies values that will be repeated over a certain interval between them. For example, if you have */4 in the Minutes field, the action will be performed every four minutes. The same effect would be to have had specified values of 0,4,8,12,16,20. You can also put a range of values before the slash. For example, 1-30/10 is the same as 1,11,21.

Crontab Restrictions

Imagine you are the system administrator and one of your users has started sending email marketing campaigns at the rate of 20,000 messages per hour. That may lead to poor experience for other users of your VPS, or you may want that user to just start paying more for the convenience. Or, you can restrict his crontabs on a user per user basis. That is where files /etc/cron.deny and /etc/cron.allow come into play. They contain a list of user names, one user name per row.

The /etc/cron.deny file exists by default and in the beginning it is empty. That means that all users of the system can use cron jobs. User with names listed in that file will not be able to use cron at all.

The counterpart file is /etc/cron.allow – only the users with names in it will be allowed to use cron.

If neither file exists, and depending on other system parameters, either only the super user will be able to use cron jobs, or all users would be able to use cron jobs.

Using crontab Command on Ubuntu

Here is a handy command:

crontab -e

It asks to decide upon an editor and we choose nano.

Once in it, we see the crontab file contents:

Editing crontab File in Debian

The command is the same:

crontab -e

Choose nano amongst eight different editors that Debian offers:

You will end up in the same content as for Ubuntu, so we won’t repeat the image.

Editing crontab File in Centos

Use nano to edit the crontab file directly:

sudo nano /etc/crontab

and the contents of the crontab file are different:

Each time you change a crontab, you will need to restart the cron utility. Here are the relevant commands.

Start, Stop, and Restart cron on Ubuntu / Debian

sudo service cron status
sudo service cron start
sudo service cron stop
sudo service cron restart

Start, Stop, and Restart cron On Centos

service crond status
service crond stop
service crond start
service crond restart

Structure Of crontab Commands

Here are the parameters of the crontab command:

crontab -e Edit crontab file, or create one if it doesn’t already exist.
crontab -l Display crontab file contents.
crontab -r Remove current crontab file.
crontab -i Remove current crontab file with a prompt before removal.
crontab -u Edit other user’s crontab file. Must be run with system administrator privileges.

Examples of cron Commands

Schedule a backup script to run every day at 5:30 AM:

30 5 * * * /path/to/script/backup-script.sh

To schedule the backup on the first day of each month at 8 PM:

0 18 1 * * /path/to/script/backup-script.sh

It is possible to use several macros to start events in the beginning of hour, day, week, and month.

@hourly path/to/script/script.sh
@daily path/to/script/script.sh
@weekly path/to/script/script.sh
@monthly path/to/script/script.sh
@reboot path/to/script/script.sh

The last line, with @reboot, will execute after server reboot.

Saving the Output of cron Jobs

In general, the script we execute as cron jobs will generate some output. We can save it log files, like this:

0 3,11,16 * * tue,sat path/to/script/script.sh > /path/to/logs/backup.log 2>&1

That command will execute on Tuesdays and Saturdays, at 3 AM, 11 AM and 16 PM and will save the output to a file called backup.log at the address /path/to/logs.

If we do not want to record any output, the command can look like this:

0 3,11,16 * * tue,sat path/to/script/script.sh > /dev/null 2>&1

How To Add cron Command To a crontab File

We simply open the crontab file with nano, and enter one or more of the above commands in the end. Here is crontab file found in the fresh server install in Centos:

sudo nano /etc/cron.d/0hourly

Now enter the line shown above and you’ll get:

Save and close the file and that restart crond in this case. Of course, in real life you would put your own script address instead of path/to/script/script.sh.

What Can You Do Next

Take a long and hard look at the way you are using your VPS and then surf around. You will find many repetitive tasks that you would like to automate so start using cron and crontab files as much as you can!

Dusko Savic is a technical writer and programmer.

duskosavic.com

The post How To Set Up Cron On Your VPS appeared first on Low End Box.