Saturday, October 16, 2021

How to mine Monero on Ubuntu Linux

Please refer to the recent post about setting up a Monero pool miner on Microsoft Windows. That post provides some background information along with how to use wallet software.

Now let's focus on running a miner on Linux Ubuntu 20.04. We will use the same public key address as the previous post. We will simply change the identifier in the start file to allow both the Windows and Linux miners to be monitored on the same page at 

Log on to the Linux machine. Create a new unix group and user. Download and install the software. Start the miner and monitor it at the mining pool website. Setup the miner to start and stop, and tune the software to more fully use the fourth CPU core.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal

$ sudo addgroup cryp
Adding group `cryp' (GID 1004) ...

$ grep cryp /etc/group

$ sudo adduser monero --ingroup cryp
Adding user `monero' ...
Adding new user `monero' (1005) with group `cryp' ...
Creating home directory `/home/monero' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for monero
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y

# If you want the new monero Linux user to be able to log on to the machine remotely, add user monero to the "AllowUsers" line in /etc/ssh/sshd_config.
$ sudo vi /etc/ssh/sshd_config

# Now edit the config file and add the monero username.
$ grep monero /etc/ssh/sshd_config
AllowUsers install1 minec monero

# Restart ssh to apply the new configuration.
$ sudo systemctl restart ssh

# ssh into the machine as the monero user.
$ whoami

$ groups

$ id
uid=1005(monero) gid=1004(cryp) groups=1004(cryp)

# Earlier we ran "lsb_release -a" it showed the name "focal" for the Ubuntu release. Get the monero software for "focal" from

$ wget 

$ gzip -d xmrig-6.15.2-focal-x64.tar.gz

$ tar -xvf xmrig-6.15.2-focal-x64.tar

$ ls -l
total 8256
drwxr-x--- 2 monero cryp    4096 Oct  5 10:38 xmrig-6.15.2
-rw-r----- 1 monero cryp 8448000 Oct  5 11:30 xmrig-6.15.2-focal-x64.tar

# Go into the software directory and create a new (or replace the example) config.json.
$ cd xmrig-6.15.2
$ ls -l
total 8256
-rw-r----- 1 monero cryp    2351 Oct  5 10:38 config.json
-rw-r----- 1 monero cryp     150 Oct  5 10:38 SHA256SUMS
-rwxr-x--- 1 monero cryp 8441704 Oct  5 10:38 xmrig

# Put the following into file config.json. Note the "user" public key is on one line.
$ cat config.json
"autosave": true,
"cpu": true,
"opencl": false,
"cuda": false,
"pools": [
                "url": "",
                "user": "44YkNzrdJnmAqwoTRMPV2e4fihv3AfZ5vMmrtaWCGQaxKuXp5eiZeRREzhPi9tVqGuA3VBRriNwKa2QxmZmRGmHVTkUSTMc",
                "pass": "runDemoB",
                "keepalive": true,
                "tls": true

# Test it from the command line. Stop the program with ctrl-c after you verify it is working.
$ ./xmrig -c ./config.json

# Create and test a command file which will be used by the Linux process monitor so the miner will be started upon machine reboot.
$ cd ~
$ pwd

# Put these three lines into the command file named Note the last command is all one line.
$ vi
umask 026
/usr/bin/nice -n 15 /home/monero/xmrig-6.15.2/xmrig -c /home/monero/xmrig-6.15.2/config.json

# Make the file executable.
$ chmod +x

# Test the command file from the command line.
$ ./

# In another terminal window, run "top" and look at the "NI" column. It should be the "nice" value in the command file. This sets the process priority lower, so the machine can more easily prioritize other work (if there is other work).

# Configure the system services so the mining process will automatically start with the machine.
# For the following commands with sudo, log in with a unix account which is in the sudoers file.
$ cd /etc/systemd/system
$ sudo vi monero.service
Description=monero miner

# Enable the service.
$ sudo systemctl enable monero.service
Created symlink /etc/systemd/system/ → /etc/systemd/system/monero.service.

$ sudo systemctl start monero.service
$ sudo systemctl status monero.service

# Run top or htop to confirm the miner is running with the specified "nice" level.
# Reboot the machine to test the miner will restart automatically.
$ sudo shutdown -r

# After machine restarts, login and run the following to verify the service is running and that it is only running one.
$ top
$ htop
$ ps -fu monero

Let's look into the mining process a little closer, and focus on the number of tasks or processes that are run. From a defensive system administration standpoint, we may want to configure the service so it can not errantly consume thousands of processes.

With the miner running as a service, it is currently using just a handful of processes. This seems to be correct, as one process per CPU core (in this case 4 cores) is reasonable.

$ ps -fu monero
UID          PID    PPID  C STIME TTY          TIME CMD
monero      1046       1  0 15:02 ?        00:00:00 /lib/systemd/systemd --user
monero      1047    1046  0 15:02 ?        00:00:00 (sd-pam)
monero      1145    1043  0 15:02 ?        00:00:00 sshd: monero@pts/0
monero      1146    1145  0 15:02 pts/0    00:00:00 -bash
monero      1471       1  0 15:10 ?        00:00:00 /bin/sh /home/monero/
monero      1472    1471 99 15:10 ?        00:05:38 /home/monero/xmrig-6.15.2/xmrig -c /home/monero/xmrig-6.15.2/c
monero      1517    1146  0 15:12 pts/0    00:00:00 ps -fu monero

$ ps -fu monero | wc -l

# In another terminal window with a Linux user which has access to sudo.
$ sudo systemctl status monero.service
● monero.service - monero miner
     Loaded: loaded (/etc/systemd/system/monero.service; enabled; vendor preset: enabled)
     Active: activating (start) since Sun 2021-10-10 15:12:24 MDT; 1min 57s ago
Cntrl PID: 1531 (
      Tasks: 10 (limit: 18964)
     Memory: 2.2G
     CGroup: /system.slice/monero.service
             ├─1531 /bin/sh /home/monero/
             └─1532 /home/monero/xmrig-6.15.2/xmrig -c /home/monero/xmrig-6.15.2/config.json
Oct 10 15:12:38 d990[1532]: [2021-10-10 15:12:38.338]  net      new job from pool.>
Oct 10 15:12:38 d990[1532]: [2021-10-10 15:12:38.338]  cpu      use argon2 impleme>
Oct 10 15:12:38 d990[1532]: [2021-10-10 15:12:38.341]  msr      msr kernel module >
Oct 10 15:12:38 d990[1532]: [2021-10-10 15:12:38.341]  msr      FAILED TO APPLY MS>
Oct 10 15:12:38 d990[1532]: [2021-10-10 15:12:38.342]  randomx  init dataset algo >
Oct 10 15:12:38 d990[1532]: [2021-10-10 15:12:38.342]  randomx  allocated 2336 MB >
Oct 10 15:12:45 d990[1532]: [2021-10-10 15:12:45.958]  randomx  dataset ready (761>
Oct 10 15:12:45 d990[1532]: [2021-10-10 15:12:45.958]  cpu      use profile  rx  (>
Oct 10 15:12:45 d990[1532]: [2021-10-10 15:12:45.960]  cpu      READY threads 3/3 >
Oct 10 15:13:46 d990[1532]: [2021-10-10 15:13:46.126]  miner    speed 10s/60s/15m >

$ systemctl show --property DefaultTasksMax

# Note the service is using 10 tasks of the 18964 limit. It appears it uses the default maximum setting.
# Note the service is using 2.2 GB of memory.
# Since the service is running correctly and the machine has four CPU cores, let's ensure the service does not run more than 99 processes.

# Add two lines to the end of /etc/systemd/system/monero.service.

# Reload the systemd configuration.
$ sudo systemctl daemon-reload

# Restart the mining process.
$ sudo systemctl restart monero.service

# Look at the Tasks limit for this mining process. It should use 999 from the monero.service file.
$ systemctl status monero.service | grep Tasks
      Tasks: 10 (limit: 999)

# Restart the machine to test the configuration.
$ sudo shutdown -r

Go to the website to monitor the mining activity. Enter the address from the config file at this website to see your miner activity.

Using top and htop, you may notice with four CPU cores that it seems only three are being used. The load average will be in the 3s rather than 4 (the number of cores). For this example on this four-core CPU, you may edit config.json and add the fourth core to the "rx" line, like this: "rx": [0, 1, 2, 3],

# Then restart the service.
$ sudo systemctl restart monero.service

# If you make this change to use more CPU, closely monitor if more hashes are being calculated. You may find that while more CPU is used, less hashes are performed due to CPU cache issues. Please read this tuning document and consider de-tuning to the prior setup. In short, hyper-threading (more logical processors than cores) usually does not scale CPU-bound processes.

To tune the OS settings, look into huge pages. First, get the current settings and monitor the current miner hash rate so you know the baseline before making tuning changes.

Without tuning, the OS may be configured like this:

$ grep -i huge /proc/meminfo
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
FileHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB

Stop the miner process, make temporary changes to OS kernel settings, and restart the miner to determine if the hash rate changes.

$ sudo sysctl -w vm.nr_hugepages=1280
vm.nr_hugepages = 1280
$ grep -i huge /proc/meminfo
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
FileHugePages:         0 kB
HugePages_Total:    1280
HugePages_Free:     1280
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:         2621440 kB

If the restarted miner process shows an increase in hashing, then make the kernel OS settings permanent.

$ sudo bash -c "echo vm.nr_hugepages=1280 >> /etc/sysctl.conf"

Reboot the machine and check the huge pages setting and the miner hash rate. For this example, the per second hash rate went from 965 to 1381 just by tuning huge pages.

No comments:

Post a Comment