Raspberry Pi: Difference between revisions

From WickyWiki
mNo edit summary
Line 3: Line 3:
[[Category:201712]]
[[Category:201712]]


= Configure =
= See also =
 
* [[Raspberry Pi LAMP stack]]
* [[MediaWiki]]
* [[Raspberry Pi SOGo Groupware]]
* [[Raspberry Pi GPIO Shutdown Button]]
* [[Raspberry Pi system clone and/or resize]]
* [[Raspberry Pi monitor]]
* [[Raspberry Pi - Pi-Hole Network-wide ad blocking]]
* [[MySQL#Database_MariaDB_access_from_other_machine|MySQL remote]]
 
= Install =


Here some pointers on configuring your device.
This guide assumes a headless installation.


== Configure tool ==
== Download and burn ==


<syntaxhighlight lang=bash>
Download Raspberry:
sudo raspi-config
* https://www.raspberrypi.org/software/operating-systems/
</syntaxhighlight>
 
It is the same image for all models. Pick "Raspberry Pi OS Lite" if you are going for a minimal footprint.


== Bash shell timout ==
Burn the image to an 4/8/16 Gb micro sd memory card from your PC. Make sure it is a supported card.


Automatic logout of a user bash session after a period of inactivity.
Windows burner software (without install)
* https://www.balena.io/etcher/


<syntaxhighlight lang=bash>
Linux software:
sudo nano /etc/profile
* User software installer ans install RaspberryPi Imager
</syntaxhighlight>


<blockquote>
Re-insert the sdcard after burning the image.
<syntaxhighlight lang=bash>
...
# set a 25 min (1500 sec) timeout policy for bash shell
TMOUT=1500
readonly TMOUT
export TMOUT
...
</syntaxhighlight>
</blockquote>


== HDMI mode in config.txt ==
== Enable ssh and wifi ==


At boot the file 'config.txt' is used to apply several non-default settings. This file is on your FAT boot partition which is usually available as /boot after the Pi is up and running.
Create an empty textfile 'ssh' in the FAT32 boot partition of the boot partition to enable ssh after booting.


More info on config.txt:
Create file 'wpa_supplicant.conf' in the FAt32 boot partition of the boot partition with the correct ssid name and password (both are case sensitive).
* https://www.raspberrypi.org/documentation/configuration/config-txt/README.md


If you don't provide specific settings for 'hdmi_group' and 'hdmi_mode' a mode will be negotiated with the de HDMI display device. If this doesn't work out for you, you can change config.txt to get what you want, here are some examples:
  country=US
  ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
  update_config=1
  network={
    ssid="WeeFee2g"
    psk="NETWORK-PASSWORD"
  }


<syntaxhighlight lang=bash>
==  Boot your Raspberry Pi ==
sudo nano /boot/config.txt
</syntaxhighlight>


<blockquote>
Boot your Raspbarry Pi, connect with ssh for further configuration. Usually there is some troubleshooting involved so don't give up.
<syntaxhighlight lang=bash>
...
#VGA
hdmi_group=1
hdmi_mode=1


#1024x768
The default user is 'pi' and password is 'raspberry'. Connect using a terminal or Putty (on windows).
hdmi_group=2
hdmi_mode=16


#720p 60Hz
<source lang=bash>
hdmi_group=1
ssh pi@192.168.1.2
hdmi_mode=4
</source>


#1080p 60Hz
== Basic configuration ==
hdmi_group=1
hdmi_mode=16
...
</syntaxhighlight>
</blockquote>


More info on HDMI:
Set hostname, change password, expand filesystem:
* https://www.raspberrypi.org/documentation/configuration/config-txt/video.md


= SSH Remote control =
<source lang=bash>
sudo raspi-config
system > change hostname
system > change password (idem-pihole-pw)
advanced > Expand filesystem
</source>


You can use remote shell to control your Raspberry Pi without directly connected mouse, keyboard and screen.
Install updates, reboot:


<syntaxhighlight lang=bash>
<source lang=bash>
ssh pi@192.168.1.2
sudo apt-get update -y
</syntaxhighlight>
sudo apt-get upgrade -y
sudo reboot
</source>


== Ssh copy local file to remote machine ==
= Minimize and optimize =


<syntaxhighlight lang=bash>
To make it smaller and faster. You can always add then again if you change your mind.
scp /path/to/local/file.zip pi@192.168.1.2:/home/pi/Downloads/file.zip
</syntaxhighlight>


And back:
Source:
* https://silocitylabs.com/post/2019/06/13/ultra-minimal-raspbian-image-for-pi-zero-and-zero-w/


<syntaxhighlight lang=bash>
List used space per package:
scp pi@192.168.1.2:/home/pi/Downloads/file.zip /path/to/local/file.zip
</syntaxhighlight>


Nautilus ssh access, in the address-bar type:
<source lang=bash>
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -nr | more
</source>


<blockquote>
Check running processes:
<nowiki>
ssh://pi@192.168.1.2
</nowiki>
</blockquote>


Ssh root access is not allowed by default but could be convenient for use with Nautilus
<source lang=bash>
Note: better set this back to 'no' when you are done
sudo htop
</source>


<syntaxhighlight lang=bash>
== Replace Openssh with dropbear ==
sudo nano /etc/ssh/sshd_config
</syntaxhighlight>


<blockquote>
<source lang=bash>
<syntaxhighlight lang=bash>
#install dropbear
PermitRootLogin yes
sudo apt install dropbear
</syntaxhighlight>
sudo nano /etc/default/dropbear
</blockquote>
NO_START=0
DROPBEAR_PORT=22
#remove Openssh
apt-get purge --yes --auto-remove openssh-server


You can now access the machine as root with Nautilus, in the address-bar type:
#add sftp server
apt install openssh-sftp-server


<blockquote>
#test
<nowiki>
sudo reboot
ssh://root@192.168.1.2/
ssh pi@192.168.1.2
</nowiki>
sftp://192.168.1.2/home/pi
</blockquote>
</source>


== Allow ssh scripting without a password request, based on keys ==
== Remove packages ==


You want the script to execute without it stopping to ask for a password.
<source lang=bash>
sudo apt-get purge --yes --auto-remove
  aptitude aptitude-common apt-listchanges apt-utils blends-tasks xserver-common x11-xfs-utils
  x11-xserver-utils xinit libsmbclient blt gvfs gvfs-backends gvfs-daemons gvfs-fuse idle
  idle-python2.7 idle3 libaudio2 libice6 liblightdm-gobject-1-0 libpulse0 libqt4-svg libqtgui4
  libsdl-image1.2 libsdl-mixer1.2 libsdl-ttf2.0-0 libsdl1.2debian libsm6 libsmpeg0
  libwebkitgtk-1.0-0 libwebkitgtk-3.0-0 libxaw7 libxklavier16 libxmu6 libxss1 libxt6 libxtst6
  lightdm lightdm-gtk-greeter lxde lxde-core midori obconf openbox python-pygame python-tk python3-tk
  scratch tk8.5 wpagui x11-common x11-utils x11-xkb-utils xinit xserver-common xserver-xorg
  xserver-xorg-core xserver-xorg-input-all xserver-xorg-input-evdev xserver-xorg-input-synaptics
  xserver-xorg-video-fbdev zenity xserver* x11-common x11-utils x11-xkb-utils x11-xserver-utils
  xarchiver xauth xkb-data console-setup xinit lightdm
  libx{composite,cb,cursor,damage,dmcp,ext,font,ft,i,inerama,kbfile,klavier,mu,pm,randr,render,res,t,xf86}*
  lxde* lx{input,menu-data,panel,polkit,randr,session,session-edit,shortcut,task,terminal}
  obconf openbox gtk* libgtk* alsa*  python-pygame python-tk python3-tk scratch tsconf
  desktop-file-utils cifs-utils samba-common smbclient scratch debian-reference-en dillo idle3
  python3-tk idle python-pygame python-tk lightdm gnome-themes-standard gnome-icon-theme
  raspberrypi-artwork gvfs-backends gvfs-fuse desktop-base lxpolkit zenity xdg-utils mupdf
  gtk2-engines alsa-utils lxde lxtask menu-xdg gksu midori xserver-xorg xinit
  xserver-xorg-video-fbdev libraspberrypi-dev libraspberrypi-doc dbus-x11 libx11-6 libx11-data
  libx11-xcb1 x11-common x11-utils lxde-icon-theme gconf-service gconf2-common udhcpd avahi-daemon
  gcc-arm-linux-gnueabihf geoip-database libgdbm* libperl* make patch perl perl-modules-* cups*
  rsync bluez bluez-firmware pi-bluetooth scratch debian-reference-en dillo idle3 python3-tk idle
  python-pygame python-tk lightdm gnome-themes-standard gnome-icon-theme raspberrypi-artwork
  gvfs-backends gvfs-fuse desktop-base lxpolkit zenity xdg-utils mupdf gtk2-engines alsa-utils
  lxde lxtask menu-xdg gksu midori xserver-xorg xinit xserver-xorg-video-fbdev libraspberrypi-dev
  libraspberrypi-doc dbus-x11 libx11-6 libx11-data libx11-xcb1 x11-common x11-utils
  lxde-icon-theme gconf-service gconf2-common firmware-atheros samba-common plymouth


* http://www.linuxproblem.org/art_9.html
sudo apt-get autoremove --purge
</source>


Root session:
Remove files


<syntaxhighlight lang=bash>
<source lang=bash>
sudo -i
sudo -i
</syntaxhighlight>
find /usr/share/doc -depth -type f ! -name copyright | xargs rm
find /usr/share/doc -depth -type f ! -name copyright | xargs rm -f
find /usr/share/doc -empty | xargs rmdir
rm -rf /usr/share/man/* /usr/share/groff/* /usr/share/info/*
rm -rf /usr/share/lintian/* /usr/share/linda/* /var/cache/man/*
rm -rf /usr/share/locale/*
rm -f /var/log/{auth,boot,bootstrap,daemon,kern}.log
rm -f /var/log/{debug,dmesg,messages,syslog}
#logout root
exit
</source>


Generate keys, accept defaults:
Other settings:


<syntaxhighlight lang=bash>
<source lang=bash>
ssh-keygen -t rsa
#disable MOTD for faster ssh connect
</syntaxhighlight>
sudo cat /dev/null > /etc/motd


Create dir on remote:
#disable HDMI, add line of text
sudo nano /etc/rc.local


<syntaxhighlight lang=bash>
  /usr/bin/tvservice -o
ssh pi@192.168.1.2 mkdir -p .ssh
</syntaxhighlight>


Add pub key to pi@192.168.1.2 authorized_keys:
#modify /boot/config.txt
 
sudo nano /boot/config.txt
<syntaxhighlight lang=bash>
cat .ssh/id_rsa.pub | ssh pi@192.168.1.2 'cat >> .ssh/authorized_keys'
</syntaxhighlight>


Should work now without password:
# disable stuff
dtparam=audio=off
dtoverlay=disable-bt


<syntaxhighlight lang=bash>
# Red PWR LED off
ssh pi@192.168.1.2
dtparam=pwr_led_trigger=none
</syntaxhighlight>
dtparam=pwr_led_activelow=off


Maybe needed, for some versions of ssh
# Green ACT LED
#dtparam=act_led_trigger=none
#dtparam=act_led_activelow=off


<syntaxhighlight lang=bash>
# disable the splash screen
cat .ssh/id_rsa.pub | ssh pi@192.168.1.2 'cat >> .ssh/authorized_keys2'
disable_splash=1
ssh pi@192.168.1.2 chmod 700 .ssh
ssh pi@192.168.1.2 chmod 640 .ssh/authorized_keys2
# Set the bootloader delay to 0 seconds, the default is 1
</syntaxhighlight>
boot_delay=0
</source>


== Nautilus file manager, manage files via ssh  ==
Reboot:


Type as location:
<source lang=bash>
sudo reboot
</source>


ssh://192.168.1.2
== Disable some services ==


Enter credentials (user pi and password), you can choose to remember these and you can also store this location as a bookmark in Nautilus for your convenience.
<source lang=bash>
sudo -i


== Require pi to enter password when sudo ==
#disable swap
sudo dphys-swapfile swapoff
systemctl disable dphys-swapfile.service
#other stuff:
systemctl disable apt-daily.service
systemctl disable apt-daily.timer
systemctl disable nfs-client.target
systemctl disable remote-fs.target
systemctl disable apt-daily-upgrade.timer
systemctl disable nfs-config.service


By default the Raspbian pi user can sudo without the need to re-enter the password. If you don't like that make this change.
exit
</source>


* https://raspberrypi.stackexchange.com/questions/57887/disable-sudo-for-user-pi-or-require-root-password
== Bash shell timout ==


<syntaxhighlight lang=bash>
Automatic logout of a user bash session after a period of inactivity.
ssh pi@192.168.1.2
sudo nano /etc/sudoers.d/010_pi-nopasswd
</syntaxhighlight>


Change it from:
<source lang=bash>
sudo nano /etc/profile
</source>


<blockquote>
<blockquote>
<syntaxhighlight lang=bash>
<source lang=bash>
pi ALL=(ALL) NOPASSWD: ALL
...
</syntaxhighlight>
# set a 25 min (1500 sec) timeout policy for bash shell
TMOUT=1500
readonly TMOUT
export TMOUT
...
</source>
</blockquote>
</blockquote>


to


<blockquote>
<syntaxhighlight lang=bash>
pi ALL=(ALL) ALL
</syntaxhighlight>
</blockquote>


== VNC ==
= Access with ssh and sftp examples =
 
You can use remote shell to control your Raspberry Pi without directly connected mouse, keyboard and screen.
 
Logon with ssh:


For mouse and screen people.
<source lang=bash>
* https://www.raspberrypi.org/documentation/remote-access/vnc/
ssh pi@192.168.1.2
</source>


Start the desktop:
Sftp copy local file to remote machine:


<syntaxhighlight lang=bash>
<source lang=bash>
startx
scp /path/to/local/file.zip pi@192.168.1.2:/home/pi/Downloads/file.zip
</syntaxhighlight>
</source>


Screen resolution
And back:


The resolution is negotiated with the monitor, if none is available it defaults to 720x480. To change, this start config tool:
<source lang=bash>
scp pi@192.168.1.2:/home/pi/Downloads/file.zip /path/to/local/file.zip
</source>


<syntaxhighlight lang=bash>
== Allow ssh scripting without a password ==
sudo raspi-config
</syntaxhighlight>


Go to "Advanced" and "Resolution" and pick the right value for you. Restart the machine when prompted to apply the changes.
You want a script to execute without it stopping to ask for a password.


== Database MySql /MariaDB remote access ==
* http://www.linuxproblem.org/art_9.html


See [[MySQL#Database_MariaDB_access_from_other_machine|MySQL remote]]
Root session:


= Performance tuning =
<source lang=bash>
sudo -i
</source>


== Memory ==
Generate keys, accept defaults:


=== GPU memory ===
<source lang=bash>
ssh-keygen -t rsa
</source>


* https://serverfault.com/questions/201075/what-tips-do-you-have-for-speeding-up-performance-of-a-mediawiki-installation
Create dir on remote:


When you are not really using the video capabilities of the Raspberry Pi, the easiest way to obtain some extra memory is to lower the GPU memory, for example to 32 MB. Start config tool:
<source lang=bash>
ssh pi@192.168.1.2 mkdir -p .ssh
</source>


<syntaxhighlight lang=bash>
Add pub key to pi@192.168.1.2 authorized_keys:
sudo raspi-config
</syntaxhighlight>


Then go to 'Advanced' > 'Memory Split' and modify the value
<source lang=bash>
cat .ssh/id_rsa.pub | ssh pi@192.168.1.2 'cat >> .ssh/authorized_keys'
</source>


=== Overview of processes, CPU and memory usage: ===
Should work now without password:


<syntaxhighlight lang=bash>
<source lang=bash>
sudo htop
ssh pi@192.168.1.2
</syntaxhighlight>
</source>


=== Disable swap ===
Maybe needed, for some versions of ssh


If you have enough free memory, maybe want to reduce writes to the sdcard you can disable the on-disk swap file:
<source lang=bash>
cat .ssh/id_rsa.pub | ssh pi@192.168.1.2 'cat >> .ssh/authorized_keys2'
ssh pi@192.168.1.2 chmod 700 .ssh
ssh pi@192.168.1.2 chmod 640 .ssh/authorized_keys2
</source>


<syntaxhighlight lang=bash>
== Nautilus file manager, manage files via ssh  ==
sudo systemctl stop dphys-swapfile
sudo systemctl disable dphys-swapfile
</syntaxhighlight>


Other ways:
Type as location:


<syntaxhighlight lang=bash>
ssh://192.168.1.2
sudo dphys-swapfile swapoff
</syntaxhighlight>


== Run headless (without desktop) by default ==
Enter credentials (user pi and password), you can choose to remember these and you can also store this location as a bookmark in Nautilus for your convenience.


Start config tool:
Ssh root access is not allowed by default but could be convenient for use with Nautilus
<syntaxhighlight lang=bash>
Note: better set this back to 'no' when you are done
sudo raspi-config
</syntaxhighlight>


Configure these options:
<source lang=bash>
# Boot options - Desktop: select text console login
sudo nano /etc/ssh/sshd_config
# Interfacing - VNC server: enable
</source>


If you now want to start the desktop:
# Set up a VNC client session to the machine
# Login via the text console
# Start the desktop:
<blockquote>
<blockquote>
<syntaxhighlight lang=bash>
<source lang=bash>
startx
PermitRootLogin yes
</syntaxhighlight>
</source>
</blockquote>
</blockquote>


== Bring Wifi down if Ethernet cable is connected ==
You can now access the machine as root with Nautilus.
 
== Require pi to enter password when sudo ==
 
By default the Raspbian pi user can sudo without the need to re-enter the password. If you don't like that make this change.
 
* https://raspberrypi.stackexchange.com/questions/57887/disable-sudo-for-user-pi-or-require-root-password


Create file:
<source lang=bash>
ssh pi@192.168.1.2
sudo nano /etc/sudoers.d/010_pi-nopasswd
</source>


<syntaxhighlight lang=bash>
Change it from:
nano /home/pi/Scripts/wlan0downifeth0.sh
</syntaxhighlight>


<blockquote>
<blockquote>
<syntaxhighlight lang=bash>
<source lang=bash>
#!/bin/bash
pi ALL=(ALL) NOPASSWD: ALL
if [[ -n $( ip a show eth0 up ) ]] ; then
</source>
  echo "wlan0 down"
  ifconfig wlan0 down
fi
</syntaxhighlight>
</blockquote>
</blockquote>


Make executable:
to


<syntaxhighlight lang=bash>
<blockquote>
chmod +x /home/pi/Scripts/wlan0downifeth0.sh
<source lang=bash>
</syntaxhighlight>
pi ALL=(ALL) ALL
</source>
</blockquote>


Modify crontab, check every hour:
= None-headless access =


<syntaxhighlight lang=bash>
== HDMI mode in config.txt ==
sudo nano /etc/crontab
</syntaxhighlight>


<blockquote>
At boot the file 'config.txt' is used to apply several non-default settings. This file is on your FAT boot partition which is usually available as /boot after the Pi is up and running.
<syntaxhighlight lang=bash>
#every hh:45 if eth0 up then wlan0 down
45 * * * * root /home/pi/Scripts/wlan0downifeth0.sh
</syntaxhighlight>
</blockquote>


To bring wifi up:
More info on config.txt:
* https://www.raspberrypi.org/documentation/configuration/config-txt/README.md


<syntaxhighlight lang=bash>
If you don't provide specific settings for 'hdmi_group' and 'hdmi_mode' a mode will be negotiated with the de HDMI display device. If this doesn't work out for you, you can change config.txt to get what you want, here are some examples:
sudo ifconfig wlan0 up
</syntaxhighlight>


To check the wifi status (returns nothing if down):
<source lang=bash>
sudo nano /boot/config.txt
</source>


<syntaxhighlight lang=bash>
<blockquote>
ip a show wlan0 up
<source lang=bash>
</syntaxhighlight>
...
#VGA
hdmi_group=1
hdmi_mode=1


== Remove software packages ==
#1024x768
hdmi_group=2
hdmi_mode=16


Slim down Raspberry Pi default installation by removing some packages
#720p 60Hz
hdmi_group=1
hdmi_mode=4


* https://blog.samat.org/2015/02/05/slimming-an-existing-raspbian-install/
#1080p 60Hz
hdmi_group=1
hdmi_mode=16
...
</source>
</blockquote>


Remove packages with:
More info on HDMI:
* https://www.raspberrypi.org/documentation/configuration/config-txt/video.md


<syntaxhighlight lang=bash>
== VNC ==
apt-get -y remove --purge <package1> <package2>
</syntaxhighlight>


Education related packages:
For mouse and screen people.
* https://www.raspberrypi.org/documentation/remote-access/vnc/


<syntaxhighlight lang=bash>
Start the desktop:
idle idle3
python3-pygame python-pygame python-tk python3-tk python-serial python3-serial python-picamera
python3-picamera python3-pygame python-pygame python-tk python3-tk python3-numpy python3-pifacecommon
python3-pifacedigitalio python3-pifacedigital-scratch-handler python-debian-reference-en python-pifacedigitalio
dillo x2x scratch nuscratch timidity smartsim pistore sonic-pi pifacecommon oracle-java8-jdk wolfram-engine
minecraft-pi python-minecraftpi penguinspuzzle
</syntaxhighlight>


Remove other packages:
<source lang=bash>
<syntaxhighlight lang=bash>
startx
sudo apt-get purge libreoffice*
</source>
sudo apt-get purge chromium-browser
sudo apt-get purge cups-common
sudo apt-get purge xpdf gcalculator
#replacment browser (if you want)
sudo apt-get install midori
</syntaxhighlight>


Remove unreferenced packages:
Screen resolution
<syntaxhighlight lang=bash>
apt-get -y autoremove
</syntaxhighlight>


Remove all packages marked 'rc':
The resolution is negotiated with the monitor, if none is available it defaults to 720x480. To change, this start config tool:
<syntaxhighlight lang=bash>
dpkg --list |grep "^rc" | cut -d " " -f 3 | xargs dpkg --purge
</syntaxhighlight>


= See also =
<source lang=bash>
sudo raspi-config
</source>


* [[Raspberry Pi LAMP stack]]
Go to "Advanced" and "Resolution" and pick the right value for you. Restart the machine when prompted to apply the changes.
* [[MediaWiki]]
* [[Raspberry Pi SOGo Groupware]]
* [[Raspberry Pi GPIO Shutdown Button]]
* [[Raspberry Pi system clone and/or resize]]
* [[Raspberry Pi monitor]]
* [[Raspberry Pi - Pi-Hole Network-wide ad blocking]]

Revision as of 12:08, 11 April 2021


See also

Install

This guide assumes a headless installation.

Download and burn

Download Raspberry:

It is the same image for all models. Pick "Raspberry Pi OS Lite" if you are going for a minimal footprint.

Burn the image to an 4/8/16 Gb micro sd memory card from your PC. Make sure it is a supported card.

Windows burner software (without install)

Linux software:

  • User software installer ans install RaspberryPi Imager

Re-insert the sdcard after burning the image.

Enable ssh and wifi

Create an empty textfile 'ssh' in the FAT32 boot partition of the boot partition to enable ssh after booting.

Create file 'wpa_supplicant.conf' in the FAt32 boot partition of the boot partition with the correct ssid name and password (both are case sensitive).

 country=US
 ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
 update_config=1
 network={
   ssid="WeeFee2g"
   psk="NETWORK-PASSWORD"
 }

Boot your Raspberry Pi

Boot your Raspbarry Pi, connect with ssh for further configuration. Usually there is some troubleshooting involved so don't give up.

The default user is 'pi' and password is 'raspberry'. Connect using a terminal or Putty (on windows).

ssh pi@192.168.1.2

Basic configuration

Set hostname, change password, expand filesystem:

sudo raspi-config
system > change hostname 
system > change password (idem-pihole-pw)
advanced > Expand filesystem

Install updates, reboot:

sudo apt-get update -y
sudo apt-get upgrade -y
sudo reboot

Minimize and optimize

To make it smaller and faster. You can always add then again if you change your mind.

Source:

List used space per package:

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -nr | more

Check running processes:

sudo htop

Replace Openssh with dropbear

#install dropbear
sudo apt install dropbear
sudo nano /etc/default/dropbear
	NO_START=0
	DROPBEAR_PORT=22
#remove Openssh
apt-get purge --yes --auto-remove openssh-server

#add sftp server
apt install openssh-sftp-server

#test
sudo reboot
ssh pi@192.168.1.2
sftp://192.168.1.2/home/pi

Remove packages

sudo apt-get purge --yes --auto-remove 
  aptitude aptitude-common apt-listchanges apt-utils blends-tasks xserver-common x11-xfs-utils 
  x11-xserver-utils xinit libsmbclient blt gvfs gvfs-backends gvfs-daemons gvfs-fuse idle 
  idle-python2.7 idle3 libaudio2 libice6 liblightdm-gobject-1-0 libpulse0 libqt4-svg libqtgui4 
  libsdl-image1.2 libsdl-mixer1.2 libsdl-ttf2.0-0 libsdl1.2debian libsm6 libsmpeg0 
  libwebkitgtk-1.0-0 libwebkitgtk-3.0-0 libxaw7 libxklavier16 libxmu6 libxss1 libxt6 libxtst6 
  lightdm lightdm-gtk-greeter lxde lxde-core midori obconf openbox python-pygame python-tk python3-tk 
  scratch tk8.5 wpagui x11-common x11-utils x11-xkb-utils xinit xserver-common xserver-xorg 
  xserver-xorg-core xserver-xorg-input-all xserver-xorg-input-evdev xserver-xorg-input-synaptics 
  xserver-xorg-video-fbdev zenity xserver* x11-common x11-utils x11-xkb-utils x11-xserver-utils 
  xarchiver xauth xkb-data console-setup xinit lightdm 
  libx{composite,cb,cursor,damage,dmcp,ext,font,ft,i,inerama,kbfile,klavier,mu,pm,randr,render,res,t,xf86}* 
  lxde* lx{input,menu-data,panel,polkit,randr,session,session-edit,shortcut,task,terminal} 
  obconf openbox gtk* libgtk* alsa*  python-pygame python-tk python3-tk scratch tsconf 
  desktop-file-utils cifs-utils samba-common smbclient scratch debian-reference-en dillo idle3 
  python3-tk idle python-pygame python-tk lightdm gnome-themes-standard gnome-icon-theme 
  raspberrypi-artwork gvfs-backends gvfs-fuse desktop-base lxpolkit zenity xdg-utils mupdf 
  gtk2-engines alsa-utils lxde lxtask menu-xdg gksu midori xserver-xorg xinit 
  xserver-xorg-video-fbdev libraspberrypi-dev libraspberrypi-doc dbus-x11 libx11-6 libx11-data 
  libx11-xcb1 x11-common x11-utils lxde-icon-theme gconf-service gconf2-common udhcpd avahi-daemon 
  gcc-arm-linux-gnueabihf geoip-database libgdbm* libperl* make patch perl perl-modules-* cups* 
  rsync bluez bluez-firmware pi-bluetooth scratch debian-reference-en dillo idle3 python3-tk idle 
  python-pygame python-tk lightdm gnome-themes-standard gnome-icon-theme raspberrypi-artwork 
  gvfs-backends gvfs-fuse desktop-base lxpolkit zenity xdg-utils mupdf gtk2-engines alsa-utils 
  lxde lxtask menu-xdg gksu midori xserver-xorg xinit xserver-xorg-video-fbdev libraspberrypi-dev 
  libraspberrypi-doc dbus-x11 libx11-6 libx11-data libx11-xcb1 x11-common x11-utils 
  lxde-icon-theme gconf-service gconf2-common firmware-atheros samba-common plymouth

sudo apt-get autoremove --purge

Remove files

sudo -i
find /usr/share/doc -depth -type f ! -name copyright | xargs rm
find /usr/share/doc -depth -type f ! -name copyright | xargs rm -f
find /usr/share/doc -empty | xargs rmdir
rm -rf /usr/share/man/* /usr/share/groff/* /usr/share/info/*
rm -rf /usr/share/lintian/* /usr/share/linda/* /var/cache/man/*
rm -rf /usr/share/locale/*
rm -f /var/log/{auth,boot,bootstrap,daemon,kern}.log
rm -f /var/log/{debug,dmesg,messages,syslog}
#logout root
exit

Other settings:

#disable MOTD for faster ssh connect
sudo cat /dev/null > /etc/motd

#disable HDMI, add line of text
sudo nano /etc/rc.local

  /usr/bin/tvservice -o

#modify /boot/config.txt
sudo nano /boot/config.txt

	# disable stuff
	dtparam=audio=off
	dtoverlay=disable-bt

	# Red PWR LED off
	dtparam=pwr_led_trigger=none
	dtparam=pwr_led_activelow=off

	# Green ACT LED
	#dtparam=act_led_trigger=none
	#dtparam=act_led_activelow=off

	# disable the splash screen
	disable_splash=1
	 
	# Set the bootloader delay to 0 seconds, the default is 1
	boot_delay=0

Reboot:

sudo reboot

Disable some services

sudo -i

#disable swap
sudo dphys-swapfile swapoff
systemctl disable dphys-swapfile.service
#other stuff:
systemctl disable apt-daily.service
systemctl disable apt-daily.timer
systemctl disable nfs-client.target
systemctl disable remote-fs.target
systemctl disable apt-daily-upgrade.timer
systemctl disable nfs-config.service

exit

Bash shell timout

Automatic logout of a user bash session after a period of inactivity.

sudo nano /etc/profile
...
# set a 25 min (1500 sec) timeout policy for bash shell 
TMOUT=1500
readonly TMOUT
export TMOUT
...


Access with ssh and sftp examples

You can use remote shell to control your Raspberry Pi without directly connected mouse, keyboard and screen.

Logon with ssh:

ssh pi@192.168.1.2

Sftp copy local file to remote machine:

scp /path/to/local/file.zip pi@192.168.1.2:/home/pi/Downloads/file.zip

And back:

scp pi@192.168.1.2:/home/pi/Downloads/file.zip /path/to/local/file.zip

Allow ssh scripting without a password

You want a script to execute without it stopping to ask for a password.

Root session:

sudo -i

Generate keys, accept defaults:

ssh-keygen -t rsa

Create dir on remote:

ssh pi@192.168.1.2 mkdir -p .ssh

Add pub key to pi@192.168.1.2 authorized_keys:

cat .ssh/id_rsa.pub | ssh pi@192.168.1.2 'cat >> .ssh/authorized_keys'

Should work now without password:

ssh pi@192.168.1.2

Maybe needed, for some versions of ssh

cat .ssh/id_rsa.pub | ssh pi@192.168.1.2 'cat >> .ssh/authorized_keys2'
ssh pi@192.168.1.2 chmod 700 .ssh
ssh pi@192.168.1.2 chmod 640 .ssh/authorized_keys2

Nautilus file manager, manage files via ssh

Type as location:

ssh://192.168.1.2

Enter credentials (user pi and password), you can choose to remember these and you can also store this location as a bookmark in Nautilus for your convenience.

Ssh root access is not allowed by default but could be convenient for use with Nautilus Note: better set this back to 'no' when you are done

sudo nano /etc/ssh/sshd_config
PermitRootLogin yes

You can now access the machine as root with Nautilus.

Require pi to enter password when sudo

By default the Raspbian pi user can sudo without the need to re-enter the password. If you don't like that make this change.

ssh pi@192.168.1.2
sudo nano /etc/sudoers.d/010_pi-nopasswd

Change it from:

pi ALL=(ALL) NOPASSWD: ALL

to

pi ALL=(ALL) ALL

None-headless access

HDMI mode in config.txt

At boot the file 'config.txt' is used to apply several non-default settings. This file is on your FAT boot partition which is usually available as /boot after the Pi is up and running.

More info on config.txt:

If you don't provide specific settings for 'hdmi_group' and 'hdmi_mode' a mode will be negotiated with the de HDMI display device. If this doesn't work out for you, you can change config.txt to get what you want, here are some examples:

sudo nano /boot/config.txt
...
#VGA
hdmi_group=1
hdmi_mode=1

#1024x768
hdmi_group=2
hdmi_mode=16

#720p 60Hz 	
hdmi_group=1
hdmi_mode=4

#1080p 60Hz
hdmi_group=1
hdmi_mode=16
...

More info on HDMI:

VNC

For mouse and screen people.

Start the desktop:

startx

Screen resolution

The resolution is negotiated with the monitor, if none is available it defaults to 720x480. To change, this start config tool:

sudo raspi-config

Go to "Advanced" and "Resolution" and pick the right value for you. Restart the machine when prompted to apply the changes.