Category: Knowledgebase

How to calculate free space in thin pool LV

You can calculate free space in thinpool LVM by using the following command:

# dmsetup status
[... ]
vg1-poolB-tpool: 0 45135824 thin-pool 1 4773/178944 90879/206109 - rw discard_passdown queue_if_no_space - 1024

In the above output, 206109 are the total available extents and 90879 are the free extents. You can now find the chunk size as follows:

# lvs -o chunk_size vg1/poolB
Chunk
64.00k

Then total free space should be 5,816,256k.

How many simultaneous VNC sessions can run on a Linux server

VNC is one of the most common methods to access servers remotely by multiple users simultaneously. VNC is usually configured as a systems service that is always on, serving a GUI session for a single or multiple users simultaneously. There is no limit on the number of VNC sessions in a Linux-based server, but constraints are based on available server resources that can be used effectively.

Simultaneous VNC sessions are limited by three primary resources in a server: CPU, RAM, and bandwidth. In a VNC user session, software-based rendering renders the Graphical User Interface (GUI) to the video frame buffer used for remote connectivity.

As such, a VNC session typically consumes high CPU resources than a local console. Similar resources utilization affects the RAM and bandwidth, as the VNC session uses system RAM for Its video frame buffer instead of video RAM.

VNC sessions constrain network bandwidth, as it uses video compression techniques for desktop display, which is less efficient. It should be noted that running videos or graphics video editors on a VNC session will increase resource utilization.

Generally, you can allocate 1 CPU core, 4GB RAM, and 10Mbps network bandwidth for each VNC session. It is recommended that for 8 VNC users, an 8-Core CPU, 32GB RAM, and 300Mbbps network connection is required.

Some mitigation strategies are given below if you have limited hardware resources on a VNC server and serve multiple VNC users.

  • Instead of using GNOME sessions, use minimal X sessions to reduce VNC server load.
  • Reduce screen geometry with geometry options such as geometry=800×600.
  • Enable QoS (Quality of Service) to prioritize VNC over regular user sessions.
  • Limit VNC sessions to single tasks instead of running multiple processes simultaneously.

How to start failed VNC service

  • The VNC server is configured on a Linux machine, but the service status is shown as failed.

systemctl status vncserver@:1.service
● vncserver@:1.service - Remote desktop service (VNC)
Loaded: loaded (/etc/systemd/system/vncserver@:1.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2021-09-01 11:30:03 +04; 11s ago
Process: 13739 ExecStart=/usr/sbin/runuser -l vncuser -c /usr/bin/vncserver %i (code=exited, status=1/FAILURE)
Process: 13739 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill %i > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
Main PID: 1231 (code=exited, status=0/SUCCESS)

  • The main reason for failed VNC server service is that X1-lock and socket files are present in /tmp/.X11-unix. Remove the files to start the VNC session.

rm /tmp/.X11-unix/X1 /tmp/.X1-lock

  • Start the VNC server service as follows:

systemctl start vncserver@:<display>.service

  • The root cause of this error is that files in /tmp/.X* are VNC session log files, and deliberately killing the session will not remove the /tmp/.X* files. The result is that the user cannot start the VNC session on the same display again. The first solution is to remove these stale files.
  • You should remove all stale files under these directories.

/tmp/.X11-unix/
X2 X3 X3 X5

/tmp/.X1-lock
.X0-lock .X11-unix/ .X1-lock .X2-lock .X3-lock .X4-lock

How to restrict normal user to run only limited set of commands

  • Normal users in Linux are usually given permission to execute a certain command in /bin/ and /usr/local/bin. Follow the below steps to remove those permissions and restrict users to run only specific commands.
  • Enter the following command to generate a restricted shell.

cp /bin/bash /bin/rbash

  • Change the shell as a restricted shell while adding the target user.

useradd -s /bin/rbash user-restrict

  • For present users on the machine.

usermod -s /bin/rbash user-restrict

  • In this way, the user user-restrict is chrooted and will not be able to access the links outside his $HOME directory /home/user-restrict
  • Create a directory under /home/user-restrict as follows:
    mkdir /home/user-restrict/directory
  • At this point, the user user-restrict can access all commands that are allowed to execute. These commands are presented from environment PATH variable set in /home/user-restrict/.bash_profile. Change it as follows:

cat /home/user-restrict/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
readonly PATH=$HOME/directory
export PATH

  • Now, if the user user-restrict log in, he/she will not be able to run even simple commands.
  • Now make softlinks of commands for user user-restrict to execute in the directory /home/user-restrict/directory

ln -s /bin/date /home/user-restrict/directory/
ln -s /bin/ls /home/user-restrict/directory/
ll /home/user-restrict/directory/
total 8
lrwxrwxrwx 1 root root 10 Aug 17 18:00 date -> /bin/date
lrwxrwxrwx 1 root root 10 Aug 17 18:00 ls -> /bin/ls

  • Logout and login again and execute the following commands

[user-restrict@example ~]$ date
Sun Aug 17 20:00:00 IST 2021
[user-restrict@example ~]$ ls
filea fileb filec filed directory
[user-restrict@example ~]$ tail
-rbash: tail: command not found

  • To restrict user user-restrict for making any modification in their .bash_profile.

chattr +i /home/user-restrict/.bash_profile

  • The above command will make the .bash_profile immutable. Only root will be able to remove the immutable flag from the file.

chattr -i /home/user-restrict/.bash_profile

How to configure VNC server in AlmaLinux 8.3

  • Install the required VNC server packages in AlmaLinux 8.3 as follows:

yum -y install tigervnc-server tigervnc

  • The default configuration files for the tigervnc-server are located in /etc/tigervnc. In this directory, the following files are present.

vncserver.users
vncserver-config-mandatory
vncserver-config-defaults

  • Map the users to a particular port by adding the following option (:x=user) in /etc/tigervnc/vncserver.users file.

:1=vnc-user1
:2=vnc-users2

  • You should configure one vnc session per user as AlmaLinux 8.3 only supports one unique user per GUI session per machine.
  • Now you will have to edit vncserver-config-defaults file to configure Xvnc parameters. The values in this file will be applied to every user unless the user has its own configuration file in $HOME/.vnc/config. The same options with different values are set in vncserver-config-mandatory file, which replaces the default configuration file and has a higher priority. The format of the configuration file is option=value.

session=gnome
#securitytypes=vncauth,tlsvnc
#desktop=myserver
#geometry=1900x1100
#localhost
#alwaysshared

  • Make sure the parameter session=gnome in the above code matches the name of the session desktop file in the/usr/share/xsessions directory (case-sensitive).
  • Run the following command to start the Tigervnc server. This command should be run as a user who will be starting and using the vnc server. The vnc password must be set while the user is logged in and not by the root.

vncpasswd

  • As root, start the Tigervnc server as follows:

# systemctl enable vncserver@:$x.service
# systemctl start vncserver@:$x.service

  • In the above command, you should replace the variable $x by the actual number configured in /etc/tigervnc/vncserver.users

systemctl enable vncserver@:1.service --now

  • Open vnc default port 5901 in the firewall as follows:

firewall-cmd --permanent --zone=public --add-port 5901/tcp

  • Reload firewall.

firewall-cmd --reload

How to set interface to promiscuous mode permanently

  • You can use a script to apply promiscuous settings to an interface or multiple interfaces when they come online.

touch /etc/NetworkManager/dispatcher.d/30-promisc
chmod +x /etc/NetworkManager/dispatcher.d/30-promisc

  • Add following script code to 30-promisc file.

#!/bin/bash
if [ "$1" == "eth0" ] && [ "$2" == "up" ]; then
ip link set dev "$1" promisc on
fi

  • You can also use network initscripts by adding code in /sbin/ifup-local file. Create a file if it is not present.

#!/bin/bash
if [ "$1" == "eth0" ]; then
/usr/sbin/ip link set dev "$1" promisc on
fi

How to apply ethtool settings to interface using Network Manager dispatcher script

In the same way as above, you can use the network manager dispatcher script to apply ethtool commands.

  • create files as follows:

touch /etc/NetworkManager/dispatcher.d/30-ethtool
chmod +x /etc/NetworkManager/dispatcher.d/30-ethtool

  • Now to apply settings to a single network interface:

#!/bin/bash
if [ "$1" == "eth1" ] && [ "$2" == "up" ]; then
ethtool -K "$1" lro off rx on gro off 
fi

  • For multiple interfaces:

#!/bin/bash
if [ "$1" == "eth1" -a "$2" == "up" ] || [ "$1" == "eth0" -a "$2" == "up" ] ; then
ethtool -K "$1" lro off rx off gro off 
fi

  • For bonding interface:

#!/bin/bashif [ "$1" == "bond0" ] && [ "$2" == "up" ]; then
for INTERFACE in bond0 eth1 eth0; do
ethtool -K "$INTERFACE" lro off rx off gro off 
done
fi

How to set up RDP with Xfce in Kali Linux

Kali Linux is supported on multiple devices. On some systems, you will only get CLI mode installation and may not have direct access to GUI. One way to have GUI on Kali Linux is to install Xfce and setting up RDP. You can do this with the simple script as follows:

#!/bin/sh
echo "[+] Installing Xfce, this will take a while"
apt-get update
apt-get dist-upgrade -y
apt-get install -y kali-desktop-xfce xrdp

echo "[+] Configuring XRDP to listen to port 3390 (but not starting the service)..."
sed -i 's/port=3389/port=3390/g' /etc/xrdp/xrdp.ini

  • To execute the above script, do the following:

wget https://gitlab.com/kalilinux/build-scripts/kali-wsl-chroot/-/raw/master/xfce4.sh
chmod +x xfce4.sh
sudo ./xfce4.sh

  • If you are not using WSL, you need to start service and connect as follows:
    sudo systemctl enable xrdp --now
  • If you are using WSL, you should install dbus-x11 for Xfce and RDP to connect.

sudo apt install -y dbus-x11

  • Start xrdp as follows:

sudo /etc/init.d/xrdp start

  • Open RDP client on Windows system, enter the IP address of the Kali Linux server and port 3390, which is the default port if you have used the script above.

192.168.1.1:3390

How to connect to/setup VNC via tunnel over SSH

If you want to connect to vnc via an ssh tunnel, you must have a user account on the machine running vnc server. You should also know that which display the vnc server is running. These details can be found in /etc/sysconfig/vncservers or via the ps command output.

  • Use the following SSH command to connect to vnc server.

ssh -L 590X:127.0.0.1:590Y -N -f -l user-name server-name

  • Make sure to replace Y in the above command with the display number on which vnc server is running. With the new versions of vncviewer, you can create ssh-tunnel with the -via flag.

vncviewer -via user-name@server-name server-name:1