Labels

Arduino (1) Bugs (3) C (1) Install (3) Linux (25) Python (10) Raspberry Pi (4)

Friday, 31 July 2015

How to only allow specific users SSH access

This post shows you to set the users that are allow SSH access.
If you don't have SSH installed then run
On Ubuntu/Debian/Mint
sudo apt-get install openssh-client openssh-server
On RHEL/Fedora/Centos
sudo yum -y install openssh-clients openssh-server

To make things easier login with root using su - as most of the commands need root privileges.
Or you can just put sudo in front of all of these commands.
Firstly make a backup of the file we will be working with in case you want to return to your original setup.
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

The line we are going to add to the file is AllowUsers "Put users here separated by spaces"

So add the line and then after it a comment for future reference something like only allow SSH access with these users

Now we need to restart the sshd service
If you are running Ubuntu/Debian/Mint run
service ssh restart
If you are running RHEL/Fedora/Centos
service sshd restart

If you are currently logged in over ssh this will close the connection and a new one will be created.

Now if you try and login with a user not specified in the sshd config file
ssh "blocked user"@"ip of pc"
You will not be able to
But if you try to login with a user specified in the sshd config file
ssh "specified user"@"ip of pc"
You should be able to login as usual.

Friday, 24 July 2015

How to configure a static IP on linux

Normally your Linux setup will come with come with a DHCP setup which requests a IP address and gets given one. The Linux box has no control over the IP address that its given this is fine if you are just browsing the web but if you want to ssh into the box or set it up as a web server it can get quite annoying if the IP just changes. A static IP address means that it will always be the same which makes it easy to access.

First we need to find your gateway address this is the address your box goes through to get to the Internet.

route -n
You should get something that looks like this:
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      
0.0.0.0         192.168.0.1     0.0.0.0         UG    303   
192.168.0.0     0.0.0.0         255.255.255.0   U     303    

My gateway address is 192.168.0.1

Now we need to open /etc/network/interfaces but before we do that make a backup as from previous experiences if you mess this up its hard to fix.
sudo cp /etc/network/interfaces /etc/network/interfaces.backup
Now open the file with a editor of your choice.
sudo vi /etc/network/interfaces
And edit the line that looks like
iface eth0 inet dhcp
to
iface eth0 inet static
Now add these three lines underneath that
address 192.168.0.100 #This is the static ip
netmask 255.255.255.0 #This is the netmask which is a 24 bit netmask.
gateway 192.168.0.1 #This is the gateway address we found earlier


Edit: Better way instead of rebooting
ifconfig eth0 down;ifconfig eth0 up

Now we need to restart for the effects to take place.
sudo reboot

If it all works you may want to remove the backup file to save space.

Tuesday, 21 July 2015

How to find out the your WAN address using the command line.

A WAN address is the IP address that the router uses to connect to the internet and it is shared by all of the devices on that LAN network.

We are going to be using tnx.nl/ip to get our IP as this just sends the IP in plain format so its easy to read.

Now lets create a script to get our IP.

Line 1:
This tells the shell to use the sh interpreter.
#!/bin/sh


Line 2:
This gets you IP using wget and tnx.nl and stores it in the variable myIP
myIP=`wget -qO- tnx.nl/ip`


Line 3:
This prints out your IP address.
echo "Current IP address is: $myIP"



Sunday, 19 July 2015

How to make a LED flash with the Raspberry Pi.

To flash a LED we will use the GPIO pins(general purpose input/output) to control the LED. We will control these GPIO pins using python.
The GPIO layout on the Raspberry Pi model B.












We will be using GPIO 17 which is Pin 11.

Installing the library for python.
sudo apt-get install python-dev python-rpi.gpio

Now create a file called blink.py
vi blink.py
Add the following lines

import time
import RPi.GPIO as GPIO

This imports the two libraries, the time library provides the delay for the LED to flash and the RPi.GPIO allows us to control the GPIO pins.

pin = 17

This sets pin to 17


GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)



This sets the pin mode to BCM so we can reference the pins and sets up pin 17 for output

while True:
    GPIO.output(pin, True)
    time.sleep(0.5)
    GPIO.output(pin, False)
    time.sleep(0.5)
This is the Loop where the LED is turned on then turned off.

Now connect the positive leg of your LED to pin 17 and the negative to ground.
When you run the code you should see the LED flash.

Monday, 13 July 2015

How to use the ping command

Ping is a program that is used to test the reachability of a host. It also measures the time it takes to send the packet and receive one.

It does this by sending a ICMP echo request packet and waiting for a reply. Pretty much all Linux flavours will come with Ping pre-installed.


How to ping using a specific interface
ping -I wlan0 google.com
This will ping google using the wlan0 interface

How to send a certain amount of ping requests
ping -c 4 google.com
This will send 4 ICMP echo requests to google

How to change the timeout of a ping
ping -w 5 google.com
This changes the timeout of the ICMP request to 5 seconds

How to ping yourself to check your interface is working
ping 0
or
ping 127.0.0.1
or
ping localhost
They all essentially do the same thing which is ping yourself.

Most ping output should look something like this:

matty@matty-ThinkPad-T420 ~ $ ping google.com
PING google.com (216.58.210.14) 56(84) bytes of data.
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=1 ttl=55
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=2 ttl=55
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=3 ttl=55
64 bytes from lhr08s06-in-f14.1e100.net (216.58.210.14): icmp_req=4 ttl=55
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 26.039/32.973/37.669/4.941 ms




Sunday, 12 July 2015

How to change your MAC address on linux at boot

I already have a post about what a MAC address is and how to find it so if you don't know what these things are look here and have a look.

How to change your MAC address at boot?
We need to edit the /etc/network/interfaces to change the MAC address of the network card before it turns on.
We do this using the pre-up command.
What the pre-up command does is it runs the command before the network card turns on and connects, this means that you will always have that MAC address.

First of all we need to be root then run
vi /etc/network/interfaces Note: Advised to make backup i.e. cp interfaces interfaces.bak
You should get something like this

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp


We need to add
pre-up ifconfig eth0 hw ether 00:00:00:00:00:01
To the end of the file.
Now reboot and check your MAC address and it should be changed.






Saturday, 11 July 2015

How to change mac address on linux temporarily

Whats a MAC address?
A MAC address is a physical way of addressing your network card at a hardware level.

How to find out your MAC address?
Open up a terminal and run
ifconfig wlan0 | grep HWaddr 
Where I have wlan0 you would put your networking device.
My output:
wlan0     Link encap:Ethernet  HWaddr 8c:a9:82:6c:9b:da
 
As you can see my MAC address is 8c:a9:82:6c:9b:da.

How to change your MAC address temporarily?
Open up a terminal as root and run
ifconfig wlan0 down
ifconfig wlan0 hw ether 00:00:00:00:00:01
ifconfig wlan0 up

This will have changed your MAC address to 00:00:00:00:00:01.
To show this run ifconfig wlan0 | grep HWaddr
This change will not survive a reboot.

Tuesday, 7 July 2015

How to change the hostname on ubuntu server.

Your hostname is a label that is assigned to your computer so that it can be identified.

There are only a couple of steps:

Step 1:
Edit /etc/hosts
sudo vi /etc/hosts
You dont have to use vi you can open it with any text editor
Here you should see your current hostname, change it to the one you want.

Step 2:
Edit /etc/hostname
sudo vi /etc/hostname
Here you should see not the hostname you just entered into /etc/hosts but the old one.
Change it to your new one.

Step 3:
Restart the hostname service
sudo service hostname restart
or
sudo /etc/init.d/hostname restart

Saturday, 4 July 2015

How to burn an ISO to CD or DVD using Wodim

Wodim is a command line tool for burning ISO files to disks.

Step 1:
Locate you CD/DVD writer.
wodim --devices
matty@matty-ThinkPad-T420 ~ $ wodim --devices 
wodim: Overview of accessible drives (1 found) :
-------------------------------------------------------------------------
 0  dev='/dev/sg1' rwrw-- : 'MATSHITA' 'DVD-RAM UJ8A0A'
-------------------------------------------------------------------------

As you can see here my devices is /dev/sg1.

Step 2: 
If your CD/DVD is blank then skip this step but if its not then erase the disk.
wodim dev=/dev/sg1 blank=fast

Step 3:
Burn your ISO image to the CD/DVD.

wodim -v dev=/dev/sg1 speed=10 -eject "path-to-iso"

This could take a while so be patient.

Friday, 3 July 2015

Installing VMware Player on debian/ubuntu.

VMware is a free piece of software that allows Virtualization of a guest operating system inside of an operating system. This can be useful when developing or trying out new distros.

To install VMware player we first need to download it.
Lin


You then need to make it executable by running:
sudo chmod +x VMware-Player-7.1.2-2780323.x86_64.bundle

Now run the executable:
sudo ./VMware-Player-7.1.2-2780323.x86_64.bundle

Now follow the installer steps then you will be good to go.

If you get a error about gcc then you need to install dependencies:

apt-get install build-essential linux-headers-`uname -r`

Thursday, 2 July 2015

Basic script to ping all hosts online.

This shell script is a script that pings all the hosts between 192.168.0.1-254. It returns if they are online or not.

#!/bin/sh
for i in `seq 1 254`
do
  ping -c 1 -t 1 192.168.0.$i | grep "64 bytes" &
  sleep 0.1
done

This takes 26 seconds.

If you run the code like this it is significantly slower as the ping command runs in the foreground. To background it you need to add a & at the end of it.

#!/bin/sh
for i in `seq 1 254`
do
  ping -c 1 -t 1 192.168.0.$i | grep "64 bytes" amp;
  sleep 0.1
done