Helpful Raspberry Pi Linux Commands

Every Raspberry Pi user at some point will need to enter some linux commands into the terminal window to accomplish something, wether thats to install some software package or move some files around. If you have never used Linux before then it can be quite challenging to get around, thats why we have created the below guide to get you started with some fo the basic and most useful commands.

Navigating this guide

We have provided the quick link below so you can navigate easily to the sections that you wish:

System

uname -a – Get current linux system information such as kernel version. This command is useful in determining if your system is up to date

uname -r – Displays the kernel release

Note: A kernel is the central part of the OS that manages things like the CPU and memory, whilst holding device drivers.

uptime – This shows how long the system has been up and running for

hostname – Shows the hostname of the device. If you have multiple devices or Raspberry Pi’s it can be useful to differentiate between them

hostname -i – Display the current IP address of the hostname

last reboot – Show system reboot history

date – Shows current system date and time

cal – Shows the calendar month. This is handy if you need to schedule tasks

w – Display who is online. It is possible to have multiple users logged in remotely to the Pi at the same time

whoami – Display who you are logged in as. Some programs may have different user permissions

Update System

apt update – Get list of latest available packages

apt upgrade – Upgrade all packages to latest version

apt dist-upgrade – Upgrade all packages and their dependancies

rpi-update – Update firmware

Hardware

dmesg – Detect hardware and boot messages. Can be useful when debugging hardware issues

cat /proc/cpuinfo – Get CPU model. You can use this to determine which Raspberry Pi model you have

cat /proc/meminfo – Hardware memory

free -m – Show used and free memory

lsusb -tv – Show USB devices that are connected

Users

id – Show the active users that is logged in with id and user group

last – Show last logins to the system

who – Show who is currently logged in to the system. (different from running w)

groupadd admin – Create users group “admin”

adduser joe bloggs – Add user “Joe Blogs”

useradd -c “Joe Bloggs” – Create user “Joe Bloggs”

deluser joe bloggs – Delete user “Joe Bloggs”

usermod – Modify user information

File Commands

ls -al – Show all information about current directory and files

pwd – Show the exact path to the current directory

mkdir directory-name – Creates a new folder directory replacing “directory-name” with the folder name

rm filename – Delete’s any file, replacing “filename” with the file you want to delete

rm -r directory-name – Delete’s any file and its contents

rm -f file-name – Forcefully removes the file

rm -fr directory-name – Forcefully removes file and its contents

cp file1 file 2 – Copy “file1” to “file2”

cp -r directory1 directory 2 – Copy directory1 to directory 2 or create directory 2 if not exists

mv file1 file 2 – rename/move “file1” to “file2”

touch file – create or update file

cat > file – place a standard input into the file

more file – output the file contents

head file – output first 10 lines of the file

tail file – output last 10 lines of the file

tail -f file – output contents of the file starting with the last 10 lines

Process

ps – display your current active processes

ps aux | grep ‘telnet’ – Find all process id’s related to “telnet” process

pmap – memory map of all processes

top – display all running processes(not all active)

kill pid – kill all processes that mention pid ID

killall proc – kill all processes named proc

File Permissions

chmod octal filename – Changes the file to use octal number system

chmod 777 home/test.py – Sets rwx (read, write, execute) permission for owner, group world

# Permission rwx
7 read,write and execute rwx
6 read and write rw-
5 read and execute r-x
4 read only r–
3 write and execute -wx
2 write only -w-
1 execute only –x
0 none

chown owner-user file – Changes the owner of the file, replacing “owner-user” with username

chown owner-user:owner group file-name – Change owner and group of file

Network

ip addr show – Display all network interface and their IP addresses

ip address add 192.168.0.1 eth0 – set manual ip address to network interface eth0

ethtool eth0 – Linux tool that show ethernet status

ping host – send echo request to test the network connection

whois domain – Get whois information for a domain, replacing “domain” with a domain name

dig domain – Get DNS information about a domain, replacing “domain” with a domain name

dig -x host – reverse lookup host

wget filename – download a file, replacing filename with URL

netstat -tupl – List all active listening ports

ifconfig – Displays current network interface information

Compression/Archive

tar cf home.tar home – Create a compressed archive called home.tar based on the contents of home directory

tar xf file.tar – Extract the files from file.tar

tar czf file.tar.gz files – Create a tar with a gzip compression

gzip file – Compress file and rename it to file.gzip

Install Packages

rpm -i pkgname – Install rpm based package

rpm -e pkgname – Removed installed package

apt install pkgname – Install package from repository

Install from source

./configure

make

make install

grep pattern files – Search for “pattern” in files

grep -r pattern dir – Search all files in directory for pattern

locate file – Find all instances of the word “file”

Login

ssh user@host – Connect to host as user i.e ssh [email protected]

ssh -p port user@host – Connect to host using a specific port, replacing “port” with the port number

telnet host – Connect to host using telnet

File Transfer

scp – Secure copy

scp file.txt server2:/temp – Copy file.txt to server2 /temp directory

rsync – Synchronise files

rsync -a /home/app /backup/ – Synchronise source to destination

sftp

Disk Usage

df -h – Show free space on mounted drives

fdisk -l – Show disk partition size

du -ah – Show disk usage in a readable format

du -sh – Show disk usage in current directory

Directory Traverse

cd .. – Go up one level from the directory tree

cd ~ – Change directory to home

cd /test – Change directory to /test

First published at 12:19pm on September 23, 2019
Last updated at 8:06pm on October 6, 2020