Skip to content

Instantly share code, notes, and snippets.

@mlagerberg
Last active September 10, 2017 22:56
Show Gist options
  • Save mlagerberg/df0e433f984b4c3595f7 to your computer and use it in GitHub Desktop.
Save mlagerberg/df0e433f984b4c3595f7 to your computer and use it in GitHub Desktop.
[rpi-install.sh] Installs and configures many things useful for a fresh Rasbian install #rpi #bash
#!/bin/bash
############################################################################
#
# Installs and configures many things useful for a fresh Rasbian install.
# All things configured are described in this repo:
# https://github.com/mlagerberg/raspberry-pi-setup
#
# Created by Mathijs Lagerberg, 2015
#
# It does NOT:
# - configure many things described in that repo that require
# manual configuration;
# - tell you which things those are.
#
# It DOES:
# - install many more packages that are NOT described in that
# repo which you probably wont need.
#
# USE WITH CAUTION AND AT YOUR OWN RISK!
#
############################################################################
##
# Pauses and wait for key press
pause() {
read -n1 -rsp $'Press any key to continue or Ctrl+C to exit...\n'
}
# Check for root access
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Show SSH fingerprint
echo "Please take note of this Pi's SSH fingerprint before continuing:"
ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub
pause
# Ask for WiFi network name and password; used later on
while : ; do
read -rp $'Please enter your WiFi SSID or [Enter] to skip:\n' _input_ssid
if [[ $_input_ssid = "" ]]; then
break
fi
read -rp $'Please enter your WiFi password (WPA2 assumed): \n' _input_password
echo "You've entered: $_input_ssid, $_input_password"
read -n1 -rp "Is that correct? [Y/n] " reply
echo
if [[ $reply =~ ^[Yy]$ ]]; then
break
fi
done
# Change default password
echo "Please change the default password for user 'pi':"
passwd pi
# Change the hostname
echo "The current hostname is:"
hostname
read -rp $'Please enter a new hostname or [Enter] to skip:\n' _input_hostname
if [[ ! $_input_hostname = "" ]]; then
hostname $_input_hostname
echo "127.0.0.1 $_input_hostname" >> /etc/hosts
echo "$_input_hostname" > /etc/hostname
/etc/init.d/hostname.sh start
fi
# Enlarge partition
echo
df -h | grep 'rootfs\|Filesystem'
read -n1 -rp "Is the above the correct Size for the SD card partition (roughly)? [Y/n] " reply
echo
if [[ ! $reply =~ ^[Yy]$ ]]; then
echo "'raspi-config' will start to enlarge the partition."
echo "Choose '1. Enlarge Filesystem' in the next screen and"
echo "select <Finish> when it's done."
pause
raspi-config &
wait
fi
# Disable root login:
echo
echo "Disabling root login..."
passwd -dl root
# Keep SSH connections alive (do this before installing packages!)
echo "Configuring SSH..."
echo "
ClientAliveInterval 30
TCPKeepAlive yes
ClientAliveCountMax 99999" >> /etc/ssh/sshd_config
#sudo service network-manager restart
sudo service networking restart
# Install all kinds of stuff
echo
echo "Installing packages..."
apt-get update
apt-get -y install git iptables fail2ban libpam-google-authenticator iftop ntfs-3g ntfs-config testdisk xrdp x11-xserver-utils unclutter motion bluetooth bluez blueman pi-bluetooth bluez-firmware gphoto2 samba samba-common-bin lirc liblircclient-dev libav-tools tightvncserver net-tools mailutils python-pip tmux
# Set-up WiFi
if [[ ! $_input_ssid = "" ]]; then
echo "Configuring WiFi..."
echo "
iface home inet dhcp" >> /etc/network/interfaces
echo "
network={
ssid=\"$_input_ssid\"
psk=\"$_input_password\"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
id_str=\"home\"
}" >> /etc/wpa_supplicant/wpa_supplicant.conf
# Prevent WiFi password to be read by other users
chmod 0600 /etc/network/interfaces
# Disable WiFi power management
echo "options 8192cu rtw_power_mgnt=0" > /etc/modprobe.d/8192cu.conf
fi
# iptables
echo "Configuring firewall..."
iptables -A INPUT -j ACCEPT -m state --state ESTABLISHED,RELATED
# Web
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 8000 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 8080 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT
# SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
# BTSync
iptables -A INPUT -p tcp --dport 55555 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 3838 -m state --state NEW -j ACCEPT
iptables -A INPUT -p udp --dport 3838 -m state --state NEW -j ACCEPT
# Syncthing
iptables -A INPUT -p tcp --dport 22000 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 21025 -m state --state NEW -j ACCEPT
# Remote desktop
iptables -A INPUT -p tcp --dport 3389 -m state --state NEW -j ACCEPT
# Ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# Block the rest
iptables -P INPUT DROP
iptables-save > /etc/iptables.up.conf
# And make sure the config file is used:
echo "#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.conf" > /etc/network/if-pre-up.d/iptables
chmod +x /etc/network/if-pre-up.d/iptables
# Install dotfiles
su pi <<'EOF'
mkdir ~/dotfiles
cd ~/dotfiles
git clone https://github.com/mlagerberg/dotfiles
cd dotfiles/
chmod +x install.sh
source install.sh
EOF
# Install hosts file
cp /etc/hosts /etc/hosts.old
wget https://raw.githubusercontent.com/mlagerberg/raspberry-pi-setup/master/scripts/hosts
mv hosts /etc/hosts
# Alter LEDs
echo "
dtparam=act_led_trigger=mmc0
dtparam=act_led_activelow=off
dtparam=pwr_led_trigger=cpu0
dtparam=pwr_led_activelow=off
" >> /boot/config.txt
# Install BitTorrent Sync
mkdir /var/cloud
cd /var/cloud/
wget "http://download.getsyncapp.com/endpoint/btsync/os/linux-arm/track/stable"
tar -zxvf stable
rm stable
(exec ./btsync --dump-sample-config) >> btsync.conf
useradd --shell /bin/false -d /var/cloud btsync
chown -R btsync:btsync /var/cloud
# All done, let's update
apt-get -y dist-upgrade
# Restart SSH
/etc/init.d/ssh restart
# And make sure it starts on boot
#sudo update-rc.d ssh defaults
sudo systemctl enable ssh
# Restart WiFi
#sudo service network-manager restart
sudo service networking restart
# And reboot
echo "The Raspberry Pi will now reboot."
echo "Note when running a Raspberry Pi 3:"
echo "After this, make sure to do do the following."
echo " sudo rpi-update"
echo " sudo reboot"
#echo "And after that reboot:"
#echo " sudo branch=next rpi-update"
#echo " sudo reboot"
echo
pause
reboot
@mlagerberg
Copy link
Author

Installs and configures many things useful for a fresh Rasbian install.
All things configured are described in this repo:
https://github.com/mlagerberg/raspberry-pi-setup

Created by Mathijs Lagerberg, 2015

It does NOT:

  • configure many things described in that repo that require
    manual configuration;
  • tell you which things those are.

It DOES:

  • install many more packages that are NOT described in that
    repo which you probably wont need.

USE WITH CAUTION AND AT YOUR OWN RISK!

Usage:

wget https://gist.github.com/mlagerberg/df0e433f984b4c3595f7/raw/efb5500ce2b6eebcde40fd59b67a6750fd8ed035/install.sh
chmod +x install.sh
sudo ./install.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment