Thursday, 18 April 2019

Linux Hard Disk Format Command







source


Q. I’ve installed a new 250GB SATA hard disk on our office CentOS Linux server. How do I format a hard disk under Linux operating system from a shell prompt?

A.. There are total 4 steps involved for hard disk upgrade and installation procedure:
  đŸ˜±
 Step #1 : Partition the new disk using fdisk command
Following command will list all detected hard disks:
# fdisk -l | grep '^Disk'
Output:
Disk /dev/sda: 251.0 GB, 251000193024 bytes
Disk /dev/sdb: 251.0 GB, 251000193024 bytes
A device name refers to the entire hard disk. For more information see Linux partition naming convention and IDE drive mappings.
To partition the disk – /dev/sdb, enter:
# fdisk /dev/sdb
The basic fdisk commands you need are:
  • m – print help
  • p – print the partition table
  • n – create a new partition
  • d – delete a partition
  • q – quit without saving changes
  • – write the new partition table and exit
Then press n, choose default primary and  then press  w to write to disk

Step#2 : Format the new disk using mkfs.ext3 command

To format Linux partitions using ext2fs on the new disk:
# mkfs.ext4 /dev/sdb1

Step#3 : Mount the new disk using mount command

First create a mount point /disk1 and use mount command to mount /dev/sdb1, enter:
# mkdir /disk1
# mount /dev/sdb1 /disk1
# df -H

Step#4 : Update /etc/fstab file

Open /etc/fstab file, enter:
# vi /etc/fstab
Append as follows:
/dev/sdb1               /disk1           ext3    defaults        1 2
Save and close the file.

Task: Label the partition

You can label the partition using e2label. For example, if you want to label the new partition /backup, enter
# e2label /dev/sdb1 /backup
You can use label name insted of partition name to mount disk using /etc/fstab:
LABEL=/backup /disk1 ext3 defaults 1 2

Note: for disks > 2TB the above method does not work, so  follow this:


parted with gpt label

Create a partition using GPT format. I've chosen to use just 1 large partition that uses the whole disk.
root@turtle:~# parted /dev/sdb
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: /dev/sdb: unrecognised disk label
(parted) mklabel gpt
(parted) mkpart
Partition name?  []?
File system type?  [ext2]? ext3
Start? 0%
End? 100%
(parted) print
Model: ATA ST3000DM001-1CH1 (scsi)
Disk /dev/sdb: 3001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  3001GB  3001GB

(parted) quit
Information: You may need to update /etc/fstab.

Saturday, 13 April 2019

How to run gunicorn script using systemd in Debian

Since upstart is deprecated, it is not available in Debian 9, so I had to use systemd which turned out to be quite easy:

nano  /etc/systemd/system/gunicorn.service

Create the file:

[Unit]
Description=Gunicorn Daemon
#After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
User= appuser
Group=www-data
ExecStart=/path/to/project/gunicorn.sh
#Restart=always
#RestartSec=1
#Restart=on-failure
# Configures the time to wait before service is stopped forcefully.
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target
You don't need to restart the server to enable the service:

systemctl enable gunicorn
Start it:
systemctl start gunicorn

Watch it:
systemctl status gunicorn
If you made changes to gunicorn.service you need to run:

systemctl daemon-reload

before restarting the daemon with

systemctl restart gunicorn

Sunday, 6 January 2019

How to compress scanned PDF?

One problem with scanned pdfs is that the size is so bloated. One good solution to compress such files is this one script:

#!/bin/sh

gs  -q -dNOPAUSE -dBATCH -dSAFER \
    -sDEVICE=pdfwrite \
    -dCompatibilityLevel=1.3 \
    -dPDFSETTINGS=/screen \
    -dEmbedAllFonts=true \
    -dSubsetFonts=true \
    -dColorImageDownsampleType=/Bicubic \
    -dColorImageResolution=120 \
    -dGrayImageDownsampleType=/Bicubic \
    -dGrayImageResolution=72 \
    -dMonoImageDownsampleType=/Bicubic \
    -dMonoImageResolution=120 \
    -sOutputFile=out.pdf \
     $1


Here the compression rate can be changed by tweaking resolution values. I found the above gives a good compression without sacrificing the text quality.


Friday, 2 November 2018

How to cut mp4 using ffmpeg

Tested on Ubuntu 16.04

-ss is the starting time
-t is the duration

ffmpeg -i input.mp4 -ss 00:12:20 -t 00:2:00 -async 1 -strict -2  cut.mp4

Friday, 13 July 2018

How to Install Go 1.10 on Ubuntu16.04


Go is an open source programming language developed by a team at Google. It provides easy to build simple, reliable, and efficient software. This language is designed for writing servers, that’s why it is using widely these days. Go has released latest version 1.10. This tutorial will help you to install Go 1.10 on your Ubuntu 18.04 LTS, 16.04 LTS, and 14.04 LTS systems.

Step 1 – Install Go Language

Login to your Ubuntu system using ssh and upgrade to apply latest security updates there.
sudo apt-get update
sudo apt-get -y upgrade
Now download the Go language binary archive file using following link. To find and download latest version available or 32 bit version go to official download page.
wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
Now extract the downloaded archive and install it to the desired location on the system. For this tutorial, I am installing it under /usr/local directory. You can also put this under the home directory (for shared hosting) or other location.
sudo tar -xvf go1.10.3.linux-amd64.tar.gz
sudo mv go /usr/local

Step 2 – Setup Go Environment

Now you need to setup Go language environment variables for your project. Commonly you need to set 3 environment variables as GOROOTGOPATH and PATH.
GOROOT is the location where Go package is installed on your system.
export GOROOT=/usr/local/go
GOPATH is the location of your work directory. For example my project directory is ~/Projects/Proj1 .
export GOPATH=$HOME/Projects/Proj1
Now set the PATH variable to access go binary system wide.
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
All above environment will be set for your current session only. To make it permanent add above commands in ~/.profile file.

Step 3 – Verify Installation

At this step, you have successfully installed and configured go language on your system. First, use the following command to check Go version.
go version

go version go1.10.3 linux/amd64
Now also verify all configured environment variables using following command.
go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/Projects/Proj1"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
...
...

Wednesday, 27 June 2018

Deploy celery on Debian 8 using upstart

Make an upstart conf file:

nano /etc/init/celery.conf

Insert in it:

description "Celery application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid john
setgid www-data
chdir /path/to/django/proj
exec bash celery.sh
Put `celery.sh` in /path/to/django/proj


CELERY_BIN="/home/john/.projenv/bin/celery"


# App instance to use
CELERY_APP="proj"
# Where to chdir at start.
CELERYD_CHDIR="/path/to/django/proj"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=1"
# %n will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
# Workers should run as an unprivileged user.
#   You need to create this user manually (or you can choose
#   a user/group combination that already exists (e.g., nobody).
CELERYD_USER="john"
CELERYD_GROUP="www-data"
# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1
export SECRET_KEY="somekey"

Start the deamon:

service celery start

That's it!‌

Wednesday, 20 June 2018

How to concatenate mp4 videos using ffmpeg on Ubuntu

for f in $(ls *.mp4); do
    ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
done

CONCAT=$(echo $(ls *.ts) | sed -e "s/ /|/g")

ffmpeg -i "concat:$CONCAT" -c copy -bsf:a aac_adtstoasc output.mp4

rm *.ts


Source

Friday, 29 December 2017

How to install Node.js 8/9 in Ubuntu/Xubuntu


nodejs-logo


  • Simply go to the official website of Node.js or click here to download th latest version of node.js.
  • After you downloaded the package, extract it using the command :
    tar -xf name_of_package
  • Rename the folder just made after extraction of the package to nodejs. Command is this :mv name_of_package nodejs
  • Copy the folder nodejs inside the ‘bin‘ folder in root directory using the command:
    sudo cp -r nodejs /bin
  • At this point, you need to add the nodejs binaries in the PATH variable so that they can be executed from anywhere.
    There are two ways to do this. I am using the less popular way to keep things easy. What you have to do is that copy the files inside nodejs/bin
    Use this command : sudo cp /bin/nodejs/bin/*/usr/local/bin/
  • Okay all done. Check the version of Node.js and NPM using the following commands :
    • node -v
    • npm -v
  • If you face any error while checking the version of npm, one like this “Error: Cannot find module ‘../lib/utils/unsupported.js’ “ do the following :
    • Remember the ‘nodejs’ folder in the ‘Downloads’?
    • Go to Downloads folder, and give the following series of commands :
      • Delete the ‘npm’ in usr/local/bin
        Use this command: sudo rm usr/local/bin/npm
      • cd ~/Downloads [Go to the Downloads Folder]
      • cd nodejs/bin [Go inside the binaries folder of node.js]
      • Execute npm from inside the node binaries file to install latest version of NPM globally use the following command:
        sudo ./npm install npm@latest -g
      • Done !
  • Now finally you can use the latest NPM v5.0.1 and Node.js v8/9
  • Suggestions/corrections are welcome
  • Thanks ! Happy coding

Monday, 11 December 2017

Monero CPU mining on Debian 8 using xmr-stak-cpu

Monero Mining Is For Everyone!

Unlike Etherum, you can mine Monero with about anything, even your old laptop CPU. That won’t yield much profit, but it is a possibility. On top of that, GPU mining does not offer a substantial increase in mining power as in Ethereum. Here, a dedicated GPU can offer only 2 to 3 times greater hashrate than a new CPU, instead of Ethereum’s 10 to 20 times. This means you’re better off setting all your old laptops to mine than purchasing a new GPU, unless of course, you intend to set up a whole farm.
You can also mine with a CPU and a GPU at the same time. Bear in mind that this probably has greater power draw, and you ought to calculate whether what you’re doing is profitable. Let’s see how to mine using just a CPU.

How Intall XMR-STAK-CPU - Monero Mining Tool

I recommend using xmr-stak-cpu, a very fast, cost-effective, open source solution. First off, we will have to install dependencies of the program. We will also have to compile it, so we need software for that, too. Run:
sudo apt install libmicrohttpd-dev libssl-dev cmake build-essential libhwloc-dev
Then, download the miner:
wget https://github.com/fireice-uk/xmr-stak-cpu/archive/master.zip
Unzip it:
unzip master.zip
Enter the new directory:
cd xmr-stak-cpu-master
Run CMake:
cmake .
After you’re done, install the miner:
make install
It should create a „bin” folder. Enter the folder:
cd bin

How to Configure

(notice we are dealing with config text in bin folder, not the one in the root)

And edit the configuration file. Usually, all you need to do is specify your pool address, wallet address and „pool password”, which for the pool we’re using is just a machine name and email. Here is an example.
nano config.txt
"pool_address" : "pool.supportxmr.com:3333",
"wallet_address" : "yourmonerowalletaddresshere",
"pool_password" : "worker:name@email.com",
You can find other pool addresses here under ` Pick a server and port`.

Once you set the configuration file, run
./xmr-stak-cpu
And your machine will start mining with your CPU!

If you want to start the process in the background run this:

nohup ./xmr-stak-cpu &

You might see nasty messages like this:

[2017-07-09 12:04:02] : MEMORY ALLOC FAILED: mmap failed

This means that you can get around a 20% hashrate boost by enabling large pages.

Large pages on Linux

Firstly stop the miner (if it's running), run the following commands to enable large pages and then start the miner as root:
sudo sysctl -w vm.nr_hugepages=128

----------------------------

Disclaimer:

This is a slight modification of a tutorial originally written by Maciej Borkowski for Ubuntu 16.04 but I tested for Debian 8 and it works flawlessly. So thought it worth copying with slight midifications for future reference and maybe help others too, as I had difficulty with similar tutorials for Ubuntu on Debian.


Tuesday, 27 June 2017

Torrent Seedbox with Transmission on Debian VPS

Installation et configuration de Transmission

INSTALLATION DES PAQUETS

C’est la partie la plus facile, il suffit d’installer le paquet transmission-daemon :
Note : vous pouvez aussi compiler les sources si vous souhaitez avoir la derniĂšre version, celle des dĂ©pĂŽts accusant un lĂ©ger retard…
sudo apt-get install transmission-daemon
Et voilĂ , votre seedbox est prĂȘte ! Enfin presque… Il nous reste un peu de configuration afin de la moduler Ă  nos besoins.

CONFIGURATION DE LA SEEDBOX

Avant de commencer Ă  mettre les mains dans le cambouis, quelques petites choses Ă  savoir :
  • Le seul fichier de configuration que vous aurez Ă  modifier est /etc/transmission-daemon/settings.json
  • Avant de modifier ce fichier, pensez Ă  arrĂȘter le service transmission-daemon :
    sudo service transmission-daemon stop
    Vous pouvez aussi modifier la configuration de Transmission alors que celui-ci est dĂ©marrĂ©, mais ne faites surtout pas un sudo service transmission-daemon restart car vous perdrez toutes vos modifications (l’expĂ©rience parle…). Pour charger votre configuration, faites :
    sudo service transmission-daemon reload
Maintenant on peut y aller 🙂 On Ă©dite donc le fichier /etc/transmission-daemon/settings.json. Ce fichier de configuration est formatĂ© en JSON et se dĂ©coupe en plusieurs parties. Vous pouvez trouver la documentation dudit fichier par ici car je ne vais pas tout reprendre en dĂ©tail.

Gestion de la bande passante

Comme je ne veux pas que Transmission consomme toute la bande passante du serveur (on a d’autres services qui tournent), j’ai choisi de brider sa bande passante Ă  7Mo/s de 8h Ă  minuit, tous les jours. Voici la configuration que j’utilise :
{
    "alt-speed-down": 7000,            // Limite Ă  7000Ko/s en download
    "alt-speed-enabled": true,         // Limite activée
    "alt-speed-time-begin": 480,       // Le bridage commande Ă  8h, soit 480min aprĂšs minuit
    "alt-speed-time-day": 127,         // 127 en binaire = 1111111 (sept 1 pour sept jours, voir doc.)
    "alt-speed-time-enabled": true,    // On active la planification
    "alt-speed-time-end": 1439,        // Heure de fin, 1439 min aprĂšs minuit, soit 23h59
    "alt-speed-up": 7000              // Limite Ă  7000Ko/s en upload
}

Gestion des fichiers et répertoires

Ici, le plus simple est de crĂ©er un utilisateur systĂšme, utilisĂ© par Transmission, pour qu’il puisse disposer d’un dossier oĂč dĂ©poser les tĂ©lĂ©chargements. Je vous propose les commandes suivantes. Elles crĂ©ent un utilisateur downloader dans le groupe debian-transmission avec son home (/home/downloader) dans lequel nous crĂ©ons un dossier incomplete/ :
sudo useradd -d /home/downloader -m downloader
sudo usermod -a -G debian-transmission downloader
sudo passwd downloader
sudo mkdir /home/downloader/incomplete
sudo chown -R downloader:debian-transmission /home/downloader
Au niveau de la configuration, voici ce que cela donne chez moi :
{
    "download-dir": "/home/downloader",              // Dossier de destination des téléchargements
    "incomplete-dir": "/home/downloader/incomplete", // Le dossier incomplete/ contient tous les téléchargements en cours
    "incomplete-dir-enabled": true,                  // ils sont déplacés dans download-dir lorsqu'ils sont terminés.
}

Configuration de l’interface web

C’est lĂ  que ça devient intĂ©ressant. Cette interface web (dite RPC) sera un moyen facile de contrĂŽler Transmission Ă  distance, on va donc faire cela aux petits oignons :). Voici ce que je vous propose :
{
    "rpc-authentication-required": true, // On l'authentification par mot de passe
    "rpc-enabled": true,                 // On active l'interface
    "rpc-password": "Password",          // Tapez votre mot de passe en clair, il sera chiffré à l'enregistrement
    "rpc-port": 9091,                    // Port d'accĂšs Ă  l'interface
    "rpc-url": "/",                      // Url d'accĂšs Ă  l'interface
    "rpc-username": "Utilisateur",       // Le nom d'utilisateur pour l'authentification
}

Autres petites choses utiles

Quelques petits autres paramĂštres que j’ai modifiĂ©. LĂ , c’est Ă  vous de voir…
{
    "ratio-limit": 5,            // Le partage est arrĂȘtĂ© au bout de 5 partages
    "ratio-limit-enabled": true, // La limite de ratio est activée
}
En fait cette option me permet de plus ou moins bien gérer ma bande passante et mon ratio de partage.
Toutes les autres options ont Ă©tĂ© laissĂ©es par dĂ©faut car je n’en ai pas eu besoin, n’hĂ©sitez pas Ă  aller faire un tour sur la documentation de Transmission pour plus d’informations, ou laissez un commentaire 😉
Il ne vous reste plus qu’Ă  enregistrer votre fichier et Ă  recharger le daemon :
sudo service transmission-daemon reload
Vous devriez maintenant pouvoir accĂ©der Ă  l’interface web de transmission, Ă  l’adresse et au port prĂ©alablement dĂ©fini. Pour nous c’Ă©tait http://mon.nomdedomaine.com:9091/. On s’authentifie et on atterri sur une page comme celle-lĂ  :
Je pense que les icĂŽnes sont assez explicites… En haut vous avez, de gauche Ă  droite :
  • Ajouter un torrent en envoyant directement une URL ou un .torrent
  • Supprimer un torrent de la liste (il ne sera pas supprimĂ© du dossier /home/downloader/)
  • DĂ©marrer le torrent sĂ©lectionnĂ©
  • Suspendre le torrent sĂ©lectionnĂ©
  • DĂ©marrer tous les torrents
  • Suspendre tous les torrents
  • Obtenir des informations sur le torrent sĂ©lectionnĂ©.
Dans la barre d’outil en bas, toujours de gauche Ă  droite, on a :
  • Deux premiers boutons de paramĂštres que je vous dĂ©conseille d’utiliser car j’ai rencontrĂ© quelques souci avec. PrĂ©fĂ©rez modifier directement le fichier de configuration.
  • Un bouton pour activer le bridage
  • Un bouton pour modifier l’affichage parmi les diffĂ©rentes listes disponibles
VoilĂ , avec ça on commence Ă  avoir quelque chose d’intĂ©ressant et de bons moments de partage en perspective. Maintenant, je vous propose de peaufiner la gestion des URLs de notre interface car devoir ajouter le port 9091 Ă  la fin de notre adresse n’est pas trĂšs joli. De plus, il nous contraint d’ouvrir un port de plus sur le pare feu, ce que je n’aime pas trop alors qu’Apache gĂšre trĂšs bien la redirection de port. Sans parler de la sĂ©curitĂ© : plus notre interface est cachĂ©e mieux c’est.
Nous allons donc crĂ©er un vhost Apache qui nous permettra d’accĂ©der Ă  l’interface de Transmission via l’url http://torrent.mondomaine.com.

Configuration d’un vhost Apache pour Transmission

On commence par activer les modules Apache dont nous allons avoir besoin :
sudo a2enmod  rewrite proxy proxy_http
On crée le vhost :
sudo vim /etc/apache2/sites-available/transmission
Pour lequel je vous propose le contenu suivant :
<VirtualHost *:80>
        ServerName torrent.mondomaine.com
 
        RewriteEngine On
        RewriteRule ^/$ /web/ [L,R=301]
 
        ProxyPass / http://127.0.0.1:9091/
        ProxyPassReverse / http://127.0.0.1:9091/
</VirtualHost>
Il ne reste plus qu’Ă  redĂ©marrer tout ça :
sudo service apache2 restart
Vous devriez maintenant pourvoir accéder à Transmission via la nouvelle URL, http://torrent.mondomaine.com.

Configuration d’un server block Nginx

Cette configuration a fait l’objet d’un article ! C’est par ici que ça se passe đŸ™‚

POUR ALLER PLUS LOIN…

On peut en effet pousser la chose plus loin en forçant l’utilisation de HTTPS par exemple. Cela rajouterait une couche de sĂ©curitĂ© en chiffrant tous nos Ă©changes avec le serveur, notamment au moment de l’authentification. Je ne vais pas le dĂ©tailler ici mais vous pouvez vous inspirer de mon tutoriel sur phpMyAdmin si ça vous tente.
Vous voilĂ  maintenant avec une seedbox prĂȘte Ă  tĂ©lĂ©charger et partager du contenu Ă  la vitesse de l’Ă©clair. Si vous utilisez un pare feu, pensez Ă  ouvrir les ports spĂ©cifiĂ©s dans settings.json (51413 par dĂ©faut) et Ă  fermer le 9091, qui ne vous servira plus.
Je pense que j’ai fait le tour de la question, si malgrĂ© tout vous en avez, n’hĂ©sitez pas Ă  utiliser les commentaires, ou la page de contact 😉
Cet article vous a plu ? Partagez-le sur les réseaux sociaux !