Raspberry Pi NAS (Samba + FTP+ DLNA)

Connect your drive

The best way to safely and conveniently access your drive from various devices is to create a dedicated user and mount your drive into that user home directory.

Add a user named mediaserver with his home directory and set password:

sudo useradd -m mediaserver
sudo passwd mediaserver

Create ssd directory:

mkdir /home/mediaserver/ssd

List connected drives:

lsblk

You should see something like that:

[~]$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 238.5G  0 disk
├─sda1        8:1    0   500M  0 part
├─sda2        8:2    0 144.9G  0 part
├─sda3        8:3    0     4G  0 part
└─sda4        8:4    0  89.1G  0 part
mmcblk0     179:0    0    29G  0 disk
├─mmcblk0p1 179:1    0   256M  0 part /boot
└─mmcblk0p2 179:2    0  28.7G  0 part /

In my case I want to use partition sda2 as shared drive.

Ensure that the drive is mounted after each boot:

sudo nano /etc/fstab

Add the following line at the end of the file:

/dev/sda2 /home/mediaserver/ssd ntfs-3g defaults,noatime 0 1

Mount drive in mediaserver home dir:

sudo mount /dev/sda2

Samba

Install Samba service if you want to access your data as a network share in Windows.

Install samba, set password for user mediaserver and edit configuration file:

sudo apt update
sudo apt-get install samba samba-common-bin
sudo smbpasswd -a mediaserver
sudo nano /etc/samba/smb.conf

And add the following line at the end of the file:

[global]
allow insecure wide links = yes

[mediaserver]
path=/home/mediaserver/ssd
writeable=Yes
create mask=0777
directory mask=0777
public=no
follow symlinks = yes
wide links = yes

Restart Samba to load the configuration changes:

sudo systemctl restart smbd

FTP server

Install an FTP server if you want to access your drive via FTP, e.g. from a smartphone or the Internet after setting up a firewall.

Install FTP server:

sudo apt update
sudo apt-get install vsftpd

Edit vsftpd conf file:

sudo nano /etc/vsftpd.conf 

With:

write_enable=YES
chroot_local_user=YES
allow_writeable_chroot=YES

# Turn on passive mode!
pasv_enable=Yes
pasv_max_port=10100
pasv_min_port=10090

# Enables only certain users!
userlist_enable=YES
userlist_file=/etc/vsftpd.userlist
userlist_deny=NO

Make user list:

sudo nano /etc/vsftpd.userlist

In the file, enter the list of users who will have access to FTP (EOL separated).

Restart service:

sudo systemctl restart vsftpd.service

Tip: By default, all newly created Linux users have access to their accounts via FTP. You can change this by creating a black/white list.

DLNA server

Install a DLNA server if you want to stream media from your hard drive to devices such as Smart TV.

sudo apt update
sudo apt-get install minidlna

Edit configuration file to set up media directories:

sudo nano /etc/minidlna.conf

With:

media_dir=V,/home/mediaserver/ssd/Movies
media_dir=A,/home/mediaserver/ssd/Music
media_dir=P,/home/mediaserver/ssd/Zdjęcia

Restart service and recreate database:

sudo service minidlna restart
sudo service minidlna force-reload

Most of smart devices will automatically recognize you service. If not, you can access your DLNA server on port 8200 (http://xxx.xxx.xxx.xxx:8200).

If you want to access some of your services from the Internet, you need to have a static IP address and forward the service ports on your router. Remember to configure your firewall and FTP access before!

Bash yes/no prompt function and password prompt with input masking

# Bash Y/n ask function
# Use: 
# if ask "Do you want to continue?"; then
#   echo "Yes"
# else
#   echo "No"
# fi
function ask {  
  read -p "$1 [Y/n] " -n 1 -r
  if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo
    return 0
  elif [[ $REPLY = '' ]]; then
    return 0
  else
    echo
    return 1
  fi
}

Prompt for password with input masking:

# Bash password prompt function with input masking
# Use:
# ask_pass "Enter the password: "
# password="$REPLY"
function ask_pass {
  REPLY=""
  prompt="$1"
  while IFS= read -p "$prompt" -r -s -n 1 char
  do
    if [[ $char == $'\0' ]]; then
      break
    fi
    prompt='*'
    REPLY+="$char"
  done
  echo
}

Zsh + Oh My Zsh + themes + powerlevel10k + plugins / bash replacement

Zsh

Install zsh:

sudo apt-get install zsh

Change the user default shell to zsh:

chsh -s $(which zsh)

Until reboot you can enter the zsh with command zsh.

If you are working in some desktop environment like Gnome you can log out and log in to save the change of default shell.

OMZ

Install Oh My Zsh:

sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

Install the recommended font to show icons in command prompt (or any of recommended fonts):

Download and copy to ~/.local/share/fonts

Code:

mkdir -p ~/.local/share/fonts
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf
wget -P ~/.local/share/fonts/ https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf

Set the font in your terminal emulator settings to make it work.

Now you can play with omz command. Just type omz to get available options.

You can change theme to e.g. omz theme set simple etc.

Powerlevel10k

Optionally you can install one of best custom theme which is Powerlevel10k.

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Set theme:

omz theme set powerlevel10k/powerlevel10k

For the first theme set, the configurator should run itself.

Once you are in powerlevel10k theme you can reconfigure it with p10k configure command.

Plugins

Now you can set plugins. Plugin list with command omz plugin list.

Recommended plugins:

  • git (set by default)
  • autosuggestions (zsh-autosuggestions)
    • install with git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
    • Enable with the command omz plugin enable zsh-autosuggestions

Sources

Better git log

First of all if you don’t have set utf-8 in bash (test with locale command or just try to type utf-8 characters in cli) you need to add export LANG=C.UTF-8 to your ~/.bashrc file:

echo 'export LANG=C.UTF-8' >> ~/.bashrc

Next you can format your command. We will show only commits on master branch with no merges:

git log master --pretty=format:"%C(blue)%cd %C(green)%cn %C(blue)%s %C(yellow)%h" -n10 --no-merges --date=short"

And optionally set alias in your ~/.bashrc:

echo 'alias gl="git log master --pretty=format:\"%C(blue)%cd %C(green)%cn %C(blue)%s %C(yellow)%h\" -n10 --no-merges --date=short"' >> ~/.bashrc

Finally restart bash instance.

Now you can use command gl.

Showing git branch in bash prompt

To achieve something like this:

user@machine:~/myproject (develop)$

In .bashrc file at the end we need to add:

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"
# without host
#PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
# with host
PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Ubuntu & Nvidia drivers

Options

  • GUI
    • Software & Updates
  • CLI
    • removing drivers
    • install certain driver from Ubuntu repo
    • install with ubuntu-drivers
    • install from POP!_OS repo
    • nouveau driver [WIP]

Install with Software & Updates app

Open Software & Updates app -> Additional Drivers tab -> check desired driver -> Apply changes

Removing drivers

sudo apt-get remove --purge '^nvidia-.*'

Install from Ubuntu repo

Search for possible drivers:

apt-cache search nvidia-driver-*

Install:

apt-get install nvidia-driver-510

From ubuntu-drivers

sudo ubuntu-drivers autoinstall

Install from POP!_OS repo

If you are running Ubuntu 19.10 or later – add the Apt Preferences File:

sudo nano /etc/apt/preferences.d/system76-apt-preferences

Add the following six lines (seven if you count the space in the middle):

Package: *
Pin: release o=LP-PPA-system76-dev-stable
Pin-Priority: 1001

Package: *
Pin: release o=LP-PPA-system76-dev-pre-stable
Pin-Priority: 1001

Save the file. Now you should be able to install the System76 Driver as described below.

Installing the Driver
To install our Driver you need to run the following commands in the Terminal:

sudo apt-add-repository -y ppa:system76-dev/stable
sudo apt-get update
sudo apt install system76-driver-nvidia

nouveau driver [WIP]

NGINX – restricting Access with .htpasswd

Install apache2-utils (Debian, Ubuntu) or httpd-tools (RHEL/CentOS/Oracle Linux):

apt install apache2-utils

Create password file:

htpasswd -c /etc/apache2/.htpasswd user1

You can confirm that the file contains paired usernames and hashed passwords:

cat /etc/apache2/.htpasswd

In vhost configuration add condition:

location /my-restricted-directory {
    auth_basic           “Administrator’s Area”;
    auth_basic_user_file /etc/apache2/.htpasswd; 
}

Validate nginx conf:

nginx -t

Reload nginx:

service nginx reload

Done.