How To: Setup sudo in Debian

sudo in Debian Linux

Apparently, Debian installer doesn’t install or activate sudo by default. This means that sudo command is not found the only privilege escalation method available is becoming root via su command. Since I like and use sudo daily, I decided to install and setup it on Debian VM.

Install sudo package in Debian

That’s the very first step you’ll need to do: use apt to install sudo. You need to become root before you do it, of course (so you must know root user password for your Debian install):

greys@debian:~$ su -
Password:
root@debian:~ # apt install sudo
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
  sudo
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/1,245 kB of archives.
After this operation, 3,886 kB of additional disk space will be used.
Selecting previously unselected package sudo.
(Reading database … 174742 files and directories currently installed.)
Preparing to unpack …/sudo_1.8.27-1_amd64.deb …
Unpacking sudo (1.8.27-1) …
Setting up sudo (1.8.27-1) …
Processing triggers for man-db (2.8.5-2) …
Processing triggers for systemd (241-5) …
root@debian:~ # sudo
usage: sudo -h | -K | -k | -V
usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] []
usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file …

Configure /etc/sudoers File

/etc/sudoers is the main configuration file for sudo command. It contains list of users and groups that are allowed to become root (or become other users by invoking su command as root).

Here’s the default file in Debian 10 Buster:

root@debian:~ # cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
 
# User alias specification
 
# Cmnd alias specification
 
# User privilege specification
root    ALL=(ALL:ALL) ALL
 
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
 
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d

I’ve highlighted the 3 most important elements of this file at this early stage:

root    ALL=(ALL:ALL) ALL

This is the line that allows you to debug sudo commands as root user.

At this means that any user that belongs to group sudo will also be allowed to use sudo commands:

%sudo   ALL=(ALL:ALL) ALL

Finally, this part includes additional configuration files from /etc/sudoers.d directory:

#includedir /etc/sudoers.d

… this means you don’t have to edit /etc/sudoers file but instead can create a specific file in /etc/sudoers.d and name it self-descriptively, like:

/etc/sudoers.d/web-server-admins

meaning, that this file will contain usernames and privileges required by web-server admins (usually commands like stopping/starting Apache or nginx webserver).

Since this is a very basic tutorial, we don’t have to edit the file at all – just need to add our user (mine is greys, as you remember) to the sudo group and check.

Add user to sudo group

Step 1: let’s make sure sudo is not accessible before we begin

This needs to be run as your regular user, not as root:

greys@debian:~$ sudo -i
[sudo] password for greys:
greys is not in the sudoers file.  This incident will be reported.
greys@debian:~$

Let’s check my groups just to be sure there’s no sudo among them:

greys@debian:~$ id greys
uid=1000(greys) gid=1000(greys) groups=1000(greys),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),108(netdev),112(bluetooth),116(scanner)

Step 2: add user to sudo group

Excellent, now it’s time to add user greys to the group sudo (we must become root again to run usermod command)

root@debian:~ # usermod -a -G sudo greys
root@debian:~ # id greys
uid=1000(greys) gid=1000(greys) groups=1000(greys),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),108(netdev),112(bluetooth),116(scanner)

As you can see, I’m now a member of the sudo group!

Step 3: Log out and log back in for group membership to be recognised

Now you need to disconnect from your server or desktop session and log in again, so that your group membersip is recognised. One reconnected, check your groups with id command and try sudo again:

greys@debian9:~$ id
uid=1000(greys) gid=1000(greys) groups=1000(greys),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),108(netdev),112(bluetooth),116(scanner)

so yes, we’re a member of sudo group now… This is the moment of truth! Let’s try to become root:

greys@debian:~$ sudo -i
root@debian:~ # id
uid=0(root) gid=0(root) groups=0(root)

Tha’ts it for today!

See Also




Show Monitor Resolutions with xrandr

Unix Tutorial

One of the things still left to be done on my Ubuntu laptop is to get the full resolution output on my LG 5K monitor. This is still work in progress, but one of the most useful tools for getting there is xrandr.



What is RandR

You may know that most of Linux laptops and desktops are using X11 or Xorg graphics system for providing core functionality to higher level graphics environments like Gnome, MATE or KDE.

RandR is one of the most common modules of X11/Xorg, it’s a plugin implementing basic Resize, Rotate and Reflect – RandR for short.

xrandr command

xrandr is a great command line utility that provides low-level management of your displays, detecting monitor resolutions and adding new display modes.

Here’s the most basic way of using xrandr: simply run it without parameters to see all the attached graphics devices and their resolutions:

greys@xps:~ $ xrandr
Screen 0: minimum 320 x 200, current 4096 x 2304, maximum 8192 x 8192
eDP-1 connected (normal left inverted right x axis y axis)
3840x2160 60.00 + 59.98 59.97
3200x1800 59.96 59.94
2880x1620 59.96 59.97
2560x1600 59.99 59.97
2560x1440 59.99 59.99 59.96 59.95
2048x1536 60.00
1920x1440 60.00
1856x1392 60.01
1792x1344 60.01
2048x1152 59.99 59.98 59.90 59.91
1920x1200 59.88 59.95
1920x1080 60.01 59.97 59.96 59.93
1600x1200 60.00
1680x1050 59.95 59.88
1600x1024 60.17
1400x1050 59.98
1600x900 59.99 59.94 59.95 59.82
1280x1024 60.02
1440x900 59.89
1400x900 59.96 59.88
1280x960 60.00
1440x810 60.00 59.97
1368x768 59.88 59.85
1360x768 59.80 59.96
1280x800 59.99 59.97 59.81 59.91
1152x864 60.00
1280x720 60.00 59.99 59.86 59.74
1024x768 60.04 60.00
960x720 60.00
928x696 60.05
896x672 60.01
1024x576 59.95 59.96 59.90 59.82
960x600 59.93 60.00
960x540 59.96 59.99 59.63 59.82
800x600 60.00 60.32 56.25
840x525 60.01 59.88
864x486 59.92 59.57
800x512 60.17
700x525 59.98
800x450 59.95 59.82
640x512 60.02
720x450 59.89
700x450 59.96 59.88
640x480 60.00 59.94
720x405 59.51 58.99
684x384 59.88 59.85
680x384 59.80 59.96
640x400 59.88 59.98
576x432 60.06
640x360 59.86 59.83 59.84 59.32
512x384 60.00
512x288 60.00 59.92
480x270 59.63 59.82
400x300 60.32 56.34
432x243 59.92 59.57
320x240 60.05
360x202 59.51 59.13
320x180 59.84 59.32
DP-1 connected (normal left inverted right x axis y axis)
2560x2880 60.00
DP-2 connected primary 4096x2304+0+0 (normal left inverted right x axis y axis) 600mm x 340mm
3840x2160 60.00 +
4096x2304 60.00*
3200x1800 60.00
2560x1440 60.00
640x480 59.94

Understanding the xrandr output

Structured output lists multiple monitors connected: eDP (embedded Display Port – this is used for the primary laptop screen) and DP-1/DP2 – which are Display Ports for external connections.

I have highlighted the 4K resolution I’m getting so far, and think additional trickery would be needed to get this monitor show its true 5K (5120×2880) resolution. Stay tuned!

See Also




Examples of Using ip command

ip command showing IPv4 addresses

You may have seen the ip command page on this website, and even used ip addr show version of it. Here’s a few more really powerful options for ip.

Show Only IPv4 Addresses with ip command

If default ip addr show (or ip a for short) is too much information:

greys@becky:~ $ ip addr show
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:b5:fb:da brd ff:ff:ff:ff:ff:ff
inet 192.168.1.66/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:feb5:fbda/64 scope link
valid_lft forever preferred_lft forever

…just specify the -4 option (short for IPv4) to only show IPv4 addresses info:

greys@becky:~ $ ip -4 addr show
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.1.66/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever

Show Specific Interface with ip command

To further narrow it down and show just info for one of the interfaces, specify it in the command line:

greys@becky:~ $ ip -4 addr show dev eth0
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.1.66/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever

Show Brief Summary using ip command

Just specify -br option to see just bare essentials for the specified interfaces (or all of them if you’re not indicating interface name) – you’ll get interface name, status (whether it’s UP or DOWN right now) and the assigned IP address:

greys@becky:~ $ ip -br -4 addr
 lo               UNKNOWN        127.0.0.1/8
 eth0             UP             192.168.1.66/24

If I want to just show this for eth0, here’s how I do it

greys@becky:~ $ ip -br -4 addr show dev eth0
 eth0             UP             192.168.1.66/24

That’s useful enough to learn in case ifconfig command is not found or you simply want to use ip command instead of ifconfig command a bit more.

See Also




Assign Keyboard Shortcut to Screenshot in Ubuntu

I’m finding myself working on Linux laptop with Ubuntu 19.04 more often than I expected – sometimes I spend most of my day research and preparing Unix Tutorial posts in Linux instead of macOS. Today I got an opportunity to improve my screenshotting productivity a bit more.



How To Take Screenshots in Ubuntu

I tried a few screenshot apps in Ubuntu but eventually settled on the default one that comes pre-installed – it’s called Screenshot. The main reason for this choice wasn’t for its functionality (and certainly not for its performance, it’s rather slow) but because most of other options don’t support the HiDPI resolutions properly – so a quick task of screenshotting something becomes a tedious chore that defeats the purpose.

So I settled on using Screenshot app. I usually press the Start (Windows?) key on my keyboard and this brings a view of all the windows and gives me the app search window at the top of the screen:

I can then type Screenshot there and press Enter to run the app:

And then select the type of screenshot action I want to progress:

Adding Keyboard Shortcut to Screenshot

I decided to improve the things by skipping the whole “search for an app named Screenshot” part of the process and started looking into Keyboard Shortcuts settings.

Turns out, there is an even better option: I can select a shortcut to not just start Screenshot but to also select the action (screenshot current window) and save the file into Pictures directory:

The default shortcut Alt+Print is more than adequate, but on my multimedia keyboard from Logitech I don’t have Print button and must press Fn for invoke it. So I decided to change the default shortcut to Alt+S:

That’s it, I now can simply press Alt+S and get the window I’m working with automatically screenshotted (is this a word?) and placed into Pictures:

Hope you like this tip, have a great day!

See Also




Listing Full Timestamps with ls in Linux

I was reading a man page for ls command and noticed a very interesting command line option. It appears that full-time option is available in Linux, but not in MacOS. Need to check later to confirm if it’s a Linux-only or filesystem specific option.



Default ls -al Behavior

Here’s how a typical long form of ls looks:

greys@s2:~/scripts $ ls -al
 total 40
 drwxr-xr-x  4 root  root  4096 Apr 25  2018 .
 drwx------. 9 greys greys 4096 Jul 26 01:09 ..
 -rwxr-xr-x  1 root  root  1344 Apr 25  2018 backup.sh
 -rwxr-xr-x  1 root  root   762 Apr 25  2018 content_jira.sh
 -rwxr-xr-x  1 root  root  1125 Apr 25  2018 copy_content_guest.sh
 drwxr-xr-x  8 root  root  4096 Apr 25  2018 .git
 drwxr-xr-x  2 root  root  4096 Apr 25  2018 lists
 -rw-r--r--  1 root  root   340 Apr 25  2018 README.md
 -rw-r--r--  1 root  root  1732 Apr 25  2018 scanning_ports.sh
 -rwxr-xr-x  1 root  root  1413 Apr 25  2018 server-info.sh

Listing full timestamps

And this is the full timestamps:

greys@s2:~/scripts $ ls --full-time
total 28
-rwxr-xr-x 1 root root 1344 2018-04-25 13:10:21.379844332 +0100 backup.sh
-rwxr-xr-x 1 root root 762 2018-04-25 13:10:21.379844332 +0100 content_jira.sh
-rwxr-xr-x 1 root root 1125 2018-04-25 13:44:04.054711619 +0100 copy_content_guest.sh
drwxr-xr-x 2 root root 4096 2018-04-25 13:31:56.514054659 +0100 lists
-rw-r--r-- 1 root root 340 2018-04-25 13:10:21.379844332 +0100 README.md
-rw-r--r-- 1 root root 1732 2018-04-25 13:10:21.379844332 +0100 scanning_ports.sh
-rwxr-xr-x 1 root root 1413 2018-04-25 13:10:21.379844332 +0100 server-info.sh

Seems pretty cool, right?

See Also




Great Things You Can Confirm with dmidecode

dmidecode-command

I’ve just started working on the Linux Commands section of Unix Tutorial, and dmidecode is one of the best commands to mention when it comes to recent Linux distros. It’s found in most distributions and helps with learning lots of useful facts about your environments – both physical and virtual.

How To Use dmidecode Command

dmidecode command needs to be run as root and shows you hardware information about your system:

  • BIOS name and version
  • manufacturer of your server, desktop or laptop
  • model name and serial number of your system

Simply run the command and pipe it to a pager like more or less:

greys@xps:~ $ sudo dmidecode | less
# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 3.2.1 present.
# SMBIOS implementations newer than version 3.2.0 are not
# fully supported by this version of dmidecode.
Table at 0x000E0000.

Handle 0x0000, DMI type 0, 26 bytes
BIOS Information
Vendor: Dell Inc.
Version: 1.2.1
Release Date: 02/14/2019
Address: 0xF0000
Runtime Size: 64 kB
ROM Size: 32 MB
...

Browsing dmidecode output and searching through the output are the best ways to use dmidecode command, but once you become familiar with your environment you’ll probably get a few dmidecode parameters you can grep for.

Hardware Vendor with dmidecode

This will report the manufacturer of your system. For my XPS laptop, it shows Dell:

greys@xps:~ $ sudo dmidecode | grep Vendor
Vendor: Dell Inc.
Vendor ID:

Model name with dmidecode

Look for the Product Name to confirm the name of your system. It shows my laptop’s model for me:

greys@xps:~ $ sudo dmidecode | grep Product
Product Name: XPS 13 9380
Product Name: 0KTW76

Motherboard with dmidecode

Finding the motherboard model will require you to search through the less pager output (press / and start typing word motherboard, then scroll up and down).

Here’s what one of my dedicated servers shows:

Handle 0x0002, DMI type 2, 15 bytes
Base Board Information
Manufacturer: Supermicro
Product Name: X11SSE-F
Version: 1.01
Serial Number: ZM163S009892
Asset Tag: To be filled by O.E.M.
Features:
Board is a hosting board
Board is replaceable
Location In Chassis: To be filled by O.E.M.
Chassis Handle: 0x0003
Type: Motherboard
Contained Object Handles: 0

Serial numbers with dmidecode

Just grep for the word Serial to find lots of serial numbers of various recognised devices. One of them (the first one in the output) is the Dell’s service tag that you usually need for hardware support:

greys@xps:~ $ sudo dmidecode | grep Serial
Serial services are supported (int 14h)
Serial Number: 50G8V**
Serial Number: /50G8V**/CN***00***00F8/
Serial Number: 50G8V**
Serial Number: Not Specified
Serial Number: Not Specified
Serial Number: To Be Filled By O.E.M.
Serial Number: 0A3E
Debug Use USB(Disabled:Serial)

There’s lots of other things dmidecode is useful for – I’ll be sure to update the dmidecode command page going forward.

See Also




Create swap from File on BTRFS Filesystem

mkswap-swapon

I didn’t create any swap during initial Ubuntu 19.04 install on Dell XPS laptop, so had to improvise when compiling a massive opensource project recently. This post shows you how to temporarily add swap memory using a regular file.

Create swap Using Regular File

The usual approach is fairly simple:

  • you create a large enough file (the desired size of your swap memory) – 1GB or 4GB or something like that
  • you initialise it as swap
  • you activated it as swap

Swap Files Support in BTRFS

Since I’ve used BTRFS filesystem for root in my Ubuntu setup, I discovered that there are additional steps needed: BTRFS filesystem creates and writes files in a way that’s been mostly incompatible with swap usage.

But as luck would have it, BTRFS supports swap files with Linux Kernel 5.0.x, so you just need to create file with specific attribute.

Swap Space Using File Procedure

Step 1: Create new empty file

We need to create a file with NOCOW (NO Copy-On-Write) property (no harm anywhere but is a requirement for BTRFS):

root@xps:/ # touch /swapfile
root@xps:/ # chattr +C /swapfile

Step 2: Allocate required amount of space to the file

root@xps:/ # fallocate -l 8G /swapfile

This file is 8GB now:

root@xps:/ # ls -ald /swapfile 
-rw------- 1 root root 8589934592 Jul 17 19:55 /swapfile

Step 3: Update permissions

root@xps:/ # chmod 0600 /swapfile 

Step 4: Initialise /swapfile as swap storage

root@xps:/ # mkswap /swapfile 
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=16d35c04-78de-4dd3-aeb0-e2228bb7ce36

Step 5: Activate swap space from /swapfile

root@xps:/ # swapon /swapfile

Step 6: Confirm newly activated swap space

root@xps:/ # free -h
total used free shared buff/cache available
Mem: 15Gi 6.1Gi 1.7Gi 3.1Gi 7.5Gi 5.8Gi
Swap: 8.0Gi 0B 8.0Gi

That’s it for today!

See Also




How To: Check AppArmor Status

apparmor-logo

AppArmor is a Linux Kernel security module that implements mandatory access control (MAC) security with per-application profiles in Debian based systems. It’s possible to confirm if AppArmor is enabled  in your Debian or Ubuntu system and to also find out the mode it’s running in.

AppArmor Status with aa-status Command

aa-status command will list the currently loaded AppArmor modules.

For instance, here’s how it looks on a system where AppArmor is inactive (Debian 9 in my case):

root@debian9:~# aa-status 
apparmor module is loaded. 
apparmor filesystem is not mounted.

And here is how AppArmor status is reported on Debian 10 system where it’s activated by default:

root@debian10:~# aa-status 
apparmor module is loaded.
20 profiles are loaded.
18 profiles are in enforce mode.
/usr/bin/evince
/usr/bin/evince-previewer
/usr/bin/evince-previewer//sanitized_helper
/usr/bin/evince-thumbnailer
/usr/bin/evince//sanitized_helper
/usr/bin/man
/usr/lib/telepathy/mission-control-5
/usr/lib/telepathy/telepathy-*
/usr/lib/telepathy/telepathy-*//pxgsettings
/usr/lib/telepathy/telepathy-*//sanitized_helper
/usr/lib/telepathy/telepathy-ofono
libreoffice-senddoc
libreoffice-soffice//gpg
libreoffice-xpdfimport
man_filter
man_groff
nvidia_modprobe
nvidia_modprobe//kmod
2 profiles are in complain mode.
libreoffice-oopslash
libreoffice-soffice
0 processes have profiles defined.
0 processes are in enforce mode.
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

See Also




Debian 10 – Buster

debian-linux-logo

Debian 10 “Buster” got released ealier this week, culminating work of the past 2 years. It’s a massive upgrade to Debian 9 and a solid release available for 10 architectures (x86/x64/AMD64 and arm64/amrhf plus MIPS, PowerPC and IBM System Z.

I’m away from home for the past 2 weeks but will attempt distro upgrade in my Debian 9 VM, so stay tuned.

Notable Improvements in Debian 10 Buster

Secure Boot is finally here in Debian 10!

This means Debian 10 comes with signed and fully trusted version of GRUB boot loader that is activated by the Shim loader.

Secure Boot is a cool security feature found on most modern PCs and laptops, basically to ensure that trusted (secure) code is loaded by UEFI (Unified Extensible Firmware Interface) before Operating System.

AppArmor is Activated by Default

This means at least the core OS functionality will run with improved security. You’ll need to disable AppArmor using GRUB options if you want to continue running OS without AppArmor.

nftables is the Default Packet Filter

nftables tool will help with managing both IPv4 and IPv6 packets. I think iptables compatibility is there, meaning you can run iptables commands with common syntax – but implementation will actually be done using nftables.

Linux Kernel Updated to 4.19

Not quite the bleeding edge (I hear Linux Kernel 5.2 released recently), but that’s exactly the point: Debian always prides itself on being one of the most stable and reliable distros, meaning it won’t have latest features but will ensure that everything avaialbe will work as expected.

See Also




How To: Upgrade Packages That Were Kept Back

ubuntu-linux-logo

Now and then you may notice that apt-get upgrade command keeps a few packages back, meaning they don’t get upgraded. This quick post shows what you can do about it and how to get all the packages upgraded.

How apt-get Keeps Packages Back

This is how keeping packages back will look like:

greys@xps:~ $ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
linux-generic linux-headers-generic linux-image-generic
0 to upgrade, 0 to newly install, 0 to remove and 3 not to upgrade.

Why Packages Are Kept Back by apt-get

Most likely reason for keeping packages back is that upgrading them means installing new packages or removing existing ones. apt-get upgrade strictly upgrades existing packages, without removing or installing anything.

Since the command you’re giving to apt-get is upgrade and not to install new packages, packages that require some old packages removed or new packages installed are kept back.

How To Upgrade Packages That Were Kept Back

Simply use the dist-upgrade option of apt-get, which will resolve dependencies and install/remove dependent package as needed:

greys@xps:~ $ sudo apt-get dist-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed
linux-headers-5.0.0-20 linux-headers-5.0.0-20-generic linux-image-5.0.0-20-generic linux-modules-5.0.0-20-generic
linux-modules-extra-5.0.0-20-generic
The following packages will be upgraded:
linux-generic linux-headers-generic linux-image-generic
3 to upgrade, 5 to newly install, 0 to remove and 0 not to upgrade.
Need to get 67.0 MB of archives.
After this operation, 334 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

That’s it for todat, enjoy!

See Also