Adding a password protected share to Ubuntu

This is one of those posts that helps me remember how to do something I knew how to do once but would inevitably forget how to do again one millisecond later. In this case it is how to configure a directory that is shared on the network but only to registered users.

First off, create a new group for the users (I will use the group name sharegroup as an example):

sudo addgroup sharegroup

Then add yourself to the group:

sudo usermod -aG sharegroup $USER

Add any other users to the group in the same way.

Now it is time to prepare the directory to be shared. Create (or choose) the directory and share it using the local network share option from the GUI. Alternatively, edit /etc/samba/smb.conf directly by adding something like this at the bottom:

[<sharename>]
  path = <full path to shared directory>
  writeable = yes
  guest ok = no
  read only = no
  browsable = yes
  create mask = 0770
  directory mask = 0770
  valid users = @sharegroup

and then restart samba:

sudo systemctl restart smbd

Then the directory group ownership must be set to the new group. Set the directory as current and enter:

sudo chgrp -R sharegroup *
sudo chgrp sharegroup .

Be very careful any time using the -R option with sudo as it is easy to completely mess up the OS! Generally it is best to start in the directory that’s being modified. That way, there’s less chance of making unintentional modifications.

Now change the permissions to allow group members to operate on files properly:

sudo chmod -R g+rw *
sudo chmod g+rw .

Then the users must be added to samba – for example:

sudo smbpasswd -a $USER

This will ask for a password to be used to access the share. That’s it!

Sending and receiving binary data using JSON encoding, Python and MQTT

I really like using JSON encoding as a way of transferring messages between processes as it is machine and language independent. Plus, it is very well suited to stream processing networks (such as rt-ai Edge) as arbitrary fields can be added to existing JSON messages and passed along. Contrast this with compiled IDLs which typically have no flexibility whatsoever.

One problem though is that binary data cannot be included in JSON messages directly. Typically base64 encoding is used to convert binary data into text. However, this is inefficient, especially in a stream processing network where base64 decoding and encoding might have to be done several times.

There are a variety of modifications to JSON around but it is very simple to just add binary data on to the end of a JSON message to form a complete message that can be transferred via MQTT for example.

In Python, an MQTT message can be published like this:

    import json
    import struct
    ...
    def publish(topic, jsonData, binData = None):
        jsonDump = json.dumps(jsonData)
        jsonString = struct.pack('>I', len(jsonDump)) + jsonDump + binData
        MQTTClient.publish(topic, jsonString)
        ...

Here, jsonData contains the normal JSON message text, binData contains the binary data to be sent along with it. To receive the message, use something like this:

    import json
    import struct
    ...
    def onMessage(client, userdata, message):
        jsonLength = struct.unpack('>I', message.payload[0:4])[0]
        jsonData = json.loads(message.payload[4:4+jsonLength])
        binData = message.payload[4+jsonLength:]
        ...

Configuring WiFi adaptors on Linux via the command line

EdimaxHardly an original post but there are so many variants around that, when I need to do this and have (as usual) forgotten how to do it, I can’t find anything that works! So here is my variant…

Add an entry for wlan0  to the /etc/network/interfaces file:

auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant.conf

Then, create /etc/wpa_supplicant.conf to use WPA encryption (this link  has examples of other modes):

ctrl_interface=/var/run/wpa_supplicant
ap_scan=1
fast_reauth=1
network={
         ssid="your_ssid"
         scan_ssid=1
         key_mgmt=WPA-PSK
         psk="your_passphrase"      
}

 That’s it for DHCP. To use a static IP address, change the /etc/network/interfaces entry (with the appropriate customizations) to be:

auto wlan0
iface wlan0 inet static
    address 192.168.2.3
    netmask 255.255.255.0
    gateway 192.168.2.1 
wpa-conf /etc/wpa_supplicant.conf

Mac Mail app crashes on launch – rebuild the envelope index

For no good reason at all my Mac Mail app started crashing on launch and would not stop. A quick Google search found this article – the key bits are reproduced here to make things easier.

Open Finder and select the Go menu option. While holding down Option, select the Library option (which is otherwise invisible). Navigate to Mail/V2/MailData. In that folder there will be a number of files starting with “Envelope Index”. Move (not copy) all of these out to the Desktop or somewhere safe just in case. Then, launch the Mail app. It will re-import the messages (which might take a long time depending on how many there are – about 20 minutes in my case) and then everything should be back to normal. If that worked, the original index files can be deleted.

Linux: setting permissions for USB serial ports using udev rules

USBIt’s pretty annoying that, by default, USB serial devices come up with somewhat restricted permissions. Sometimes adding the user to the dialout  group works, sometimes it doesn’t. The most reliable way to fix this for all time is to add a udev rule but I can never remember the syntax, hence this post…

Continue reading “Linux: setting permissions for USB serial ports using udev rules”