Setting up a USB Bitcoin miner using cgminer on Ubuntu

By: (plus.google.com) +David Herron; Date: November 4, 2017

Tags: Bitcoin Mining

Looking at the ever-increasing Bitcoin price and wondering how to get in on the action? One way to dip a toe in the water is to buy a cheap older-model Bitcoin miner, such as the Bitmain Antminer U3. These run around $100 on eBay currently and are a proper ASIC-based Bitcoin miner with very low power consumption. It connects to your computer via USB, and requires software like cgminer to connect to a mining pool. Other USB based Bitcoin miners exist, and while the following instructions are for the Antminer U3, the process should be similar for any other USB Bitcoin miner.

You may already know this - but a little basic preview. Bitcoins are "mined" by a distributed computation process. Chunks of data is distributed over the Internet to mining hardware. The mining hardware runs the computations to prove a new block on the Bitcoin Blockchain. The hardware that successfully completes this computational task wins that block of Bitcoin, and then the Bitcoin network silently moves on to computing the next coin.

Since it's completely impractical for an individual bitcoin mining hardware to compete in the current mining landscape it's necessary to connect your hardware to a mining pool. Several mining pools exist, and in this case we'll use the Slushpool.

For a list of pools see: (en.bitcoin.it) https://en.bitcoin.it/wiki/Comparison_of_mining_pools

To sign up with Slushpool, go to (slushpool.com) https://slushpool.com/ and go through the signup process.

Their help center has a beginners guide that's useful. One thing to note is their warning that ASIC Miners are the only way to go in the current Bitcoin mining landscape. That's because the computational difficulty has grown to where a CPU is simply unable to do the TeraHashes/Second that is now typical.

Configuration parameters

The mining pool you choose may have different policies than this. What you'll need is three items of configuration to connect your mining hardware to the mining pool.

  • URL that's to be used to connect with the mining pool -- such as stratum+tcp://stratum.slushpool.com:3333
  • The username for your account with the mining pool -- such as reikiman
  • The worker name for the particular piece of hardware -- such as worker1
  • THe password to use in the connection

I lied, that's four pieces of configuration. Slushpool doesn't care what password you use. If you like, feel free to connect to Slushpool using my username. There's no password, and your hardware will connect and start computing hashes. But you will not get any economic benefit, I will. Please, feel free to connect your hardware to my account. I won't stop you.

The next configuration is to set up a payment address. This is a Bitcoin address for your wallet. That address is provided by your wallet provider.

The address might look like 1MKQ2HaCU6SPT4aYqEv8tKeYpXdgjjanzG -- Feel free to use that address if you like, since that's my wallet.

For now just record these parameters somewhere. For the wallet address, go to the Settings area of the mining pool and enter the address in the appropriate place.

cgminer

If you want to install cgminer from source, go here: (github.com) https://github.com/ckolivas/cgminer

On x86 Ubuntu you can install it from the package manager

$ apt-cache search cgminer
cgminer - multi-threaded multi-pool Bitcoin miner
$ sudo apt-get install cgminer

THe cgminer version available that way is modern enough to support the Antminer U3 out of the box. It may be that your Bitcoin miner isn't supported with the pre-built binary for Ubuntu, and you'll instead have to compile from source. If so, go through the instructions in the Github repository.

I tried setting this up using ARM-based Linux Single Board Computers (e.g. Raspberry Pi) but did not have success. With the Debian version on the the hardware I tried (the C.H.I.P. and the Orange Pi Zero2) the cgminer version did not support the Antminer U3. I tried compiling from source with no luck.

The problem with using cgminer is that it's a CURSES based application and it doesn't fit well as a background process. But we want it to run 24 hours a day, dictating that it be a background process.

cgminer in the background using screen

Fortunately there's a screwball way to proceed that's kind of ingenuous. The screen application is normally meant to interactively to maintain multiple terminal sessions in one terminal window.

Let's start with this command:

screen -LdmS cgminer /usr/bin/cgminer --config /home/david/cgminer.conf

The options are:

  • -L turns on logging of output in each window
  • -d -m Starts the screen process in detached mode, creating a new session without attaching to it. This is great for system-level startup scripts like what we'll do in a minute.
  • -S cgminer sets the session name

The rest is a command-line to run, namely the cgminer command pointing to a configuration file.

$ cat cgminer.conf
{
"pools" : [
        {
                 "url" : "stratum+tcp://stratum.slushpool.com:3333",
                "user" : "reikiman.worker1",
                "pass" : "anything-at-all"
        }
],
       "api-listen" : true,
       "api-port" : "4028",
       "expiry" : "120",
       "failover-only" : true,
       "log" : "5",
       "no-pool-disable" : true,
       "queue" : "2",
       "scan-time" : "60",
       "worktime" : true,
       "shares" : "0",
       "kernel-path" : "/usr/local/bin",
       "api-allow" : "0/0",
       "icarus-options" : "115200:1:1",
       "icarus-timing" : "3.0=100"
}

Here's where you configure the pool configuration. Again, feel free to connect your hardware to my account, I won't mind ;-)

If running this manually from the command-line, use sudo as so:

sudo screen -LdmS cgminer /usr/bin/cgminer --config /home/david/cgminer.conf

Once you've run this, you can connect to the screen session as so:

screen -d -r cgminer

The display may look like what's in the image at the top of this posting. If your configuration is correct, cgminer will connect to the mining pool and start retrieving blocks. The other parts of the display will start showing a hashing rate, and information about each block will be printed as the blocks are processed.

The hash rate is shown here:

(5s):18.29G (1m):14.36G (5m):14.32G (15m):14.31G (avg):16.14Gh/s

And your status is shown on the next couple of lines.

Once you're done browsing the cgminer output, symply type Control-A d to exit the screen session. You can re-run screen -d -r cgminer to hop back into the session to see that you can attach and detach whenever you like.

Making a background service

The above is fine and will keep the cgminer process running -- until the system reboots. This does not automatically restart the cgminer process on rebooting the system. To do so, we need to write a script that manages a background service.

While we could make an LSB-compliant init script in /etc/init.d let's instead create a service.

Create this file: /lib/systemd/system/cgminer.service

[Unit]
Description=cgminer
After=network.target
After=graphical.target

[Service]
Type=forking
# Type=simple
User=david

#Start:
# screen -dm creates detached session that forks.
#  use this with Type=forking
ExecStart=/usr/bin/screen -LdmS cgminer /usr/bin/cgminer --config /home/david/cgminer.conf
#
# screen -Dm creates detached session that doesn't fork.
#  use this with Type=simple
# ExecStart=/usr/bin/screen -LDmS cgminer /usr/bin/cgminer --config /home/david/cgminer.conf

#Stop:
# tell cgminer to quit (not screen), and screen will exit
ExecStop=/usr/bin/screen -S cgminer -X stuff 'q'

# or tell screen to quit and clobber cgminer - not best choice
#ExecStop=/usr/bin/screen -S cgminer -X quit
#Probably need a kill definition in case the miner is hung

#Reload:
# sending the string 'scy' (settings, restart, yes) to cgminer will
#  cause a restart which will re-read config file if using one
ExecReload=/usr/bin/screen -S cgminer -X stuff 'scy'

[Install]
WantedBy=multi-user.target

This has two options - Forking and Non-Forking. You'll see that Forking mode is done with the -LdmS option string, while Non-Forking is done with -LDmS. The service description has commands to Start, Stop and Reload the service, and the commands in each case do the correct thing.

Next you initialize and start the service.

sudo systemctl daemon-reload
sudo systemctl enable cgminer.service
sudo systemctl start cgminer.service

And finally, this lets you verify the service is running:

sudo systemctl status cgminer.service

This command can be useful for examining the startup of the process in case it doesn't start as expected.

sudo journalctl -xe

The next thing to try is connecting to the cgminer session:

screen -d -r cgminer

This is exactly the same as before, but the cgminer session is started automatically when the system reboots. If you want to test that, restart the system.

You may need to manually stop and restart the background service. For example, to debug the configuration and ensure it's starting correctly.

sudo systemctl stop cgminer.service
sudo systemctl start cgminer.service
« Review of the Orange Pi Zero Plus2 - incredibly tiny but uncertain of its usability Social media networks don't spread propaganda, people spread propaganda (through social media networks) »
2016 Election Acer C720 Ad block AkashaCMS Amazon Amazon Kindle Amiga Android Anti-Fascism AntiVirus Software Apple Apple Hardware History Apple iPhone Apple iPhone Hardware April 1st Arduino ARM Compilation Astronomy Asynchronous Programming Authoritarianism Automated Social Posting Ayo.JS Bells Law Big Brother Big Finish Bitcoin Mining Black Holes Blade Runner Blockchain Blogger Blogging Books Botnet Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Chrome Apps Chromebook Chromebooks Chromebox ChromeOS CIA CitiCards Citizen Journalism Civil Liberties Clinton Cluster Computing Command Line Tools Comment Systems Computer Hardware Computer Repair Computers Cross Compilation Crouton Cryptocurrency Curiosity Rover Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Developers Editors Digital Photography Diskless Booting Disqus DIY DIY Repair DNP3 Do it yourself Docker Docker MAMP Docker Swarm Doctor Who Doctor Who Paradox Drobo Drupal Drupal Themes DVD E-Books E-Readers Early Computers Election Hacks Electric Bicycles Electric Vehicles Electron Emdebian Encabulators Energy Efficiency Enterprise Node EPUB ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Facebook Fake News Fedora VirtualBox File transfer without iTunes FireFly Fraud Freedom of Speech Gallifrey git Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Heat Loss Hibernate Hoax Science Home Automation HTTPS Human ID I2C Protocol Image Analysis Image Conversion Image Processing ImageMagick In-memory Computing InfluxDB Infrared Thermometers Insulation Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man Iternet of Things iTunes Java JavaScript JavaScript Injection JDBC John Simms Journalism Joyent Kaspersky Labs Kindle Kindle Marketplace Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac OS Mac OS X Machine Readable ID macOS MacOS X setup Make Money Online MariaDB Mars Matt Lucas MEADS Anti-Missile Mercurial Michele Gomez Micro Apartments Military Hardware Minification Minimized CSS Minimized HTML Minimized JavaScript Missy Mobile Applications MODBUS Mondas MongoDB Mongoose Monty Python MQTT Music Player Music Streaming MySQL NanoPi Nardole NASA Net Neutrality Node Web Development Node.js Node.js Database Node.js Testing Node.JS Web Development Node.x North Korea npm NY Times Online advertising Online Community Online Fraud Online Journalism Online Video Open Media Vault Open Source Governance Open Source Licenses Open Source Software OpenAPI OpenVPN Paywalls Personal Flight Peter Capaldi Photography PHP Plex Plex Media Server Political Protest Postal Service Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Refurbished Computers Remote Desktop Republicans Retro-Technology Reviews Right to Repair River Song Robotics Rocket Ships RSS News Readers rsync Russia Russia Troll Factory Russian Hacking SCADA Scheme Science Fiction Search Engine Ranking Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Shell Scripts Silence Simsimi Skype Social Media Social Media Warfare Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spear Phishing Spring Spring Boot SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Trump Campaign Ubuntu UDOO Virtual Private Networks VirtualBox VLC VNC VOIP Web Applications Web Developer Resources Web Development Web Development Tools Web Marketing Website Advertising Weeping Angels WhatsApp Window Insulation Wordpress YouTube YouTube Monetization