Linux Notes

Linux Notes, linux stuff, bash, commands reference, Linux How-to's, etc.


Last Updated: August 10, 2018 by Pepe Sandoval



Want to support us?

If you find the information in this page useful and want to help us you can make a donation

Use PayPal

This will help us to create more stuff and fix the existent content... or probably your money will be used to buy beer


Linux Notes

/usr

User usable programs and data. User System Resources

  • /usr/include - Contains 'header files', needed for compiling user space source code. Headers like stdio.h, stdlib.h stdint.h, etc. are here.
  • /usr/lib - Contains program libraries for the normal user-programs, that mostly can be found under /usr/binand /usr/bin
  • /usr/local/lib - are the libraries for locally installed programs and packages (things you've compiled and installed from source-packages yourself)

/lib

Contains those shared libraries needed to boot the system and run the commands/programs in the root filesystem (binaries in /bin and /sbin folders). Kernel modules (device drivers) are under /lib.

/sys

  • /sys/module - Its a sysfs directory hierarchy containing information on currently-loaded modules

/proc

  • /proc is a "pseudo" file system that exists only in kernel memory and is used primarily for querying various per-process as well as kernel statistics, contains processes as subdirs

    • /proc/iomem - This file shows you the current map of the system's memory for each physical device:
  • /proc/<pid>/status: check status of a process

/etc

  • /etc/shells - shows which command shells are installed on your Linux system.

Shell

  • A shell is the term used to refer to a program that interprets and executes command, is used as a command line user interface
  • There are built-in commands which are used by default and other programs/commands with the same name. Use command type to know how a command will be interpreted
    • type -a <command>
    • type -a echo
  • For built in commands you can do help <command>to get help information
  • enable -a: show built-in shell commands
  • To check which terminal you are using echo $0 or echo $SHELL
  • Bash ( Bourne Again Shell ) is a Unix shell and command language and its the default shell on most Linux based distros
Expansions
  • brackets: mkdir -p testdirs/{test1,test2,test3}
  • single character ?: ls tes?t
  • any string *: ls tes*
  • enclosed characters: ls test[123]
  • commands substitution use $(): echo "the current dir is: $(pwd)"

grub

  • To change the grub boot default option just change the GRUB_DEFAULT=0 on the /etc/default/grub file and the run update-grub

loops

for
  • one-line for: for i in `seq 1 10`; do echo $i; done
  • using variable as limit
    for (( i=0; i <= $upperlim; i++ )); do
       echo "$i"
    done
while
  • decrement counter with while loop
    while [ $cnt -gt 0 ]
    do
      echo "Welcome $cnt times"
      cnt=$(( $cnt - 1 ))
    done

Commands

printf

  • printf "Message no color, \033[0;36mMessage color\033[0m\n"

ssh

  • ssh -t <user>@<host> '<bash_command_here>; <shell_name>'
    • ssh -t jose@127.0.0.1 'echo Hello; bash'

Enable SSH

  • Install sudo apt-get install openssh-server
  • Check status sudo service ssh status
  • Restart service sudo service ssh restart
  • Allow connections on port 22: sudo ufw allow 22

scp

  • scp -r <path_to_local_folder> <host_user_here>@<host_dest_ip>:<host_dest_folder>
    • scp -r content/ root@10.219.85.238:/home
    • scp -P 40XX -r content/ root@10.219.85.238:/home

rsync

  • rsync <source_folder> <dest_folder>
    • rsync -vP <source_file> <dest_file>
    • rsync -azhP --exclude=".*" <source_folder> <dest_folder>
    • rsync -azhP --exclude=".*" <source_folder> <dest_user>@<dest_ip>:<dest_folder>

ssh keys

  • ssh-keygen -t rsa
  • eval ssh-agent -s
  • ssh-add -l
  • ssh-add ~/.ssh/id_rsa
  • ssh-keygen -i -f id_dsa_1024_a.pub >> ~/.ssh/authorized_keys

openssl

  • openssl enc -aes-256-ofb -in user.md -out user.enc
  • openssl enc -aes-256-ofb -d -in user.enc -out user.md

SHA - md5

  • md5sum <path_to_file>

key

apt-get install debian-archive-keyring apt-key update

date

  • date --set="YYYYMMDD HH:MM:SS"
    • date --set="20150716 14:36:00"

find

  • find <path_to_search> -name <search_filename_here>
    • find /usr/include -name stdio.h
  • find <path_to_search> -type <type_indicator> -iname "search_pattern_here"
    • find ./ -type f -iname "myfile"

grep

  • grep -rIn <pattern_to_search> <path_to_search>
    • grep -rIn "#include <stdio.h>" ./
    • grep -rIn main ./

version & architecture

  • cat /etc/issue
  • lsb_release -a
  • uname -a
  • dpkg --print-architecture
  • arch

System statistics

  • vmstat - reports memory-usage statistics
    • vmstat <interval> <num_reports> e.g. vmstat 1 3
  • netstat - reports statistics for network interfaces
  • iostat - reports I/O usage for disk

Tracing

  • strace - traces system calls invoked by a process
  • tcpdump - collects network packets

dmesg

  • dmesg -wH >> kernel.log
  • dmesg -wH
  • dmesg -C

kill process

Check process ID (pid with) ps command

  • Check ssh connections ps -aux | grep "sshd"
  • Check tty connections ps -dN | grep pts/0
  • Check user processes ps -fu
  • Check real time processes top

Use kill command to kill process

  • kill -l: show sigspec list
  • kill <pid>
  • kill 9 <pid> or kill -SIGKILL <pid>
  • kill -<signal name/number> <pid>

Signals Table

Sig Name Number Description
SIGHUP 1 Hangup (POSIX)
SIGINT 2 Terminal interrupt (ANSI)
SIGQUIT 3 Terminal quit (POSIX)
SIGILL 4 Illegal instruction (ANSI)
SIGTRAP 5 Trace trap (POSIX)
SIGIOT 6 IOT Trap (4.2 BSD)
SIGBUS 7 BUS error (4.2 BSD)
SIGFPE 8 Floating point exception (ANSI)
SIGKILL 9 Kill (can't be caught or ignored) (POSIX)
SIGUSR1 10 User defined signal 1 (POSIX)
SIGSEGV 11 Invalid memory segment access (ANSI)
SIGUSR2 12 User defined signal 2 (POSIX)
SIGPIPE 13 Write on a pipe with no reader, Broken pipe (POSIX)
SIGALRM 14 Alarm clock (POSIX)
SIGTERM 15 Termination (ANSI)
SIGSTKFLT 16 Stack fault
SIGCHLD 17 Child process has stopped or exited, changed (POSIX)
SIGCONT 18 Continue executing, if stopped (POSIX)
SIGSTOP 19 Stop executing(can't be caught or ignored) (POSIX)
SIGTSTP 20 Terminal stop signal (POSIX)
SIGTTIN 21 Background process trying to read, from TTY (POSIX)
SIGTTOU 22 Background process trying to write, to TTY (POSIX)
SIGURG 23 Urgent condition on socket (4.2 BSD)
SIGXCPU 24 CPU limit exceeded (4.2 BSD)
SIGXFSZ 25 File size limit exceeded (4.2 BSD)
SIGVTALRM 26 Virtual alarm clock (4.2 BSD)
SIGPROF 27 Profiling alarm clock (4.2 BSD)
SIGWINCH 28 Window size change (4.3 BSD, Sun)
SIGIO 29 I/O now possible (4.2 BSD)
SIGPWR 30 Power failure restart (System V)

Build packages

  • apt-get install debhelper dpkg-dev binutils
    • dpkg-buildpackage -rfakeroot -Tclean
    • dpkg-buildpackage -b
  • dnf install fedora-packager after build check -> /root/rpmbuild/RPMS/
    • rpmbuild -bb --build-in-place specs/*.spec

Keyboard

  • setxkbmap -query
  • alt+shift: to toggle keybords
  • setxkbmap -option grp:alt_shift_toggle us,es
  • cat /usr/share/X11/xkb/rules/xorg.xml

Modules

  • lsmod
  • insmod <module>.ko
  • insmod <module>.ko <param1_name>=<param1_value> <param2_name>=<param2_value>
  • rmmod <module>.ko
  • modinfo <module>.ko

PCI/PCIe

  • lspci -nn
  • lspci -d 8086:19f2 -x -v
  • lspci -d 8086:19f2 -xxx -v
  • Read config space byte/word/long:
    • setpci -s <bus>:<dev>.<fun> <offset_hex>.<Size>
      • setpci -s 01:00.0 2.W
      • setpci -s 01:00.0 0.L
      • setpci -s 01:00.0 8.B
        • setpci -d <vid>:<did> <offset_hex>.<Size>
      • setpci -s 0101:0101 2.W (2 bytes)
      • setpci -s 0101:0101 0.L (4 bytes)
      • setpci -s 0101:0101 8.B (1 byte)
  • Write config space byte/word/long is equal to read but use = and the value to write
    • setpci -s <bus>:<dev>.<fun> <offset_hex>.<Size>=<value>
    • setpci -d <vid>:<did> <offset_hex>.<Size>=<value>

dpkg

  • dpkg -i <package_name>
  • dpkg -r <package_name>
  • dpkg --get-selections: Show installed packages.
  • dpkg -l: Show installed packages.
  • dpkg -l <package_name>: Show info of a package
  • dpkg -c <package_name>.deb: Show files in a package
  • dpkg-deb --contents <package_name>.deb: Show info and files in a package
  • dpkg -S <path_to_file>: shows which package installed this file

apt

  • apt-get remove <package_name>
  • apt-get install <package_name>=<package_version>: Install a certain version
  • apt-get search cache <package_name>: search available packages
  • apt-cache show <package_name>: show available package information
  • apt-cache madison <package_name>: show available versions of a package
  • apt-cache pkgnames --all-names: Show available packages.
  • Search package that contains a file
    • apt-get install apt-file
    • apt-file update
    • apt-file search <file>

Setup mail services

  1. Update repos then install SSMTP and mail utilitites
    apt-get update
    apt-get install ssmtp mailutils
  1. Edit the SSMTP configuration file /etc/ssmtp/ssmtp.conf. Mandatory lines:
    root=postmaster
    mailhub=smtp.gmail.com:587
    hostname=<HOSTNAME_OF_DEVICE_HERE>
    AuthUser=<YOUR_GMAIL_EMAIL_HERE>
    AuthPass=<YOUR_GMAIL_PASSWORD>
    UseSTARTTLS=YES

You will need root useer permissions to edit /etc/ssmtp/ssmtp.conf You can use the hostname command to get the hostname of your device

  1. Edit the SSMTP aliases file /etc/ssmtp/revaliases add a line for each user that will send emails
    root:<YOUR_GMAIL_EMAIL_HERE>:smtp.gmail.com:587
    pi:<YOUR_GMAIL_EMAIL_HERE>:smtp.gmail.com:587
  1. Set permissions for SSMTP confi file: chmod 774 /etc/ssmtp/ssmtp.conf

  2. Test email command: echo "Test text" | mail -s "Test Mail" mailuser@example.com

  3. Add MAILTO variable at the top of the crontab file MAILTO="<dest_email>" add it as empty to disable cron mail service

Reference: Sending emails from the Raspberry Pi

Check Memory and HD usage

RAM

  • free -m

HD

  • df -h
  • df -H
  • df
  • fdisk -l
  • du -h --max_depth=1

Other

  • startxfce4
  • sudo shutdown -h now
  • readelf --debug-dump=line <path-to-shared-lib.so>
  • sudo chown -R <username>:<group> <directory>
  • sudo passwd <user_name>
  • aspell -c file.txt
  • sudo dpkg-reconfigure tzdata
  • ln -s -f <existing_folder_or_file> <path_and_name_of_existing_soft_link>
  • ln -s <existing_folder_or_file> <path_and_name_of_soft_link>

tar

.tar.gz
  • tar: tar -czvf name_for_tar.tar.gz /dir/to/targz/
  • untar: tar -xzvf tar_file.tar.gz
  • untar: tar -xzvf tar_file.tar.gz -C /output/dir
.tar:
  • tar: tar -cvf name_for_tar.tar /dir/to/tar/
  • untar: tar -xvf tar_file.tar
.gz:
  • compress: gzip -9 index.php
  • uncompress: gzip -d index.php.gz
.zip:
  • zip: zip name_for_zip.zip folder
  • unzip: unzip zip_file.zip

network

  • sudo service networking stop
  • sudo service networking start
  • sudo service networking restart
  • w
  • netstat
curl (connet to URL)
  • curl <url>
  • curl -I <url>
nslookup
  • nslookup <web_site>
traceroute
  • traceroute <web_site> : response are not acumulative it measures an approx of time from init poin to the reported point
ip
  • ip addr show - view a list of your network devices
  • sudo ip addr add <ip_addr_here> dev <dev_name>
  • sudo ip link set <dev_name> up
  • sudo ip link set <dev_name> down
  • ip route show
Set Static IP
  1. Use nmcli to get the gateway, IP Address and DNS info
    • nmcli device show <interfacename> | grep IP4
    • nmcli device show enp0s3 | grep IP4
  1. Edit the interfaces file sudo vim /etc/network/interfaces. Add the following lines with the appropriate information you got with nmcli

    auto enp0s3
    iface enp0s3 inet static
    address 192.168.0.10
    netmask 255.255.255.0
    gateway 192.168.0.1
    dns-nameservers 192.168.0.1
  2. Edit /etc/NetworkManager/NetworkManager.conf:

    [main]
    plugins=ifupdown,keyfile
    #dns=dnsmasq
    
    [ifupdown]
    managed=true
route
  • route -n - view routing table
arp
  • arp -a - view local arp table
hostname
  • hostname is stored on /etc/hostname and /etc/hosts files

apache

  1. Installs Apache, utilities, configuration files, and other items
    • sudo apt-get install apache2
    • apt-cache show apache2
  1. Config files in /etc/apache2
    • main config file /etc/apache2/apache2.conf

redirect >, >>, <, <<

  • output funcions write to the stdout
  • input function read from the stdin

pipe |

  • stdout of the programm on the left will be used as teh stdin of the program on the right

xrdp

  • sudo apt install xrdp

VNC vnc4server

  1. install dependencies: sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal

  2. install VNC server:

    • vnc4server: sudo apt-get install vnc4server
    • x11vnc: sudo apt-get install x11vnc
  1. Create VNC server file. vnc4server: sudo vim /etc/systemd/system/vncserver@:1.service with the following content:

    [Unit]
    Description=Start VNC server at startup
    After=syslog.target network.target
    
    [Service]
    Type=forking
    #YOUR USER HERE!
    User=pepe
    PAMName=login
    #YOUR USER HERE!
    PIDFile=/home/pepe/.vnc/%H%i.pid
    #ExecStartPre=/usr/bin/vncserver -kill %i > /dev/null 2>&1
    #Change your resolution here
    ExecStart=/usr/bin/vncserver -depth 16 -geometry 1920x1080 %i
    ExecStop=/usr/bin/vncserver -kill %i
    
    [Install]
    WantedBy=multi-user.target
  1. Reload daemon, start VNC and set password using the following two commands
    sudo systemctl daemon-reload
    vncserver # Set password
  1. Edit VNC startup file to look like this:

    #!/bin/sh
    export XKL_XMODMAP_DISABLE=1
    unset SESSION_MANAGER
    unset DBUS_SESSION_BUS_ADDRESS
    
    [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
    [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
    xsetroot -solid grey
    vncconfig -iconic &
    
    gnome-session --session=ubuntu-2d &
    gnome-panel &
    gnome-settings-daemon &
    metacity &
    nautilus &
    gnome-terminal &
  1. Enable VNC server at startup sudo systemctl enable vncserver@:1.service
    • Manually start VNC server command: sudo systemctl start vncserver@:1.service
Desktop share native app
  1. On Ubuntu open "Desktop Share" app, enable remote connections and set password
  2. On the terminal execute: gsettings set org.gnome.Vino require-encryption false
Want to support us?

If you find the information in this page useful and want to help us you can make a donation

Use PayPal

This will help us to create more stuff and fix the existent content... or probably your money will be used to buy beer