Skip to content

Instantly share code, notes, and snippets.

@vancetran
Last active August 1, 2019 06:30
Show Gist options
  • Save vancetran/4711776 to your computer and use it in GitHub Desktop.
Save vancetran/4711776 to your computer and use it in GitHub Desktop.
Handy Linux Terminal Commands

Handy Linux Terminal Commands

My scratchpad for useful Linux terminal commands, as I hack away at the Raspberry Pi.

Updating the Distro

Distro

via RaspberyPi.org

sudo apt-get update
sudo apt-get dist-upgrade

Version Check

uname -a

Managing Storage

These tips via eLinux - R-Pi NAS

Make new partition, if brand new drive fdisk /dev/sdb

Format to ext3 mkfs.ext3 -L "Label" /dev/sdb1

Change drive Label e2label /dev/sdc1 "Label"

Monitor Storage Mount

dmesg | tail

Edit fstab

sudo nano /etc/fstab

Re-mount all

sudo mount -a

Fstab template (for auto-mounting)

Mount by device path

/dev/sdxx /home/shares/public/disk1 auto gid=pi,uid=pi,noatime 0 0 # from elinux
/dev/sda1 /home/shares/public/disk1 ntfs-3g noatime 0 0 # Current

Better yet, auto-mount by Label

LABEL=Media /home/shares/public/media ntfs-3g noatime 0 0
LABEL=Media /home/shares/public/media2 ntfs-3g noatime 0 0
LABEL=Media /home/shares/public/media2 ntfs-3g noatime 0 0
LABEL=Seedbox /home/shares/public/seedbox ntfs-3g noatime 0 0
# Proper fstab entry for exfat, so it can be writable
LABEL=VT16 /home/shares/public/vt16 exfat defaults,auto,umask=000,users,rw 0 0

Show disks, mount points, and storage usage

df -h

List Disks by ID

ls -l /dev/disk/by-id

List Disks (via simon pie man)

sudo fdisk -l

List all disks, their Labels, UUIDs, tpye, and mount points

sudo blkid

Mount Disks

sudo mount /dev/sda1 /home/shares/public/Media

Unmount disks

sudo umount /home/shares/public/disk2

# fuser -m /dev/sdc1
/dev/sdc1: 538
# ps auxw|grep 538
donncha 538 0.4 2.7 219212 56792 ? SLl Feb11 11:25 rhythmbox

blockdev --getbsz /dev/sda1

Samba

Edit samba config

sudo nano /etc/samba/smb.conf

Backup previous samba config

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old

Create public directories

sudo mkdir /home/shares
sudo mkdir /home/shares/public
sudo chown -R root:users /home/shares/public
sudo chmod -R ug=rwx,o=rx /home/shares/public

Reboot samba

sudo /etc/init.d/samba restart

Sample public folder from simon pi man

[usb]
comment = USB Share
path = /mnt/disk1
read only = No
create mask = 0777
directory mask = 0777
guest only = Yes
guest ok = Yes

Sample public folder from elinux

[public]
  comment = Public Storage
  path = /home/shares/public
  valid users = @users
  force group = users
  create mask = 0660
  directory mask = 0771
  read only = no

Monitor SMB Log

tail -f /var/log/samba/log.smbd

VNC

vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565

General

Shutdown & Reboot

Easiest method: sudo halt These work too: sudo shutdown -h now sudo shutdown -r now

Check Current CPU Frequency

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

List Processes' CPU Utilization

top

Add the command to your ~/.bash_aliases

alias home='cd /home/dave/public_html' // Make an alias
alias // Lists all aliases

Connect via SSH

ssh pi@raspberrypi

sudo apt-get update
sudo apt-get install ntfs-3g
mount -t ntfs-3g /dev/sda1 /media/USBHDD
sudo apt-get install exfat-fuse
sudo mount -t exfat-fuse /dev/sda1 /media/drive1

LABEL=VT16 /home/shares/public/vt16 exfat defaults,auto,umask=000,users,rw 0 0

more on exFAT - 8/2015

Regenerate SSH Keys for Security

sudo rm /etc/ssh/ssh_host_* && sudo dpkg-reconfigure openssh-server

Starting Transmission

sudo /etc/init.d/transmission-daemon start
sudo /etc/init.d/transmission-daemon stop

Updating settings:

sudo nano /etc/transmission-daemon/settings.json
sudo service transmission-daemon reload

Transmission blocklist: http://list.iblocklist.com/?list=bt_level1

Install Unrar

Helpful stackexchange

Make sure you have a source repository by editing /etc/apt/sources.list.

$ cat /etc/apt/sources.list
# Default repository
deb http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
# Source repository to add
deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi

Sync the apt database.

$ sudo apt-get update

Create a working directory and move into it. The unrar-nonfree command will be built in this directory.

$ mkdir ~/unrar-nonfree && cd ~/unrar-nonfree

Install the dependencies required by unrar-nonfree.

$ sudo apt-get build-dep unrar-nonfree

Download the unrar-nonfree sources and build the .deb package.

$ sudo apt-get source -b unrar-nonfree

Install the generated .deb package. Its name varies depending on the version of unrar-nonfree.

$ sudo dpkg -i unrar*.deb

You can remove the working directory you created in 4.

$ cd && rm -r ~/unrar-nonfree

Once installed, you can use either unrar or unrar-nonfree (unrar is a simlink to unrar-nonfree).

Warning: unrar-nonfree and unrar-free options are different. For instance, to extract an archive:

$ # with unrar-free
$ unrar -x my_archive.part01.rar

$ # with unrar-nonfree
$ unrar e my_archive.part01.rar

Please man unrar-nonfree for more details.

Unrar

unrar e source.rar ../../destination

Continue to run a job in the background, even if you close the terminal/logoff

nohup [command] &

Adding current remote SSH key into local known_hosts

Handy if you run into WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! message on fresh reformat of the Pi

ssh-keyscan raspberrypi >> ~/.ssh/known_hosts

Do even more with Raspberry Pi

eLinux Raspberry Pi Guides

Wireless setup via command line

Handy guide

sudo nano /etc/network/interfaces Ensure that the section about wlan0 (typically found at the end of the file) reads as follows:

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Scan for local wireless: sudo iwlist wlan0 scan | grep ESSID

In: sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add:

network={
        ssid="YourSSID"
        psk="password"
        key_mgmt=WPA-PSK
}

Seems to work best without specifying TKIP/WPA stuff.

Wifi auto reconnect

Go to /etc/ifplugd/action.d/ and rename the ifupdown file to ifupdown.original
Then do: cp /etc/wpa_supplicant/ifupdown.sh ./ifupdown
Finally: sudo reboot

http://raspberrypi.stackexchange.com/a/5341/5454

Raspbmc black screen? Try adding this to the config.txt file

hdmi_group=1
hdmi_mode=4

Video blackouts? Try:

hdmi_force_hotplug=1
config_hdmi_boost=4

Openelec config.txt

Need to mount the flash as writable before editing /flash/config.txt

mount /flash -o remount,rw

OpenELEC on USB Storage

For particularly ornery Raspberry Pi's that seem to corrupt SD card storage... Also, it's supposedly faster and is great when you have small capacity SD cards on hand.

http://kodi.wiki/view/HOW-TO:Install_XBMC_on_Raspberry_Pi_with_USB_drive

Transfer file over SCP / SSH

via Unix Stack Exchange

To copy a file from B to A while logged into B: scp /path/to/file username@a:/path/to/destination

To copy a file from B to A while logged into A: scp username@b:/path/to/file /path/to/destination

Adding a swapfile

https://linuxbsdos.com/2017/05/26/replace-the-swap-partition-with-a-swap-file-after-upgrading-to-ubuntu-17-04/

fixing cryptswap issue

https://ubuntuforums.org/showthread.php?t=2372031 https://askubuntu.com/questions/1031890/recovery-of-old-ecryptfs-data-does-not-work

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