Linux: Difference between revisions

From indicium
Jump to navigation Jump to search
Stefan (talk | contribs)
Stefan (talk | contribs)
Line 164: Line 164:
After doing the steps above, do:
After doing the steps above, do:
  sudo cp /home/stefan/.config/monitors.xml /var/lib/gdm3/.config
  sudo cp /home/stefan/.config/monitors.xml /var/lib/gdm3/.config
==== Install latest Nvidia drivers from Nvidia (instead of Debian) ====
https://linuxcapable.com/install-nvidia-drivers-on-debian/


=== Restart Gnome ===
=== Restart Gnome ===

Revision as of 10:20, 4 October 2024

Debugging Linux applications

LDD

Linux LDD is just a wrapper for:

LD_TRACE_LOADED_OBJECTS=1 <cmd>

Core dumps

To check core dump handler:

cat /proc/sys/kernel/core_pattern
|/usr/sbin/coredump-handler %h_%t_%p.core

Valgrind

valgrind --trace-children=yes --leak-check=full --show-leak-kinds=all <binary>

Dual boot with Windows

Installation

First install Windows as usual, then boot (in UEFI mode) from Debian netinst usb stick and install Debian. Install grub on your primary disk drive.

Repair when computer only boots into Windows

Download the Debian Live standard ISO from here: https://cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/

Use Rufus to write it to a bootable USB stick: https://rufus.ie

In Rufus, select your USB drive and the downloaded Debian ISO.

Make sure you select Partition schema as GPT and Target system as UEFI (non CSM). Hit "Start" and when prompted choose to write the image in DD mode!

Now insert the USB stick in your broken computer and boot it while hitting a magic key (such as F12) to enter BIOS boot menu. Select to boot your USB stick in UEFI mode.

Once Debian Live has started up, follow the instructions here: https://wiki.debian.org/GrubEFIReinstall#Using_A_Live_CD.2FUSB_To_Fix_Your_Current_System

In short, first you need to figure out your target system root and EFI partitions:

sudo fdisk -l
...
Device              Start        End    Sectors   Size Type
/dev/nvme0n1p1       2048    1394687    1392640   680M EFI System
/dev/nvme0n1p2    1394688    1656831     262144   128M Microsoft reserved
/dev/nvme0n1p3    1656832 1764220927 1762564096 840.5G Microsoft basic data
/dev/nvme0n1p4 1969020928 1971048447    2027520   990M Windows recovery environment
/dev/nvme0n1p5 1971048448 1997713407   26664960  12.7G Windows recovery environment
/dev/nvme0n1p6 1997715456 2000408575    2693120   1.3G Windows recovery environment
/dev/nvme0n1p7 1764220928 1936101375  171880448    82G Linux filesystem
/dev/nvme0n1p8 1936101376 1969020927   32919552  15.7G Linux swap

So in this case our EFI partition is /dev/nvme0n1p1 and our rootfs partition is /dev/nvme0n1p7.

Then do something like:

sudo mount /dev/nvme0n1p7 /mnt/
sudo mount /dev/nvme0n1p1 /mnt/boot/efi
for i in /dev /dev/pts /proc /sys /sys/firmware/efi/efivars /run; do sudo mount -B $i /mnt$i; done
sudo chroot /mnt
sudo grub-install /dev/nvme0n1
sudo update-grub

Repair when computer only boots into Linux

Most probably this is due to that os-prober was not run during your last update-grub session.

In Linux, edit the file /etc/default/grub and add the following line:

GRUB_DISABLE_OS_PROBER=false

Also make sure you have the os-prober command installed:

sudo apt install os-prober
sudo os-prober

Then run:

sudo update-grub

Configure grub to always boot into your last selected OS

In Linux, edit the file /etc/default/grub and add the following lines:

GRUB_DEFAULT=saved
GRUB_SAVEDEFAULT=true

Configuring Linux

Users

Add user stefan to group sudo:

sudo adduser stefan sudo

Change console language to english

sudo dpkg-reconfigure locales

Then select en_US.UTF-8 (plus your local language), and then on next screen set the default language.

Secure Boot

If you want to run Linux, but you also dual boot into Windows and need to have your BIOS set up to boot in Secure Boot (for instance if you play Valorant in Windows), then you have two choices.

  1. Boot Windows in secure mode but Linux in insecure mode.
  2. Boot both Windows and Linux in secure mode.

Option 1

To get the first option working, you need to install Fedora/Nobara with UEFI enabled, but secure boot disabled in BIOS. If you at this point enable secure boot, Linux will not boot (complain about shim error).

Boot up with secure boot disabled and do:

sudo mokutil --disable-validation

Follow the instructions and then reboot your PC and choose to "Change Secure boot state" as described here: https://wiki.ubuntu.com/UEFI/SecureBoot/DKMS

If you do this, you can install GRUB2 and enable Secure Boot in your BIOS again. If you now boot into GRUB2, you can select to boot Windows (which will then properly boot up in Secure Mode) or you can choose to boot Linux which will then boot up in insecure mode.

Option 2

If you want to have also Linux booting up in secure mode, you can do that as well, but it requires a bit more work. There is a good guide here: https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#PreLoader

Basically you need to:

1 Create your own MOK key

openssl req -newkey rsa:4096 -nodes -keyout mok.key -new -x509 -sha256 -days 3650 -subj "/CN=my Machine Owner Key/" -out mok.crt
openssl x509 -outform DER -in mok.crt -out mok.cer

2 Sign the vmlinuz file in your /boot/ folder with this key

sbsign --key mok.key --cert mok.crt --output /boot/vmlinuz-linux /boot/vmlinuz-linux

3 Enroll your key into your BIOS with mokutil

mokutil --import mok.cer

Then Linux will boot up fine in Secure Mode. You will however have 2 issues:

  • Whenever a new kernel is installed on your system, you need to sign it with your own MOK key, otherwise it will not boot.
  • You can not install third party drivers such as the Nvidia graphics drivers unless you sign also them with your own MOK key.

Graphics drivers

Disable Nvidia drivers and enable Intel embedded HD graphics driver

sudo apt-get purge nvidia-*
dpkg --list | grep nvidia

Make sure the list of nvidia packages is empty

sudo apt-get install --reinstall xserver-xorg-video-intel libgl1-mesa-glx libgl1-mesa-dri mesa-utils xserver-xorg-core
sudo dpkg-reconfigure xserver-xorg
sudo update-alternatives --remove gl_conf /usr/lib/nvidia-current/ld.so.conf
reboot

Disable Nvidia drivers and enable nouveau drivers

sudo apt-get purge nvidia-*
dpkg --list | grep nvidia

Make sure the list of nvidia packages is empty

sudo apt-get install --reinstall xserver-xorg-video-nouveau

Disable nouveau drivers and enable Nvidia drivers

Option 1, using package manager:

sudo apt-get install --reinstall nvidia-driver

or if your graphics card is not supported by the latest drivers (you can install the nvidia-detect package and run it to get this information):

sudo apt-get install --reinstall nvidia-legacy-390xx-driver

Option 2, using latest official NVidia driver:

  • Go to [1] and download the latest Linux driver for your system.
  • Switch to console mode (Ctrl+Alt+F1), kill any Xorg process (or mask/stop them with systemctl if you run systemd)
  • Run the NVidia installer as root
systemctl mask gdm
systemctl stop gdm
killall Xorg
sudo ./NVIDIA-<driver-version-name>.run

Display configuration set in nvidia-settings is not retained after boot

This is a conflict between nvidia-settings and Debians internal settings application.

  • Remove ~/.config/monitors.xml (this is Debians settings)
  • Reboot
  • Set everything up the way you want with nvidia-settings
  • Go to Settings - Display
  • Do some minor change that brings up the "Apply" button and press it
  • Current settings are now saved to ~/.config/monitors.xml and will be used during next boot.

Display configuration for gdm3

After doing the steps above, do:

sudo cp /home/stefan/.config/monitors.xml /var/lib/gdm3/.config

Install latest Nvidia drivers from Nvidia (instead of Debian)

https://linuxcapable.com/install-nvidia-drivers-on-debian/

Restart Gnome

Hit ALT+F2, then you get a "gnome run command" thingy. Enter "r" and hit enter!

Switch default compiler

(cc --version;c++ --version;gcc --version;g++ --version) | grep Debian
 cc (Debian 6.1.1-11) 6.1.1 20160802
 c++ (Debian 6.1.1-11) 6.1.1 20160802
 gcc (Debian 6.1.1-11) 6.1.1 20160802
 g++ (Debian 6.1.1-11) 6.1.1 20160802

sudo apt-get install gcc-4.9 g++-4.9

gcc-4.9 --version
 gcc-4.9 (Debian 4.9.3-14) 4.9.3

g++-4.9 --version
 g++-4.9 (Debian 4.9.3-14) 4.9.3

gcc-5 --version
 gcc-5 (Debian 5.4.1-1) 5.4.1 20160803

g++-5 --version
 g++-5 (Debian 5.4.1-1) 5.4.1 20160803

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

(cc --version;c++ --version;gcc --version;g++ --version) | grep Debian
 cc (Debian 4.9.3-14) 4.9.3
 c++ (Debian 4.9.3-14) 4.9.3
 gcc (Debian 4.9.3-14) 4.9.3
 g++ (Debian 4.9.3-14) 4.9.3

To go back:

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Console is not filling the screen completely

This may be due to that the console thinks it is connected to a different display device than it actually is. This can be fixed by adding kernel command line parameters to disable that specific video mode.

ls /sys/class/drm
card0@  card0-DisplayPort-1@  card0-DisplayPort-2@  card0-DisplayPort-3@  card0-HDMI Type A-1@  card0-HDMI Type A-2@  card0-LVDS-1@  card0-SVIDEO-1@  card0-VGA-1@  controlD64@  version

Now check the different "modes" and "connected" and "enabled" nodes under each device to figure out which needs to be disabled. Then add to your kernel command line things such as

video=LVDS-1:d video=SVIDEO-1:d video=TV-1:d

More information: https://bbs.archlinux.org/viewtopic.php?id=94990

udev

udev rules

Some sample udev rules which might come handy:

KERNEL=="tty*", GROUP="dialout", MODE="0660"
SUBSYSTEM=="gpio", GROUP:="gpio", MODE:="0660"
SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="XXXXXXXX", SYMLINK+="ftdiserial2usb"

Use %m for the device number in symlink names.

udev info

To query a device about its properties (which can be used in udev rules):

udevadm info --path=/sys/class/leds/example --query=all --attribute-walk
udevadm info --name /dev/sda
udevadm info -a /dev/sda

More info: https://linux.die.net/man/7/udev

udev debugging

Command to reload udev rules:

sudo udevadm control --reload-rules && sudo udevadm trigger

Command to test udev rules:

udevadm test --action=add /class/gpio

Screen

How to make the scrollback work in screen? Add the following to your .screenrc file:

termcapinfo xterm ti@:te@


LVM

How to extend your LVM disk

Investigate your current setup with:

lsblk

Then you can perform operations like these (which reduces size of live-root with 50GB and extends size of live-home with 50GB):

lvreduce -r -L -50G /dev/mapper/nobara_localhost--live-root
lvextend -r -L +50G /dev/mapper/nobara_localhost--live-home

Linux performance

http://www.brendangregg.com/

Measure elapsed time with precision in bash scripts

date +%s.%N
<cmd>
date +%s.%N

Kernel

Update to newer kernel on Debian

Add the backports repository to apt (/etc/apt/sources.list or something similar). Add these as new lines, do not change the existing ones:

deb [arch=amd64] http://debian.lth.se/debian bookworm-backports main contrib non-free non-free-firmware
deb-src [arch=amd64] http://debian.lth.se/debian bookworm-backports main contrib non-free non-free-firmware

Then do:

sudo apt update

Then just install the latest linux-image-amd64, but since backports is lower prioritized, you need to specify that you want the backport version:

sudo apt install linux-image-amd64/bookworm-backports

If you then get messages about missing firmware (due to kernel being newer than linux-firmware-nonfree):

Possible missing firmware /lib/firmware/i915/mtl_gsc_1.bin for module i915
Possible missing firmware /lib/firmware/i915/dg2_huc_gsc.bin for module i915
Possible missing firmware /lib/firmware/i915/mtl_huc_gsc.bin for module i915
Possible missing firmware /lib/firmware/i915/mtl_guc_70.bin for module i915

you can manually:

Update to newer kernel on Ubuntu

Simply install the correct version of linux-generic-hwe:

sudo apt install linux-generic-hwe-20.04

or

sudo apt install linux-generic-hwe-22.04

Clean out journalctl logs

journalctl --flush --rotate --vacuum-time=30d

Set up public key SSH authentication

On your client PC which you want to connect from, run:

ssh-keygen

Accept all the defaults. Use a passphrase if you want to (not for automated setups).

Then send your key to the server for your account:

ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.26

If you can not use ssh-copy-id (such as if adding a key for user root which can not log in using password), then simply cat they key from .ssh/id_rsa.pub on your client to the .ssh/authorized_keys file on your server.