Change ownCloud User Home in MYSQL

So I recently created a new ownCloud 10 server to get away from ownCloud 9. This meant creating a new CentOS 7 VM bladybladyblah…

One thing that caught me out, among many to do with ownCloud, was that the original user created during the setup process couldn’t save or view files after I had reconfigured the home directory to be more secure.

After looking in the MYSQL database, I saw that the original user’s home directory had not been updated to match the new path. To check this I used the following commands and looked for the home column:

USE owncloud;
SELECT * FROM oc_accounts;

After those commands, I updated the users home setting by using the following command:

UPDATE oc_accounts SET home="/new/dir/username" WHERE user_id="user";

Nice simple fix for an issue that was driving me up the wall.

Hope you enjoy!

Linux Directory Checking Script

Morning! Oh wait, it’s the afternoon…

Today, I finally got around to making a script that will run automatically on my network storage server (Raspberry Pi with a dinky USB hard drive) and check if the USB HDD is accessible.

This issue started a couple of weeks ago where I was getting weird IO errors on the USB disk about every 2 weeks. Instead of buying a new drive, creating a RAID array or anything else equally as intelligent and appropriate, I decided to just reboot my Raspberry Pi every time this happened. Now, I don’t want to do this manually every time so I finally created a script and added it to my cron jobs.

You can see the script I used below:

#!/bin/bash

if [ ! -d "path/to/check" ]; then
    #Directory is not found and HDD is not okay, do whatever is below
    uptime=$(uptime)
    currenttime=$(date)
    echo "Host rebooted at $currenttime. Uptime was$uptime" >> /path/to/output.txt
    sudo reboot
fi

My crontab job is running as root because the sudo reboot part was giving me a couple of issues. This is the entry in the root crontab:

@hourly /path/to/sh/file

Enjoy!

Swaks Email Scripting

Hi, in this blog post I will show you how I configured Swaks for sending emails using SMTP using my custom SMTP server.

First I ran the following command to install swaks:

sudo apt-get install swaks

With swaks installed, I could start building a test command just to
see if this would actually work. I started with the below command:

swaks --to destination@email.com --from source@email.com --auth --auth-user=source@email.com --auth-password=passwordforsource@email.comaccount --server smtp.example.com -tls

This gave me an error along the lines of “Could not authenticate – connection refused” after talking to the people hosting my SMTP server, I found out that I needed to make sure that I was using port 587 and not the default port of 465.

So I made sure that my command explicitly used that port by adding it to the server parameter and managed to get an email to successfully send. You can see the code I used below:

swaks --to destination@email.com --from source@email.com --auth --auth-user=source@email.com --auth-password=passwordforsource@email.comaccount --server smtp.example.com:587 -tls

Extra

I wanted to use this in a script so that I could launch the script and an email would get sent. I also wanted to change what would get sent in the actual email since currently, it was just using the default values.

After 5 or so minutes of cobbling a script together, I came up with what you can see below:

#!/bin/bash

hostname=$(hostname)
uptime=$(uptime)

swaks --to destination@email.com \
--from=source@email.com \
--auth \
--auth-user=source@email.com \
--auth-password=password-for-source-email \
--server smtp.example.com:587 \
--body "$hostname - uptime is $uptime" \
--header "Subject: $hostname is still up" \
-tls \

Now I can send this email whenever I want, I even created a CRON job to send the email every hour. Enjoy!

Unix Permissions Winform

In this post, I will show case a winform application that I have just finished building which will tell you the correct command when given the required permissions. For example, a read permission is identified as a 4 in Unix environments.

I have created a small table below:

 Permission Level  Permission Bit
 Read  4
 Write  2
 Execute  1

Since you need to define permissions for: the owner; owner group and others, you need to supply 3 permission bits per command. Plus one more for special permissions at the beginning but we can ignore that for now.

So if we wanted to give the below permissions:

Special – ignore

Owner – read (4), write(2) and execute(1)

Owner group – read(4) and write(2)

Other – read(4)

we would use the following command: chmod 0764 <path-to-file>

Now we can get on with the actual winform… I created this to tell me what permissions I needed to assigned. Below is a screenshot of the winform:

Front Winform

This also keeps a short history of the permissions in the history textbox which is flushed after so long to stop the textbox from overflowing. Here is a download for the project, in the zip folder is both a ps1 file and an exe file. Enjoy!

 

 

 

Coloured BASH Output Using TPUT

From my previous post, found here, you can see that I have formatted the text to be a specific colour depending on what sort of output I get. So for errors I make the text red and for successful messages I make the text green.

This is easy to implement into BASH scripts and a lot of other formatting can be applied as well. In this post, I will be covering the colorization (probably not a word), underlining, bold text and resetting the changes.

Colour Possibilities:

  • 0 – Black
  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White
$(tput setaf 1)TEXT HERE

Bold Text:

#Starts bold characters

$(tput bold)TEXT HERE

#Ends bold characters

$(tput sgr0)TEXT HERE

I would just like to add here that tput sgr0 removes all formatting and returns text to the default style and colour.

Underlining:

#Starts underlining

$(tput smul)TEXT HERE

#Ends underlining

$(tput rmul)TEXT HERE

Below is a full script which includes all the possible combinations of the examples above, apart from black text.

#!/bin/bash

echo "
regular bold underline
$(tput setaf 1)Text $(tput bold)Text $(tput sgr0)$(tput setaf 1)$(tput smul)Text$(tput rmul)
$(tput setaf 2)Text $(tput bold)Text $(tput sgr0)$(tput setaf 2)$(tput smul)Text$(tput rmul)
$(tput setaf 3)Text $(tput bold)Text $(tput sgr0)$(tput setaf 3)$(tput smul)Text$(tput rmul)
$(tput setaf 4)Text $(tput bold)Text $(tput sgr0)$(tput setaf 4)$(tput smul)Text$(tput rmul)
$(tput setaf 5)Text $(tput bold)Text $(tput sgr0)$(tput setaf 5)$(tput smul)Text$(tput rmul)
$(tput setaf 6)Text $(tput bold)Text $(tput sgr0)$(tput setaf 6)$(tput smul)Text$(tput rmul)
$(tput setaf 7)Text $(tput bold)Text $(tput sgr0)$(tput setaf 7)$(tput smul)Text$(tput rmul)
"

I know it looks quite horrible in the source code but this is what the output looks like:

Full possibilities

Enjoy!

Installing And Using Samba Shares On Linux

To install samba capabilities on your Linx box, run the following commands:

sudo apt-get install samba samba-common-bin

This will run off an install process, note that it may ask you if you’re sure about installing this, press y to accept and continue.

Now edit the samba configuration file using:

sudo nano /etc/samba/smb.conf

Go all the way to the end of the file and add the following lines…

  • [share]
  • comment=Pi Share
  • path= DIRECTORY LOCATION e.g. /mnt/library
  • browseable = yes
  • writeable = yes
  • only guest = no
  • create mask = 0777
  • public = no
  • guest ok = no

 

At this point your may need to restart the smb service using the following command:

sudo service smbd restart

You may need to know the IP address of the Linux share in order to access it, you can do this by using “ifconfig” and looking for something along the lines of “192.168…” on the eth0 interface”

To access the share using the Windows and R key to bring up the run dialog box and type in the IP address of the Linux box or its hostname. It would now ask you for credentials, here you should use your regular Linux credentials.

You can see this below:

Credentials Prompt

Once it has accepted my credentials I get the following:

Samba Share

Hopefully, you get the same as I did, if not then leave a comment. Enjoy!

 

Securely Installing and Using FTP on Linux

First, install VSFTP (Very Secure File Transfer Protocol) using the following command:

sudo apt-get install vsftpd

Now edit the vsftpd.conf file to perfect the setup of VSFTP

open the file using:

sudo nano /etc/vsftpd.conf

You should be shown something similar to below:

vsftpd.conf

Make the following changes to the configuration file…

  • anonymous_enable=NO
  • local_enable=YES
  • write_enable=YES
  • chroot_local_users=YES

now add the following lines to the bottom of the file…

  • force_dot_files=YES
  • allow_writable_chroot=YES

 

Now restart the vsftpd service using:

sudo service vsftpd restart

You should now be able to connect to you Linux box using a program such as FileZilla using your username and password. You may need to get the IP address of your Linux box before you know where to connect to. So on your Linux use the command:

ifconfig

to know where to connect to.

You will notice that you cant move out of your home directory. I have this setup fo security reasons but if you wish to remove this then simply comment out the chroot_local_users part of the vsftpd.conf file.

I will be showing in a future blog how to upload files to external drives whilst chrooted in your home directory. Chrooted helps with security as it means that even if someone gets your username and password, they still cant browse the entire system.

Enjoy!

Making External VFAT Drive Writable on Linux

 

Prerequisites: 

I would install nano, this is a text editor on Linux which is MUCH easier to use for beginners than vi. You can do this by using “sudo apt-get install nano” or “yum install nano” depending on your distribution of Linux.


First we will need to find where in /dev your drive is being displayed. You can do this by running:

lsblk

which will display all the drives currently connected to your Linux machine. The easiest way to see which drive you want is to check the size of the drive. For example, I have connected a 500GB hard drive so when I run “lsblk” I get the following:

lsblk

From this screenshot you can already see that I have mounted the drive partition I require, which in my case is sda3.

Now you need to create a directory in the mnt directory as this is were you should mount drive in Linux to keep things tidy. You can do this by using:

sudo mkdir /mnt/library

I have called by directory “library” but you can call it whatever you want.

Now we need to configure this in the fstab file which basically tells the system what to do with connected drives once the system starts.

You can open the fstab file by using:

sudo nano /etc/fstab

This should give you something similar to below:

fstab 1

Now go to the bottom of the file and add something similar to the one I have added, you can see this below:

fstab

Just for reference the line I have added is:

“/dev/sda3         /mnt/library          vfat          users,umask=00          0          0”

Make sure to check the directory locations as you might have called your something else and your drive label might be different.

You can now check in your /mnt/library directory and you should see that your drive has been mounted and you can delete and add new files to the location.

Enjoy!

Linux New Users Form

Following on from my recent upload on Linux scripting, I have yet again created a BASH script to make my Linux’ing life easier. This is also my second script created in BASH so I guess i’ve accomplished something by not running for the hills…

What I needed was a script to make creating FTP users easier on my CentOS box. Below is a list of things I needed the script to accomplish:

  • Get a username from a user prompt
  • Get a Description from a user prompt
  • Create the new user
  • Change the users password
  • Add the username to /etc/vsftpd.userlist
  • Add the username to /etc/vsftpd/chroot_list
  • Make a directory in the home folder of the user called “ftp”
  • Change the permissions and ownership on this directory
  • Make a directory in the “ftp” folder called “files”
  • Change the permissions and ownership on this directory
  • Ask the user to create a share or not
  • Get a share name from user prompt
  • Make a directory in the users “files” folder with the same name as the share name
  • Mount the share to the “files” directory
  • Ask if the new user is the owner of the share
    • if so then change the ownershipa and permissions on the share
    • if not then just change the permissions on the share
  • Finish

Here is my script for achieving these goals:

#!/bin/bash
#Creating new FTP users

##Gathering Variables
echo "Enter a username"
read Username

echo "Enter a description"
read Description

useradd -m -c "$Description" -s /bin/bash $Username

passwd $Username

echo "$Username" | tee -a /etc/vsftpd.userlist
echo "$Username" | tee -a /etc/vsftpd/chroot_list

mkdir /home/$Username/ftp
chown nobody:nobody /home/$Username/ftp
chmod a-w /home/$Username/ftp

mkdir /home/$Username/ftp/files
chown $Username:$Username /home/$Username/ftp/files
chmod 0700 /home/$Username/ftp/files

read -p "Create a share? [yn]: " CreateShare
if [[ $CreateShare = y ]] ; then

 read -p "Enter a share name: " ShareName

 mkdir /home/$Username/ftp/files/$ShareName
 mkdir /home/shares/$ShareName
 mount --bind /home/shares/$ShareName /home/$Username/ftp/files/$ShareName
elif [[ $CreateShare = n ]] ; then
 read -p "Enter the pre-existing share name: " ShareName
 mkdir /home/$Username/ftp/files/$ShareName
 if [ -d /home/shares/$ShareName ] ; then
  echo "Mounting share"
  mount --bind /home/shares/$ShareName /home/$Username/ftp/files/$ShareName
 else
  echo "Cannot find the share name : $ShareName"
 fi
else
 echo "Not a y or n"
fi

read -p "Is $Username the owner of this share? [yn]: " ShareOwner
if [[ $ShareOwner = y ]] ; then
 chown $Username:$Username /home/shares/$ShareName
 chmod 0775 /home/shares/$ShareName
else
 echo "$Username is not the owner of $ShareName"
 chmod 0775 /home/shares/$ShareName
fi

echo "Finished creating user : $Username" * Insert your code here

Hopefully somebody gains something from this, probably not though. Enjoy!

Linux Mounting Script

Recently I have set up a VSFTPD CentOS 7 server and chrooted all the local users to their home directory. *If you know what that means $YourPoints = $YourPoints + 5*

But when ever the system was rebooted or for any other reason that I don’t fully understand yet, the mounts from the shares to the users home directory would get lost. Because the users were chrooted into their home directories, I couldn’t use symbolic or hard links. Instead I had to use the:

mount --bind

option to make the mount accessible to the chrooted users.

But since the mount kepts getting lost and I had grown tired of the users complaining that they couldn’t access the share. Plus it was taking too long manually remounting all the shares, let alone finding out which ones had become disconnected before the users started to complain. So I created a script to check and ask me if I wanted to mount the share.

Just so you know, this is my first time scripting in BASH and I only did it because of my short lived love with Linux. Below is an example of the code in my script, I basically created one of these functions for each user:

#TESTING USER3
if mountpoint -q /home/user3/ftp/files/share
 then
 echo "user3 is mounted"
 else
 echo "user3 is not mounted"
 read -p "Do you want to mount share? (y or n):" REPLY
 if [[ $REPLY = y ]]
  then
  mount --bind /home/shares/share/ /home/user3/ftp/files/share
 fi
fi

This way all I have to do is run this script and it will mount all of the shares for me. Let me know if there is an easier way to do this or if im missing something obviouse. Enjoy!