====== Raspberry Pi optimization and tuning ====== **Related docs:** * https://extremeshok.com/1081/raspberry-pi-raspbian-tuning-optimising-optimizing-for-reduced-memory-usage/ ===== RPi Defaults ===== RaspberryPi running Raspbian \\ \\ username: **pi** \\ password: **raspberry** \\ ===== Completed and optional server preparation ===== ==== Optional: Enable ZRAM ( RAM Compression ) ==== ==== Replacing OpenSSH with Dropbear | Save: +10MB RAM ==== This will reduce the memory consumption by 10MB, however you will be sacrificing: ssh portforwarding and ssh logging. apt-get install dropbear openssh-client Stop OpenSSH server you will not loose your current SSH connection, if you are connected via ssh /etc/init.d/ssh stop Enable dropbear to start at boot-time sed -i 's/NO_START=1/NO_START=0/g' /etc/default/dropbear optional: prevent root logins sed -i 's/DROPBEAR_EXTRA_ARGS=/DROPBEAR_EXTRA_ARGS="-w"/g' /etc/default/dropbear optional: prevent password logins sed -i 's/DROPBEAR_EXTRA_ARGS=/DROPBEAR_EXTRA_ARGS="-s"/g' /etc/default/dropbear optional: prevent password logins for root sed -i 's/DROPBEAR_EXTRA_ARGS=/DROPBEAR_EXTRA_ARGS="-g"/g' /etc/default/dropbear optional: prevent root logins and prevent password logins sed -i 's/DROPBEAR_EXTRA_ARGS=/DROPBEAR_EXTRA_ARGS="-w -s"/g' /etc/default/dropbear optional: change the listening port from 22 to 2222 sed -i 's/DROPBEAR_PORT=22/DROPBEAR_PORT=2222/g' /etc/default/dropbear Start Dropbear /etc/init.d/dropbear start **Test** Make sure you can connect to the server over ssh (using an ssh client). Disable OpenSSH, but allow it to be used for SSH / SFTP

Remove the extra tty / getty's | Save: +3.5 MB RAM

tty2-tty6 will be disabled. We are keeping tty1 for console, unless you choose to disable it.
sed -i '/[2-6]:23:respawn:\/sbin\/getty 38400 tty[2-6]/s%^%#%g' /etc/inittab
1
2
3
	

Remove the extra tty / getty's | Save: +3.5 MB RAM

tty2-tty6 will be disabled. We are keeping tty1 for console, unless you choose to disable it.
sed -i '/[2-6]:23:respawn:\/sbin\/getty 38400 tty[2-6]/s%^%#%g' /etc/inittab

optional: disable getty on the Raspberry Pi serial line
Shell
sed -i '/T0:23:respawn:\/sbin\/getty -L ttyAMA0 115200 vt100/s%^%#%g' /etc/inittab
1
	
sed -i '/T0:23:respawn:\/sbin\/getty -L ttyAMA0 115200 vt100/s%^%#%g' /etc/inittab
Replace Bash shell with Dash shell | Save: +1 MB RAM

Replacing Bash with Dash will increase the system’s overall performance ie. speed up the system boot, reduce disk space, use fewer libraries (save memory) and is more reliable:
Dash is an acronym for Debian Almquist shell (dash). It is a Unix and Linux shell which is much smaller than bash but still aiming at POSIX-compliancy. dash is a POSIX-compliant implementation of /bin/sh that aims to be as small as possible. dash is a direct descendant of the NetBSD version of ash (the Almquist SHell), ported to Linux in early 1997. It was renamed to dash in 2002.
Shell
dpkg-reconfigure dash
1
	
dpkg-reconfigure dash

    Use dash as the default system shell (/bin/sh)? Yes

Enable a 512MB swapfile

Customize the size of the swap file, 512MB recommended.
Shell
echo "CONF_SWAPSIZE=512" > /etc/dphys-swapfile
1
	
echo "CONF_SWAPSIZE=512" > /etc/dphys-swapfile

Initialize the swapfile
Shell
dphys-swapfile setup
1
	
dphys-swapfile setup

Start/enable the swapfile
Shell
dphys-swapfile swapon
1
	
dphys-swapfile swapon
Enable better usage of the swap

Default swappiness is 1, we will change this value to 10, which will allow for better memory usage at the expense of more swap usage. note: this could reduce the life of your sdcard.
Shell
sed -i 's/vm.swappiness=1/vm.swappiness=10/g' /etc/sysctl.conf
1
	
sed -i 's/vm.swappiness=1/vm.swappiness=10/g'  /etc/sysctl.conf

Purge cached block devices before cached filesystem entries
Shell
echo 'vm.vfs_cache_pressure=50' >> /etc/sysctl.conf
1
	
echo 'vm.vfs_cache_pressure=50' >> /etc/sysctl.conf
Optional: Enable Preload to speed up load times | only recommended for desktop usage

Note: this will increase memory usage, recommended for desktop to increase overall responsiveness.
Do NOT enable for servers.
Shell
apt-get install -y preload
1
	
apt-get install -y preload
Shell
sed -i 's/sortstrategy = 3/sortstrategy = 0/g' /etc/preload.conf
1
	
sed -i 's/sortstrategy = 3/sortstrategy = 0/g'  /etc/preload.conf

Reboot
Shell
shutdown -r now
1
	
shutdown -r now
Optimize / mount
Shell
sed -i 's/defaults,noatime/defaults,noatime,nodiratime/g' /etc/fstab
1
	
sed -i 's/defaults,noatime/defaults,noatime,nodiratime/g' /etc/fstab
Disable IPv6

Most users will not need ipv6 support and if you are only using a local there is no need. Disable it to save the resources and speed up networking.
Shell
echo "net.ipv6.conf.all.disable_ipv6=1" > /etc/sysctl.d/disableipv6.conf
1
	
echo "net.ipv6.conf.all.disable_ipv6=1" > /etc/sysctl.d/disableipv6.conf

Disable the kernel module
Shell
echo 'blacklist ipv6' >> /etc/modprobe.d/blacklist
1
	
echo 'blacklist ipv6' >> /etc/modprobe.d/blacklist

Remove IPv6 hosts
Shell
sed -i '/::/s%^%#%g' /etc/hosts
1
	
sed -i '/::/s%^%#%g' /etc/hosts

Reboot
Shell
shutdown -r now
1
	
shutdown -r now
Overclock cpu, sdram and gpu core without increasing voltage

Overclocking is now Officially Supported.

    “None” “700MHz ARM, 250MHz core, 400MHz SDRAM, 0 overvolt”
    “Modest” “800MHz ARM, 300MHz core, 400MHz SDRAM, 0 overvolt”
    “Medium” “900MHz ARM, 333MHz core, 450MHz SDRAM, 2 overvolt”
    “High” “950MHz ARM, 450MHz core, 450MHz SDRAM, 6 overvolt”
    “Turbo” “1000MHz ARM, 500MHz core, 500MHz SDRAM, 6 overvolt”

Select one of the following options:

800Mhz permanently: The settings below are safe for all Raspberry Pi’s
Shell
echo -e "arm_freq=850\nsdram_freq=450\ncore_freq=350\nforce_turbo=1" >> /boot/config.txt
1
	
echo -e "arm_freq=850\nsdram_freq=450\ncore_freq=350\nforce_turbo=1" >> /boot/config.txt
OR

850Mhz permanently: The settings below are only to be used if you have heatsinks installed
Shell
echo -e "arm_freq=950\nsdram_freq=500\ncore_freq=450\nforce_turbo=1" >> /boot/config.txt
1
	
echo -e "arm_freq=950\nsdram_freq=500\ncore_freq=450\nforce_turbo=1" >> /boot/config.txt
OR

700Mhz-1000Mhz dynamic: Scales the cpu frequency according to the load
Shell
echo -e "force_turbo=0" >> /boot/config.txt
1
	
echo -e "force_turbo=0" >> /boot/config.txt

Reboot
Shell
shutdown -r now
1
	
shutdown -r now
Replace Deadline Scheduler with NOOP Scheduler

NOOP scheduler is best used with solid state devices such as flash memory.
Shell
sed -i 's/deadline/noop/g' /boot/cmdline.txt
1
	
sed -i 's/deadline/noop/g' /boot/cmdline.txt

Reboot
Shell
shutdown -r now
1
	
shutdown -r now
Replace rsyslogd with inetutils-syslogd and remove useless logs

Reduce memory and cpu usage. We just need a simple vanilla syslogd. Also there is no need to log so many files. Just dump them into /var/log/(cron/mail/messages)

remove rsystlog
Shell
apt-get -y remove --purge rsyslog
1
	
apt-get -y remove --purge rsyslog

install syslogd
Shell
apt-get -y install inetutils-syslogd
1
	
apt-get -y install inetutils-syslogd

Create a vanilla syslogd setup
Stop syslogd
Shell
service inetutils-syslogd stop
1
	
service inetutils-syslogd stop

Remove old logs
Shell
for file in /var/log/*.log /var/log/mail.* /var/log/debug /var/log/syslog; do [ -f "$file" ] && rm -f "$file"; done for dir in fsck news; do [ -d "/var/log/$dir" ] && rm -rf "/var/log/$dir"; done
1
2
	
for file in /var/log/*.log /var/log/mail.* /var/log/debug /var/log/syslog; do [ -f "$file" ] && rm -f "$file"; done
for dir in fsck news; do [ -d "/var/log/$dir" ] && rm -rf "/var/log/$dir"; done

Create syslog.conf
Shell
echo -e "*.*;mail.none;cron.none\t -/var/log/messages\ncron.*\t -/var/log/cron\nmail.*\t -/var/log/mail" > /etc/syslog.conf
1
	
echo -e "*.*;mail.none;cron.none\t -/var/log/messages\ncron.*\t -/var/log/cron\nmail.*\t -/var/log/mail" > /etc/syslog.conf

Configure logrotate
Shell
mkdir -p /etc/logrotate.d echo -e "/var/log/cron\n/var/log/mail\n/var/log/messages {\n\trotate 4\n\tweekly\n\tmissingok\n\tnotifempty\n\tcompress\n\tsharedscripts\n\tpostrotate\n\t/etc/init.d/inetutils-syslogd reload >/dev/null\n\tendscript\n}" > /etc/logrotate.d/inetutils-syslogd
1
2
	
mkdir -p /etc/logrotate.d
echo -e "/var/log/cron\n/var/log/mail\n/var/log/messages {\n\trotate 4\n\tweekly\n\tmissingok\n\tnotifempty\n\tcompress\n\tsharedscripts\n\tpostrotate\n\t/etc/init.d/inetutils-syslogd reload >/dev/null\n\tendscript\n}" > /etc/logrotate.d/inetutils-syslogd

Start syslogd
Shell
service inetutils-syslogd start
1
	
service inetutils-syslogd start

[/social-locker]
Set a static IP and remove DHCP-Client

Reduces CPU and memory.
Create a backup config
Shell
cp -f /etc/network/interfaces /etc/network/interfaces.dhcp-backup
1
	
cp -f /etc/network/interfaces /etc/network/interfaces.dhcp-backup

Edit file: /etc/network/interfaces
replace

    iface eth0 inet dhcp

with

    iface eth0 inet static
    #set your static IP below
    address 192.168.1.107

    #set your default gateway IP here
    gateway 192.168.1.1

    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255

Reboot
Shell
shutdown -r now
1
	
shutdown -r now

===== Desktop optimization =====
===== TV setup-box (stream box) optimization =====