Linux - Tips

From XennisWiki
Jump to: navigation, search

Basic

Shutdown, reboot and hibernate

Shutdown and reboot

(1) Shutdown or (2) reboot

poweroff
reboot

Hibernate

Install pm-utils

apt-get install pm-utils

Hybernate by pm-hibernate

Get process which uses a port

(1) Get information who uses a port and (2) use the returned pid to get information about that process.

lsof -i :<port number>
ps ax | grep <pid>

Get memory information

All memory information

cat /proc/meminfo

RAM size

grep MemTotal /proc/meminfo

Get number of cores

grep -c ^processor /proc/cpuinfo

Switch between GUI and terminal

Shortcut Description
[Ctrl] + [Alt] + [F1] Switch to terminal
[Alt] + [F7] or [Alt] + [Right] Swich back to GUI

Change login shell (chsh)

Output current shell

echo $SHELL

List installed shells

chsh -l

Set a default shell (use for instance: chsh -s `which zsh`)

chsh -s <full-path-to-shell>

Get the UUID of a partion

Get the UUID of a partion by it's label (for instance /dev/sda1)

blkid <label>

Create a bootable USB drive to install a Linux distribution

Linux
  • Option 1 with Terminal: see dd
  • Option 2 with GUI: Many system already have a tool installed. For example Startup Disk Creator on Ubuntu or Deepin Boot Manager on Deepin.
  • Option 3 with GUI: Install and run the tool UNetbootin
Windows

Download the tool Rufus and use it to create a bootable USB drive to install a Linux distribution.

Create a new entry for menu

Create a /usr/share/applications/<application>.desktop file (replace the placeholder with the actual name of the application, for instance firefox or franz). An example for the content of the file is

[Desktop Entry]
Type=Application
Version=1.0
Name=Franz
GenericName=Internet Messenger
Comment=A free messaging app for services like WhatsApp, Slack, Messenger and many more.
Icon=franz
TryExec=/opt/franz/Franz
Exec=/opt/franz/Franz %U
Terminal=false
Categories=Network;InstantMessaging;

Adjust the field accordingly.

Disable a tool to automatically start

The directory .config/autostart contains *.desktop files of tools, that automatically start. Delete a file prevents the tool to start on start (login).

Switch audio output using the terminal

List audio card profiles

pacmd list-cards

Switch audio output to card with index 0 and profile named hdmi-stereo

pacmd set-card-profile 0 output:hdmi-stereo

Figure out Windows licence key stored in BIOS configuration

sudo hexdump -s 56 -e '"MSDM key: " /29 "%s\n"' /sys/firmware/acpi/tables/MSDM

Display CPU temperature

Install lm-sensors and run the configuration

apt-get install lm-sensors 
sensors-detect

Ouput the CPU temperature

sensors

Setup

Install local mail via Postfix

  • Install Postix
    • Debian: apt-get install postfix
    • Arch Linux: pacman -S postfix
  • Open the configuration file /etc/postfix/main.cf and add the luser_relay and set the luser_relay to local:
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = <hostname>
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = <hostname>, localhost.localdomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
default_transport = local
relay_transport = error
inet_protocols = all
luser_relay = <user>@localhost
  • Restart the service: service postfix restart

Send a local mail. It should be listed in the /var/mail/<user> file.

(Optional) Setup Thunderbird
  • Open Account Settings and select Account Actions > Add Other Accounts
  • Choose Unix Mailspool (Movemail) and follow the setup dialog. As mail enter the defined luser_relay

Configuration

Create environment variable

(1) Print all environment variables, (2) create a environment variable and (3) print it

printenv # or env
export EXAMPLE_ENV="hello"
echo $EXAMPLE_ENV
Always set environment variable
  • Add the line export EXAMPLE_ENV="hello" to the file ~/.bashrc
    • For instance use: echo "export EXAMPLE_ENV=hello" >> ~/.bashrc
  • Reload the .bashrc file: source ~/.bashrc

Increase number of user watches (watch files)

Further information: Increasing the amount of inotify watchers

Debian

Configure the number of max_user_watches in the /etc/sysctl.conf file. Then apply the changes.

echo "fs.inotify.max_user_watches = 524288" >> /etc/sysctl.conf
sysctl -p --system

Arch Linux

echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system

Partions

Network

Set a custom DNS with resolvconf

Install resolvconf. For Debian:

apt-get install resolvconf

Edit the head file

vim /etc/resolvconf/resolv.conf.d/head

Add for instance nameserver 8.8.8.8 as a new line.


(1) Enable the update scripts and (2) run them

resolvconf --enable-updates
resolvconf -u

Use the host command to resolve a domain.

Files

Extract a file

tar xfvz sample.tar.gz

read

Den Inhalt einer Datei in eine Variable einlesen

read -r DATEI < ~/pfad/datei.txt
echo $DATEI

Copy image (.img, .iso) to USB or SD card

dd will format the device, set it's label and copy the image. (Hint: The file system is iso9660, the label is for instance ARCH_201901 and no flags are set.)

  • Plug in device (if it is mounted, unmount it: umount <label>)
  • Figure out device label with lsblk (or with fdisk: fdisk -l). A label is for instance /dev/sdc (Note: not a particular partition /dev/sdc1).
  • Copy image to device
sudo dd if=path/myImage.img of=/dev/sdX oflag=direct bs=1048576 status=progress && sync

Example of coping a image inside a Zip file to a device

unzip -p myImageInAZip.zip | sudo dd of=/dev/sdX bs=4M conv=fsync

Split a file by lines

Split the file example.txt every 100 lines (creates output example_split_a, example_split_b, ...)

split -l 100 example.txt example_split_

Hash of a file

see OpenSSL

Find binary files

find -type f -executable -exec file -i '{}' \; | grep 'x-executable; charset=binary'

Find largest directories

du -hsx * | sort -n -r | head -n 10

Errors and problems

/tmp directory is full

Solution

Increase the size

mount -o remount,size=6G /tmp/

See also