List of Useful Linux Commands

To become root in Linux Mint, type:

sudo su

The password is blank, so just hit "enter" for the password.

To show the Linux distribution, kernel number, and OS word length

uname -a

To show the host name:

hostname

To show the IP address of the machine you are on:

ifconfig

To show the MAC address of the network card of interest,

type:

ifconfig -a

the MAC address will be next to "HWaddr" or "ethernet".

To show the UUID's of the attached storage devices:

sudo blkid

To show all the USB devices, type:

lsblk

To show the read speed of a USB device, type:

sudo hdparm -Ttv /dev/sdc1

And look at the number to the right of "Timing buffered disk reads:".

To use the "find" command to find a file in the current directory

or below, type:

find ./ | grep

To use "locate" to find specific files, type:

sudo updatedb

locate

To check the encoding of the file in.txt:

file -bi in.txt

To change a file's encoding from ascii to UTF-8:

iconv -f ascii -t utf-8 -o out.txt in.txt

To show all the running daemons in Linux Mint:

sudo service --status-all

To see if a particular service is running in Linux Mint:

sudo service status

For Linux distributions with Systemd, show all running services with:

sudo systemctl | grep running

, or try:

sudo systemctl list-unit-files --state=running

Other systemctl commands:

sudo systemctl status

sudo systemctl hibernate

sudo systemctl edit --full

sudo systemctl is-active

sudo systemctl is-enabled

sudo systemctl is-failed

sudo systemctl list-units --state=active

sudo systemctl list-unit-files

To start a daemon running, type:

sudo service start

To see if a particular daemon is running, type:

systemctl | grep

In Linux Mint or Raspian, to start a process (which can be your own

script,as long as it has execute privaledges) called

running at bootup, since putting a call to

in /etc/rc.local doesn't seem to work in Linux Mint, type:

sudo update-rc.d defaults

, which puts the file into /etc/init.d and

causes it to run at startup. You must ALSO create a service by putting

a file (like, for example, rc.local.service) into the

/etc/systemd/system/ folder.

In linux Mint or Raspbian, you can put process that you want to startup at boot

into /etc/init.d/rc.local or into /etc/rc.local. I have my Raspbian processes

starting up in /etc/rc.local, and my Linux Mint processes starting up

in /etc/init.d/rc.local.

In some Linux distributions, services can be started, stopped,

restarted, reloaded, or removed at any time by typing:

sudo service start

sudo service stop

sudo service restart

sudo service reload

sudo update-rc.d remove

To set the data and time, for example Dec 22 2017 at 9:57 AM:

sudo date --set="20171222 09:57:00"

To download a package without installing it. The package

appears in /var/cache/apt/archives.

sudo apt-get install --download-only

To install a package's dependencies:

sudo apt-get build-dep

To install a package using only .deb files that

are already in the /var/cache/apt/archives directory,

go to the /var/cache/apt/archives directory and type:

sudo dpgk -i *

To search for packages by name:

apt-cache search -n

To show what would happen if you were to you install a package

without actually installing it:

sudo apt-get install --dry-run

To get a short description of a package:

apt-cache search

To get information about and already-installed package:

apt-cache show

To see package dependencies:

apt-cache showpkg

To download only source of a package:

sudo apt-get --download-only source

To download source, unpack, and compile a package:

sudo apt-get --compile source

Some system admins don't post their keys for their

repositories, and this prevents you from using sudo apt-get

install. This happened for Parrot OS, and I fixed it with:

sudo apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 363A96A5CEA9EA27

To show what packages are already installed:

dpkg -L

To remove a package:

dpkg -r

To add a repository to /etc/apt/sources.list.d directory:

sudo add-apt-repository

To see what repositories you have added, type:

apt-cache policy | grep http | awk '{print $2 $3}' | sort -u

or

inxi -r

and this also tells you other things, like the location of your

sources.list file.

To remove a repository from your local repository list, type:

sudo add-apt-repository remove ppa:/ppa

To see what applications are contained in a repository:

grep ^Package /var/lib/apt/lists/repository-name*_Packages | awk '{print $2}' | sort -u

or look for the packages in the repository files in the /var/lib/apt/lists directory.

To tar a directory or file:

tar -cvf name-of-archive.tar /path/to/directory-or-file

To make a gzip tar file, use -czvf (must be in this order to work).

To untar a file or directory:

tar -xvf name-of-archive.tar

To tar a directory preserving owner and file permissions:

tar --same-owner -cvf name-of-archive.tar /path/to/directory

To show the amount of disk space used (in megabytes):

df -hk

The -k option shows the exact number of bytes on the disk!

To show the amount of disk space used by one directory:

du -sh

To display disk partition sizes:

sudo fdisk -l

To create a Linux swap partition, open fdisk or gparted

and create an ext2 partition that is twice the size of your RAM.

Then edit the /proc/sys/vm/swappiness file and insert 10 to begin

swapping when RAM is 90% full. Then, for example:

swapon /dev/sda5

sudo sysctl vm.swappiness=10

swapon -s

NOTE: editing /proc/sys/vm/swappiness directly doesn't work because the

backup file is not overwritten, and it is not used at next boot up.

Then add

swapon /dev/sda5

sudo sysctl vm.swappiness=10

to the /etc/rc.local file.

Another way to create a swap partition is:

sudo fallocate -l 4G /swapfile (makes file with size 4GB)

sudo chmod 600 /swapfile (as root?)

sudo mkswap /swapfile (formats the swap file)

sudo swapon /swapfile

The command to check that you did everything correctly is:

sudo free -h

To make this permanent add to /etc/fstab:

/swapfile swap swap defaults 0 0

To show the total and free memory:

cat /proc/meminfo or free -h

To create a symbolic link:

ln -s

for example:

ln -s /etc/NameOfComputer /etc/hostname

To remove a symbolic link:

rm

To get information about a file, including whether it is 32 or 64 bits:

file

Prints the shared objects (shared libraries) required by a program or shared object.

ldd

To show the real path, not the path to the symbolic link:

pwd -P

To convert a DOS file to a unix file:

sed -i 's/.$//'

To replace Sam with Samuel everywhere in the file customer.txt:

cat customer.txt | sed -e 's/Sam/Samuel/' > customer.txt

or

sed -i “s/Sam/Samuel/g” customer.txt

The -e option to sed tells it to use the next item as the sed command.

For more information about sed commands, see http://www.complexsql.com/sed-command/.

To only change the first occurence in the file:

sudo

sed -i “s/Sam/Samuel/” customer.txt

To change multiple files:

sed -i "s/Sam/Samuel/g" *.txt

To add a string of characters on a new line, insert \n before the string.

To difference two files while ingnoring whitespace differences:

diff -w

To create an image file of what is on a disk:

sudo dd if=/dev/sdb1 of=/mnt/USB/Image_File_1.img bs=4096 conv=notrunc,noerror

To write an image file to a disk:

sudo dd if=/mnt/USB/Image_File_1.img of=/dev/sdb1 bs=4096 conv=notrunc,noerror

To write a piratebox image file to a flash card:

First remove all partions on the flash card (assume it is /dev/sdc), then

sudo dd if=/mnt/USB/PirateBox_1.img of=/dev/sdc bs=4096 conv=notrunc,noerror

To copy the entire contents of partition sdb1 to partition sdc1:

dd if=/dev/sdb1 of=/dev/sdc1 bs=1M

To install the Arch Linux ISO onto a USB stick, type:

sudo dd if=archlinux-2020.09.01-i686.iso of=/dev/sdb bs=4M status=progress oflag=sync

To write an ISO file to to a flash drive, try:

sudo cp bootable.iso /dev/sdx

To install a program in a gethub directory using pip (which can be

installed using "sudo apt-get install python-pip"):

pip install https://github/com/kimbauters/ZIMply

To see what modules (drivers) are being used, type:

lsmod

In Linux mint, to create a daemon called rc.local,

create an rc.local executable and put it in

/etc/init.d. Then type:

sudo chmod 744 /etc/init.d/rc.local

sudo update-rc.d rc.local defaults

To get the rc.local daemon to run at boot, you may also have to

create a service, as shown here:

https://wiki.archlinux.org/index.php/User:Herodotus/Rc-Local-Systemd

On the PirateBox, to write log files to RAM, instead

of to the SD card, add this line to the /etc/fstab file:

tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0

The size=100m means use a maximum size of 100 MB. This prevents all

the RAM from being filled up. This is already in the PirateBox img

file that I downloaded from the PirateBox website.

To change a read-only file system to a read and writeable file system:

sudo mount -o remount,rw '/media/mint/'

To fix the problem of a USB flash drive not allowing you to copy contents:

sudo umount /media/mint/

sudo mkdir USB

sudo mount -o rw /dev/sdc1 /media/mint/USB

(or sudo mount -t ntfs /dev/sdc1 /media/mint/USB)

(or sudo mount -t ntfs-3g /dev/sdc1 /media/mint/USB) (which is more reliable than ntfs)

If you can't change owner or file permissions on the files on a USB flash

drive, remember that the NTFS file system does not support Linux-style file

permissions!!!

Do not use linux tools to check and repair NTFS drives. Instead, boot Windows,

and use Windows’ own file system check and repair tools (chkdsk).

To send an email with sendmail from the Linux command line, type:

sendmail user@example.com < /tmp/email.txt

To install a Debian package from a file .deb:

sudo dpkg -i .deb

sudo apt-get install -f (downloads any packages that .deb package is dependent on)

***User Control***

To create a new user:

sudo adduser

To change a user's password:

sudo passwd

To add a user to an existing primary group:

useradd -g cartoons tom

To create a primary group

To remove a user:

sudo deluser

To prevent a user from being able to log on:

sudo passwd -l

To allow a user to log on again:

sudo passwd -u

To show the groups of which a user is a member:

sudo groups

To grant sudo privaledges:

sudo adduser sudo

To add a user to any group:

sudo adduser

You have to reboot the computer for the change to take effect.

To remove a user from a group:

sudo deluser

***NETWORKING***

Display all the network interfaces:

ifconfig -a

Examples of how to disable and enable a network interface:

ifconfig wlan0 down (use "up" to bring interface back up)

To assign an IP adress to eth0:

ifconfig eth0 192.168.1.5

To bring up eth0:

ifconfig eth0 up

Examples of how to find the IP address or the DNS name of the IP address:

nsloopkup www.google.com

nslookup 8.8.8.8

Show all the computers on the network:

cat /etc/hosts or hostname -I

To logon to a computer on the network:

ssh @

for example:

ssh alarm@192.168.77.1

Then enter password.

To SSH using a different port number:

ssh alarm@192.168.77.1 -p

To see who is connected to your computer by ssh:

ss | grep ssh

To show who is currently on the network:

w

To show who has logged in over the last couple of months:

last -a

Another way to find out which host computers are connected to

your local network (with network mask 255.255.255.0) is to type:

sudo nmap -sn 192.168.77.0/24

where 192.168.77 is replaced by the first three parts of the "inet add" IP

address of your network card (eth0 or wlan0 or wlan1), which can

be found with, for example, "ifconfig eth0".

To copy a file from one computer to another on the network, type:

scp filename username@ip_address:location

where username@ip_address is the user on the computer you are

not on and location is a file name with a full path. For example:

scp my_text_file.txt alarm@192.168.77.1:~/.

For this to work, you must know the password of the user on the other computer!

To copy a whole directory from a remote computer to your local computer:

scp -rp user_name@192.168.1.112:/var/www www_10-18-20

The above command will preserve the dates but not the file permissions.

To scp from a remote location over the internet, do this:

scp -P piratebox_mods.html user_name@cheapskatesguide.org:~/.

where is the port number that is used to get around the website ISP's

port blocking.

To copy a directory from the Raspberry Pi and preserve permissions, this should work:

rsync -rp www root@192.168.1.139:/media/mint/KINGSTON/www_11-10-19

but it just sits there and never asks for the root password.

This command from my Linux laptop fails to copy root-owned files,

because root login has been diabled:

rsync -a root@192.168.1.112:/var/www www_3-20-19

The "a" option means archive, which means copy files recursively

while preserving the directories, dates, and file permissions. Files

owned by user_name can be copied with this command:

rsync -a user_name@192.168.1.112:/var/www www_3-20-19

To show the gateway address and net mask

netstat -rn

To show the SSID's of the wifi networks in the area:

sudo iwlist wlan0 scan | grep SSID

The command "iwconfig wlan0 " will give information for the network you

are currently connected to, the signal strength included.

To change the transmission power of wlan1, in the /etc/network/interfaces type:

ifconfig wlan1 down # Prevents wifi from working!!!

iwconfig wlan txpower 15

ifconfig wlan1 up

The txpower is in dB, so 15 dB is 30 mW. For the PirateBox, add the above lines

into the /opt/piratebox/config/piratebox.conf file below the line that is "INTERFACE="WLAN1"".

If you still can't get a txpower higher than 20 dB, you may have to change the wifi

regulatory domain to some other country. Do this by:

iw reg set US (for the Untied States)

iw reg set GY (for Gyana)

iw reg set BO (for Bolivai)

To check that the regulatory region is now correct, type:

iw reg get

To set the wifi channel to channel 6, in the /opt/piratebox/config/hostapd.conf file, put:

channel 6

To see if a wifi adapter is being used, type:

lsusb

If you can see your wifi adapter listed, it is "most likely" running.

To check to see if the adapter's driver is loaded, type:

lsmod

To see if the wifi adapter is definitely being picked up, type:

ifconfig -a

If no bits are being transmitted or recieved, it is down, so type:

sudo ifconfig wlan0 down

sudo ifconfig wlan1 up

You can also type:

sudo iwlist wlan1 scan

where wlan1 is the adpater's wifi designation. The displayed

wifi adapter's designation obtained this way may have

been from the previous "sudo iwlist wlan0 scan".

To set the IP address of wlan1, tyoe:

sudo ifconfig wlan1 192.168.77.1

You can make wlan1 be used automatically on boot by changing

the /etc/network/interfaces file to replace wlan0 with wlan1 everywhere.

In Raspbian Stretch, if you can't ping either wwww.google.com or 8.8.8.8 ,

but you can ping the Raspberry Pi from your laptop, try sending a DHCP

request from the Raspberry Pi:

sudo dhclient -v eth0

This should get your dhcp daemon working again.

To show what rules are active in the iptables, type:

iptables -L -v

To see if the dnsmasq daemon (which acts both as dns and dhcp

servers for connections) is running, type:

systemctl status dnsmasq.service

Note that if the dnsmasq daemon is not running, typing:

dnsmasq

will attempt to run dnsmasq and will PRINT ANY ERROR MESSAGES TO

THE CONSOLE. This will help you debug. The same will work with the

hostapd daemon!

To determine which ports are open on your local eth0, get the IP from "ifconfig eth0".

Then type:

nmap --top-ports 20

Or go to grc.com/shieldsup.

An a Raspberry Pi, to close a port ("filter" it so no traffic can pass), type:

sudo iptables -A INPUT -p tcp --destination-port -j DROP

sudo service iptables save

Make sure ports 53, 139, and 445 are blocked on Linux Mint. If the

above commands don't work, you may have to remove the running daemons.

On Linux Mint, to close a port, if ufw is enabled, type:

sudo ufw deny

To see what ufw commands are available, type:

sudo ufw show

To start and stop ufw:

sudo ufw disable

sudo ufw enable

Allow ssh:

sudo ufw allow ssh

Allow port 80 and 443

sudo ufw allow 80/tcp

sudo ufw allow 443/tcp

To get a list of ports that are allowed through ufw:

sudo ufw status

To show the status with numbers in front of the rules:

sudo ufw status numbered

To remove a rule:

sudo ufw delete 5 (for the 5th rule)

To find out which program is listening to this port, type:

sudo lsof -i -P | grep ': '

or

sudo netstat -plnt

Note that this DOES NOT tell you which ports are open, only which ports are

being listen to.

*****DRIVER MODULES*****

Modules are stored in /usr/lib/modules/kernel_release. On the PirateBox

they are found in /usr/lib/modules/kernel_release/kernel; the Ralink

rt2800usb wifi driver is in /usr/lib/modules/kernel_release/kernel/drivers/net/wireless/ralink directory.

To see what modules (drivers) are being used, type:

lsmod

To show information about a module:

modinfo module_name (don't include the .ko.gz at the end)

To list the options that are set for a loaded module:

systool -v -m module_name

To display the comprehensive configuration of all the modules:

modprobe -c | less

To display the configuration of a particular module:

modprobe -c | grep module_name

List the dependencies of a module (or alias), including the module itself:

modprobe --show-depends module_name

To load a module:

modprobe module_name

To load a module by filename (i.e. one that is not installed

in /usr/lib/modules/$(uname -r)/):

insmod filename [args]

To unload a module:

modprobe -r module_name

Or, alternatively:

rmmod module_name

To install an Alfa AWUS036NH wifi adapter driver, for

example DPO_RT5572_LinuxSTA_2.6.1.3_20121022.tar.bz2 :

sudo apt-get install gcc

tar -xvf DPO_RT5572_LinuxSTA_2.6.1.3_20121022.tar.bz2

cd DPO_RT5572_LinuxSTA_2.6.1.3_20121022

make

If make gives an error message that says it can't find stdio.h, then

sudo apt-get install build-essential

sudo make install

If you get a compilation error, try downloading from Alfa.com.tw

a newer driver file.

*****FUN STUFF*****

To install and use fortune and cowsay:

sudo apt-get install fortune cowsay

fortune | cowsay

To get this to come up every time you open a terminal window, add this line to the end of your ~/.bashrc file:

fortune | cowsay

To install espeak:

sudo apt-get install espeak

To make your computer talk:

espeak "Hello, I am your computer."