Allow users of a certain group to run a command without sudo passwd

Time to time I usually find myself typing sudo to execute commands requiring sudo rights, and this is repetitive which simply means one gets weary of typing sudo password every time, hence this blog post. Reason for this is to remind me how to do it the next time I am faced with such conundrum(s).

Suppose I wanted to add a group of users who are allowed to run mount and umount without passwords. So I first want to add a group called “staff”

sudo groupadd staff

Next we need to edit the /etc/group and add the users

staff:407:

will be present ,hence append users you want to add the users separated by commas.

staff:x:407:user1,user2,...

Now we need to configure sudo to allow members of the “staff” group to actually invoke the mount and umount commands.

You just need to add the following lines to /etc/sudoers or execute sudo visudo

%staff ALL=NOPASSWD: /sbin/mount, /sbin/umount

Now sudo mount won’t ask password but since it is a pain in the butt typing sudo all the time, we can avoid it by doing the following:

I can create the following script called “/usr/bin/mount” (and similar script for umount)

#! /bin/sh
sudo /sbin/mount $*

To make this slightly more secure, We might want to change the ownership of these scripts to the “staff” group.

chgrp staff /usr/bin/mount /usr/bin/umount

and then make them executable only for the group “staff”

chmod g+x /usr/bin/mount  /usr/bin/umount

Note:Depending on the OS you are using please check where mount and umount commands are located. It might be in /bin/ instead of /sbin.
So you might have to make necessary changes

 

Done

Smart Doorbell using Raspberry Pi

About a year ago, I had to do a project titled Smart Home Automation using a Raspberry Pi and Arduino, part of the project  incorporated a doorbell but not just any doorbell but a Smart doorbell.

Why do I call it a Smart Doorbell?

A classic doorbell can be defined as a signalling device typically placed near an entry door to a building. When a visitor presses a button the bell rings inside the building, alerting the occupant to the presence of the visitor.

The smart doorbell, works in that manner however when a visitor presses the doorbell button it notifies the occupant with an images of the visitor and also sends an email, sms and push notification (to an smart device) to the occupant in case they are not able to hear the bell ringing.

If the button is pressed a pre-recorded voice notification is played for the occupant to check the door as there might be someone, this happens concurrently with an sms notification while a picture of the visitor is captured and sent to both email and push notification. Initially there was a feature to enable 2-way real-time video communication but due to network latency and high resource usage the feature was deprecated.

Click For A Demo

Comment if you need the code

See code snippets(Initial Revision): https://mmphego.wordpress.com/2015/01/11/smart-doorbell-using-rpi-with-voice-and-email-notification/

Setting up an MQTT server on Debian Jessie

Jolabs Tech Blog

Intro

Today we`re going to be setting up our own home automation server in a dedicated linux server. It’s gonna host our platform using the the Node.Js environment. The different devices around the house are going to be speaking MQTT to this server. I’ve chosen MQTT because of it’s robustness and lightweightness allowing for easy deployment on my favourite controllers: ESP8266. The esp-01, still the cheapest to this date,only sports 4mb of flash, has no trouble at all controlling a couple of relay’s for now. In the future the newer/bigger boards can be used to get more IO and function, but for now I’m going to be using the esp-01’s extensively around the house flexibly (it’s wireless…).

Requirements

The idea is to have the server online 24/7, so consider a computer thats not to power-hungry; like a Raspberry Pi or a small embedded box computer. Alternatively you can boot up…

View original post 324 more words

Bash terminal prompt trick

This cool trick will transform one’s dull terminal to a fancy terminal with date + time and current Git branch if any.

Copy below code to your ~/.bashrc

export PS1='\[\033[0;32m\]\[\033[0m\033[0;38m\]\u\[\033[0;36m\]@\[\033[0;36m\]\h:\w\[\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\[\033[0m\033[0;31m\] [\D{%F %T}] \$\[\033[0m\033[0;32m\] >>>\[\033[0m\] '

When done, source the bashrc file and then voila!!!

$ source ~/.bashrc

Dull Terminal

screenshot_2016-12-13_10-08-28

Epic Terminal with date, time and current git branch

screenshot_2016-12-13_10-07-58

How to mount Google Drive on your Linux(Ubuntu) system

I have always been a fan of cloud storage as anything can happen on my mechanical harddrive, but now that I have completely migrated to Ubuntu and having to realise that the is no support whatsoever for Google drive applications, I was on a quest to find a working and reliable on. Then I stumbled upon Google Drive Ocamlfuse which is a FUSE filesystem backend for Google Drive which you can use to mount your Google Drive under Linux.

Link: https://github.com/astrada/google-drive-ocamlfuse

Installation

sudo add-apt-repository ppa:alessandro-strada/ppa
sudo apt-get update
sudo apt-get install google-drive-ocamlfuse

Usage

run:

google-drive-ocamlfuse

Make sure you are connected to the internet as this will open your browser and require access to your Google Drive. Click “Allow” and this will give the application access to your files.

Mounting

Now we mount GDrive to your system.

run:

mkdir -p ~/Google_Drive
google-drive-ocamlfuse ~/Google_Drive

That’s about it, unless you want to be heroic and modify changes in the configuration file which is  here:

~/.gdfuse/default/config

Automount

To mount Google Drive upon startup, run:

crontab -e

and insert

@reboot google-drive-ocamlfuse ~/Google_Drive

Or go here: https://github.com/astrada/google-drive-ocamlfuse/wiki/Automounting

Done!!!

GitHub Repo creation via CLI

I am so excited after creating my first repository on github and successfully adding some test files to it from my local machine!
But due to my limited internet connection, I stumbled upon some diffulties.

It took me a lot of time and effort, and there were actually a few times when I thought that I wouldn’t be able to do it. It took me hours, and that is not how much it should have taken to just create a new repository.

So i had a huge challenge – How do I make more repositories and add my files to it without the need of a browser… mmmhhh after some vigorous Googling I came upon a simple command using ‘curl’.

Challenge was accepted and at 03:10am I was done creating a badass bash script that would not only create a github repository, it would initialise it and push my files to the repository with only a press of a button.

#!/bin/bash
# -*- coding: utf-8 -*-
#
#  gitrep-create
#  
#  The purpose of this script is to generate Github repository via cli instead of going to github.com
#  Instructions for use: 
#  1. Install github [debian: sudo apt-get install -y git]
#  2. Fill in/Enter details
#     git config --global user.name "Your Name"
#     git config --global user.email "youremail@domain.com"
#  3. Generate token on github.com > Settings > Applications > Generate token
#
#
#  Copyright 2015 Mpho Mphego <mpho112@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  

function progress(){
echo -n "Please wait..."
while true
do
  echo -n "."
  sleep 1
done

}

# Note:
# To run from ~/.bash_profile, uncomment function below. and copy code to ~/.bash_profile
# else create script and place it in ~/bin.

#gitrepo-create(){

  #trap "killall background" EXIT
  repo_name=$1
  dir_name=`basename $(pwd)`
  username=`git config github.user` 
  token=`git config github.token` 
  WGET="/usr/bin/wget"


 if [ "$repo_name" = "" ]; then
    echo "Repo name (hit enter to use '$dir_name')?"
    read repo_name
  fi
 
  if [ "$repo_name" = "" ]; then
    repo_name=$dir_name
  fi

  if [ "$username" = "" ]; then
    echo "Could not find username, run 'git config --global github.user <username>'"
    invalid_credentials=1
  fi

  echo Git Username: $username

  if [ "$token" = "" ]; then
    echo "Could not find token, run 'git config --global github.token <token>'"
    invalid_credentials=1
  fi

  echo "Creating Github repository '$repo_name'"
  progress &
  myself=$!

  $WGET -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
  if [ ! -s /tmp/index.google ];then
    
    echo "Check your Internet Connection."
    kill $myself &> /dev/null

  else
    curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}' > /dev/null 2>&1
    kill $myself &> /dev/null
    echo -n "...done."
    echo " "
    echo "Enter 1 to Create new repository."
    echo "Enter 2 to Push on existing repository."
    read inputs
    if [ "$inputs" == "1" ]; then
      echo "Creating a new repository."
      echo "#" $repo_name >> README.md
      git init
      echo " "
      git add README.md
      echo " "
      git commit -m "first commit"
      echo " "
      git remote add origin git@github.com:$username/$repo_name.git
      echo " "
      git push -u origin master
      echo ".......Done........"
    fi
  
    if [ "$inputs" == "2" ]; then
      echo "Pushing an existing repository"
      git remote add origin git@github.com::$username/$repo_name.git
      echo " "
      git push -u origin master
      echo ".......Done........"
    fi
    
  fi 
#}