Laboratorium Komputerowe Progmar

We are using cookies in the page. If you use the page you agree for the cookies.      Close

Ubuntu developer commands

Counts system calls and displays execution time of the calls: strace -c dd if=/dev/urandom of=/dev/null count=1000

Runs specified program and saves to a strace.out file all system calls that the program used: strace -o strace.out -f ./program

Creates a file of size 4MB, formats it as a swap file and attaches it as an additional swap space: dd if=/dev/zero of=/tmp/swap.dat bs=1k count=4096
mkswap /tmp/swap.dat
swapon /tmp/swap.dat

Turns on core file dumps: ulimit -c unlimited

Displays PID of the command cmd: pgrep -n cmd

Displays current working directory with all symlinks replaced by their's destinations: pwd -P

Makes gcc not to delete an assembler file which the C code was translated to. The file is placed in the /tmp directory (ex. /tmp/ccQrSAQZ.s): gcc -v -c main.c Compiles an assembler file into the object file: as -V -Qy -o main.o /tmp/ccQrSAQZ.s

Lists all opened files in the operating system: lsof

Lists all opened by a process of specified PID files in the operating system: lsof -p 1234

Displays processes that have specified file opened: fuser some_file.txt

Kills all processes that have specified file opened: fuser -k -KILL some_file.txt

Displays all processes that have opened any files located on the specified filesystem: fuser -m /media/flash

Prints specified file's names and attributes in symbolic and numeric formats: stat --format="%-15n %A,%a" /dev/log dev/sda /dev/zero

Prints file last access time in seconds since 1970-01-01 00:00:00 : stat -c %X file.txt

Prints all at least 4 character strings that are found in specified file: strings filename.dat

Prints all at least 4 character (in bytes) UTF-8 strings that are found in specified file: strings -eS filename.dat

Prints hexadecimal content of the file, each word is built of 16-bit, in the right column it prints counterparted ascii characters: xxd filename.dat

Prints hexadecimal content of the file, each word is built of 32-bit, in the right column it prints counterparted ascii characters: xxd -g4 filename.dat

Prints hexadecimal content of the file, each word is built of 16-bit, in the right column it does not print counterparted ascii characters: hexdump filename.dat

Prints hexadecimal content of the file, each word is built of 32-bit, in the right column it neither prints counterparted ascii characters nor prints offsets in the left column: hexdump -e '6/4 "%8x "' -e '"\n"' filename.dat

Displays shared memory objects: ipcs -m -m

Displays shared memory objects and information about which process (PID) has created which shared memory object and which process bound to the given object last or removed binding to it: ipcs -p

Checking for which processes use given shared memory object ([shmid] is shared memory object's id): ipcs -m
ipcs -m -p
lsof | head -1 ; lsof | grep [shmid]

Displays additional information about specified shared memory object: ipcs -m -i [shmid]

Prints System V message queues that exists in the system: ipcs -q

Displays additional information about specified message queue (msqid is the numeric identifier of the message queue): ipcs -q -i msqid

Removes message queue of specified identifier msqid: ipcrm -q msqid

Prints System V seaphores that exists in the system: ipcs -s

Displays additional information about specified semaphore (semid is the numeric identifier of the semaphore): ipcs -s -i semid

Removes semaphore of specified identifier semid: ipcrm -s semid

Checks if process is working (if the output is empty then the process is working): kill -0 PID

Removes shared memory object of specified identifier shmid: ipcrm -m [shmid]

Prints pid, effective user id, real user id and the command which started the process for every process in the system: ps -eo pid,euser,ruser,comm

Displays system call that has blocked the process on a semaphore: ps -o wchan -p PID

Mounts POSIX queue filesystem: sudo mount -t mqueue none /mnt/mqs Displays detailed information about specified POSIX queue: cd /mnt/mqs
touch myqueue
cat myqueue

To remove specified POSIX queue just remove its file from the filesystem by using rm command: rm myqueue

ls -l /dev The result of above command is list of all files in /dev catalog together with some info abot the files. Each file description takes one line in the resultset. If the line for a given file starts with a letter: b (block), c (character), p (pipe) lub s (socket), then this means that the file is a device (b, c) or a special file (p, s).

Dispays detailed information about specified device, including it's path in /sys/devices directory: udevadm info --query=all --name=/dev/sda

Monitoring uevent messages (about plugging or unplugging a device): udevadm monitor

Monitoring uevent messages that comes from kernel and refers changes in SCSI subsystem: udevadm monitor --kernel --subsystem-match=scsi

Displays a list of SCSI devices (it looks through the sysfs filesystem: /sys): lsscsi

Displays a list of USB devices: lsusb

Conventions in naming devices in the system:

/dev/sd* - hard drives (ex. /dev/sda, /dev/sdb) Main partitions on a hard drive /dev/sda are represented by files: /dev/sda1 .. /dev/sda4. If you want to create logical partitions then one of the four main partitions must be an extended partition. Logical partitions placed in the extended partition are represented by following files: /dev/sda5, /dev/sda6, ...
/dev/sr* - CD and DVD drives (ex. /dev/sr0, /dev/sr1) The device files have symbolink links as follows: /dev/cdrom, /dev/cdrom1, /dev/cdrw, /dev/cdrw1, /dev/dvd, /dev/dvdrw, ...
/dev/hd* - PATA hard drives (ex. /dev/hda, /dev/hdb, /dev/hdc, /dev/hdd) These are an aged devices rarely occured now:
/dev/tty*, /dev/pts/*, /dev/tty - Terminals
/dev/tty1 - first virtual console
/dev/tty2 - second virtual console
/dev/tty6 - sixth virtual console
/dev/pts/0 - first pseudoterminal device
/dev/pts/1 - second pseudoterminal device
/dev/tty - terminal appropriate for current process
In Ubuntu system you can switch from the graphical environment to n-th (n from 1 to 6) virtual console by pressing keyboard shortcut CTRL+Fn. Next you can switch between the virtual consoles by pressing keyboard shortcuts Alt+Fn. You can at every moment return to the graphical environment by switching to 7-th virtual console: Alt+F7.
When you are logged into a virtual console then you can to switch to another virtual console by issuing following command: chvt n - (where n is a destination virtual console's number).
/dev/ttyS* - RS-232 serial ports /dev/ttyS0 - counterpart of windows' COM1,
/dev/ttyS1 - counterpart of windows' COM2
/dev/ttyUSB* and /dev/ttyACM* - USB ports /dev/ttyUSB0, /dev/ttyUSB1, ...,
/dev/ttyACM0, /dev/ttyACM1, ...
/dev/lp0 and /dev/lp1 - one-direction parallel ports /dev/lp0 - counterpart of windows' LPT1
/dev/lp1 - counterpart of windows' LPT2
These ports are replaced by USB ports.
/dev/parport0 and /dev/parport1 - two-direction parallel ports
/dev/dsp, /dev/audio, /dev/snd/* - audio devices
ALSA devices' files are located in /dev/snd catalog.
OSS devices' files are /dev/dsp and /dev/audio.

You can create a device with a mknod command. As a parameters to the command you must pass its type, main number and a minor number: sudo mknod /dev/sda1 b 8 2 - first partition of a hard disk sda

Forces kernel to reread partition table of specified device: blockdev -rereadpt /dev/sdb

Prints byte number counting from the beginning of the disk, which the specified partition (sda2) starts at: cat /sys/block/sda/sda2/start

Prints list hard disks' partitons with identifiers UUID associated to them and theirs types: sudo blkid For a VFAT partitions theirs UUID are theirs serial numbers:

To mount a partition according to its UUID, ussue following command: mount UUID=..... /mnt/disk

Umounts / directory and mounts it again using /etc/fstab settings. It is useful when the / directory was mounted read-only and you want to mount it with full access rights: mount -n -o remount /

Command for checking filesystems, if they have errors or not: fsck It is periodically run called by the operating system. In interactive mode you call it by passing a full path to the checked device, ex: fsck /dev/sda5 Inspected filesystem must be umounted otherwise it could cause filesystem errors and loss of data. There is an exception from the rule: if you have run the system in a single user mode and mounted the / filesystem in read-only mode then you can run the fsck command for the filesystem safely.

Inspecting filesystem without attempting to repair errors found: fsck -n

Replacing corrupted superblock with its backup copy residing in a block with specified number: fsck -b numer

If the filesystem was created with default options, then the command below will show a list of block's numbers that store backup copies of a superblock: mke2fs -n

Creating and activating a swap partition: mkswap /dev/hda5
swapon /dev/hda5

Disabling a specified swap partition: swapoff /dev/hda5

Preparing and activating a file as a swap space: dd if=/dev/zero of=swap_file bs=1m count=megabytes_count
mkswap swap_file
swapon swap_file

Disabling a swap partition or a swap file: swapoff swap_partition_or_swap_file

Process of a system startup:

  1. Checking the processor.
  2. Checking the memory.
  3. Recognition of a device's bus.
  4. Detection of a devices.
  5. Configuring a complementary kernel subsystems (network, etc.)
  6. Mounting a root filesystem.
  7. Running a user space (init process: init.d or upstart or systemd).

Kernel parameters used during booting can be found in a file /proc/cmdline: cat /proc/cmdline ex.:
BOOT_IMAGE=/boot/vmlinuz-3.2.0-90-generic-pae root=UUID=4b43b290-bce6-478e-a245-997995a92c13 ro quiet splash vt.handoff=7
Parameter ro is used for the command fsck to safely inspect a root filesystem during booting. When the checking will be completed then the init process will remount root filesystem to have full access rights.

GRUB - command line (it is accessed by pressing 'c' key in the GRUB's menu):

ls - prints devices and disk's partitions found. Msdos keyword in the partition name means that there is a MBR partition table on the disk. Gpt keyword in the partition name means that there is a GPT partition table on the disk.
ls -l - prints detailed information on devices found, ex. UUID ids, filesystems types, last modification time, etc.
echo $root - prints main partition's name
ls ($root)/ lub ls (hd0,msdos1)/ - prints file list on the main partition
ls ($root)/boot - prints /boot directory content on the main partition
set - displays list of GRUB variables. One of the most important variables is a $prefix variable. It holds the location of GRUB's configuration and a list of files it is accessing.

Command for generating a GRUB's configuration. It dumps it to the standrad output: grub-mkconfig Saving generated configuration to a specified file: grub-mkconfig -o /boot/grub/grub.cfg

Prints the system runlevel: who -r

Guessing the kind of working init process in the system:

  1. If there exists in the system directories: /usr/lib/systemd and /etc/systemd, then it is a systemd.
  2. If there exists in the system directory /etc/init which hcontains a lot of *.conf files, then it is an Upstart
  3. If it is neither 1 nor 2 and there exists in the system /etc/inittab file, then it is most probably a System V init program.
  4. You can also use the manual page to determine init process kind: man 8 init

Prints a list of active units (services) in the system: syssystemctl list-units

Writes to a system log a specified message and its type: logger -p It is a message written to a system log

Prints groups that current user belongs to: groups

Lists all processes working in the system including threads: ps axm -o pid,tid,command

Monitoring specified processes: top -p PID_1 -p PID_2 ...

Measures the processor time that the command consumed: /usr/bin/time polecenie

Prints the system uptime and the average processor load: uptime

Prints information on major and minor processes' page faults: ps -o pid,min_flt,maj_flt,command

Monitoring memory and processor usage in 5-seconds interval: vmstat 5

Prints statistics of input/output devices and processor usage: iostat

Prints statistics of only input/output devices usage: iostat -d

Prints detailed information on the disk's usage (together with all theirs partitons): iostat -p ALL

Prints information on the input/output devices usage for all threads running in the system. The results are presented the same as for top command: iotop

Displays processor usage statistics in 1 second interval in four steps: sar 1 4

Displays virtual memory usage statistics in 1 second interval in four steps: sar -r 1 4

Monitors paging and virtual memory operations in 1 second interval in four steps: sar -B 1 4

Displays block devices usage statistics in 1 second interval in one step (-p option causes that in result data there are devices' names instead of theirs major and minor numbers): sar -d -p 1 1

Displays ethernet devices usage statistics in 1 second interval in four steps: sar -n DEV 1 4

Displays ethernet devices errors statistics in 1 second interval in four steps: sar -n EDEV 1 4

Displays NFS filesystem client statistics in 1 second interval in four steps: sar -n NFS 1 4

Displays NFS filesystem server statistics in 1 second interval in four steps: sar -n NFSD 1 4

Displays sockets statistics in 1 second interval in four steps: sar -n SOCK 1 4

Displays all DEV, EDEV, NFS, NFSD, SOCK statistics in 1 second interval in one step: sar -n ALL 1 1

Prints detailed information about x86 (Intel/AMD) processor installed in the system: x86info

Prints statistics that describe source code efficiency coming within cache memory usage: valgrind --tool=cachegrind ./a.out params

Prints dynamic libraries functions calls list, sorted by waiting time for a call: ltrace -c command ex.: ltrace -c dd if=/dev/urandom of=/dev/null count=1000

Displays detailed information (including execution time, -T parameter) about specified (-e parameter) shared library's functions: ltrace -T -e read,write dd if=/dev/urandom of=/dev/null count=1

Lists system calls sorted by waiting time of the call: strace -c command ex.: strace -c dd if=/dev/urandom of=/dev/null count=1000

Attaches strace program to a specified running process: strace -ttt -p PID

Profiling the specified program (the program must be compiled with -pg parameter and without any optimalisation flags): gprof ./progname

As above, but it does not display particular fields descriptions: gprof -b ./progname

Counts how many times particular rows of source code were executed: gcc -fprofile-arcs -ftest-coverage test.c
gcov test.c
less test.c.gcov
rm test.c.gcov test.gc*

Displays a list of processes that have ports in use or that are listening on the ones (-n parameers disables host resolving): sudo lsof -i -n

Displays a process that has specfied port in use: sudo lsof -n -i:port

Displays all processes that are listening on TCP ports: sudo lsof -iTCP -sTCP:LISTEN

Listens on the specified port: netcat -l -p port

Displays a list of domain sockets that are currently in use together with processes that use them: lsof -U

If a program needs during compilation the kernel headers (causes compilation error: Kernel Headers Not Found) then you have to install following package: sudo apt-get install linux-headers-amd64 (for a 64-bit operating system)

Lists all objects and functions that are provided by a specified static library: nm library_name.a

Lists all shared libraries that are required by a specified program: ldd program_name

Lists all shared libraries that specified program was built with but which are not used by it: ldd -u program_name

Compilation and making of the shared object: gcc -shared -fpic -o mylib1.c mylib2.c

Analysis of the memory that are used by specified program: valgrind --tool=memcheck ./program_name

Examination of the specified program about memory leaks: valgrind --quiet --leak-check=full ./program_name

Examination of the specified program about memory usage by its functions: valgrind --tool=massif ./nazwa_programu

Displays information about selected window: xwininfo

Displays X server events: xev

Displays a list of all input devices: xinput --list

Displays properties of the input device with specified identifier ID: xinput --list-props ID

Monitoring the events sent by a D-Bus service in a system mode: dbus-monitor --system

Monitoring the events sent by a D-Bus service in a session mode: dbus-monitor --session

Scans the specified disk drive for a bad blocks: sudo badblocks -n /dev/sda

Displays all libraries that are provided by a pkg-config command: pkg-config --list-all

That is how to inspect computer's hardware:

Processor: cat /proc/cpuinfo
Hardware: sudo dmidecode
sudo lshw sudo lshw -c display (only graphic cards)

Sample source files that are in use below: ------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>

void message(void);

int main(void) {
    printf("Hello world\n");
    return 0;
#include <stdlib.h>
#include <stdio.h>

void message(void) {
    printf("Hello world from\n");

Compilation with displaying detailed information about program/static library being compiled and linked: gcc -v -o hello hello.c

Displays dependencies (required libraries) for specified program/library: ldd hello

There are static and shared libraries. Static ones (*.a) are ar archives which contain object files (*.o). They can be created as showed below: ar clq libname.a file1.o file2.o file3.o ...

Static libraries' content can be extracted with command: ar x libname.a

Compilator links a program only with that object files from the library which are needed by it. Dynamic libraries (*.so) are not linked with the program. They are loaded to process memory during its startup (just before calling main function). Program only contains information about shared libraries it requires to load them and run properly.
Shared library can be built as follows: gcc -shared -fpic -o dynamic_library.c

Program can be built with any libraries in following way: gcc -o hello hello.c libname.a or so: gcc -o hello hello.c -L ./ -lname -ldynamic_library

Now let's compile the hello program together with shared library it needs: gcc -shared -fpic -o dynamic_library.c
gcc -o hello hello.c

When we run the built application now, we sticks to following error message: ./hello: error while loading shared libraries: cannot open shared object file: No such file or directory To run program properly we have to apply the path to the directory with our library in system variable LD_LIBRARY_PATH, for example: LD_LIBRARY_PATH=. ./hello We can also copy the library to /usr/lib or to /usr/local/lib directory.
In Ubuntu system we can alternatively create a file /etc/ that holds directories containing our libraries: one directory per line. Next we have to refresh the libraries cache by issuing following command: sudo ldconfig Note that in order to it successfully worked the library file must start with lib prefix, ex: libdynamic_library.

Displays information about executable elf's sections size: size hello

Displays symbols and theirs size in the object or executable file: nm -S hello

Displays undefined symbols in the object file: nm -u hello.o

Removes symbols from the library file: strip --strip-debug libname

Removes symbols from the executable file: strip --strip-unneeded program

Displays the specified process' memory map: pmap PID

The file is read and processed by the configure script in order to generate proper Makefile file.

Displays successive commands from the Makefile file without executing them: make -n

Makes the compiler to generate warnings: CFLAGS=-Wall ./configure