Linux Tricks

Auto Lock the screen after complete booted to desktop environment

Posted on Updated on

Introduction

To activate the VNC server and remote control the desktop. You need to login the desktop session with a user account.

I have a headless computer in my research office. To use the VNC remote control, I set up an automatic login of the desktop environment. The VNC server will startup automatically at the same time. After the desktop environment is loaded. This command script will lock the desktop environment.

The advantage of this setup is that you can remote control the computer with a desktop environment without any setup. In tradition, you need to login the computer using SSH and running some command to start up the VNC server. Also, you need some program such as tmux/screen to maintain your session and keep the VNC server running.

Procedure

1. Set automatic login on the User tab
2. In the Startup Applications Perferences, insert a new item called auto lock, and insert this to the command input:
dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock

Reference

https://askubuntu.com/questions/181265/automated-login-and-screen-lock

Assign Multiple IP Address to single ethernet interface

Posted on Updated on

Introduction

Sometimes you need to assign multiple IP addresses to a single network interface. Traditionally, you need multiple network cards (NICs) to set up multiple IP addresses. For example, you need Two NICs for Two IP addresses, and so on. 

Actually, you can alias multiple IP addresses to a single NIC. Now we are going to discuss serval methods to achieve this.

Methods

Using Network manager

Suggested for new ubuntu linux machine (>= 16.04)

 

Edit on nmcli

1. Check the connection name of your NIC card
$ nmcli dev status

DEVICE  TYPE      STATE      CONNECTION         
eth0    ethernet  connected  Wired connection 1 
l4tbr0  bridge    connected  l4tbr0    

In this example, we target eth0 and the name of this connection is “Wired connection 1”

2. Setup Edit mode on nmcli
$ sudo nmcli con edit "Wired connection 1"

===| nmcli interactive connection editor |===

Editing existing '802-3-ethernet' connection: 'Wired connection 1'

Type 'help' or '?' for available commands.
Type 'describe [<setting>.<prop>]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, ipv4, ipv6, tc, proxy
nmcli>
3. Use the nmcli to edit the NIC configuration

In this tutorial, we are going to set up 2 ip address which is 192.168.2.6/24 and 192.168.3.6/24. Make sure you answer NO to keep the ipv4.method in auto mode. You can retain the DHCP setup on your NIC.

nmcli> set ipv4.addresses 192.168.2.6/24,192.168.3.6/24
do you also want to set 'ipv4.method' to 'manual'? [yes]: no
4. Check the setup again
nmcli> print ipv4

ipv4.method:                            auto
ipv4.dns:                               --
ipv4.dns-search:                        --
ipv4.dns-options:                       ""
ipv4.dns-priority:                      0
ipv4.addresses:                         192.168.2.6/24,
192.168.3.6/24
4. Save the configuration
nmcli> save
5. Check the configuration file that exists
# We are using Ubuntu, difference system may have a different configuration file path

sudo cat /etc/NetworkManager/system-connections/Wired\ connection\ 1

 

Edit on nmtui

Working on it.

 
 

Using “interfaces”

This method is not recommended to use if you are using Network manager to manager your NIC card. Your NIC card will become “Unmanaged” and you cannot use the Network Manager to change your network configuration in nmcli, nmtui or even in the GUI.

1. Edit the setting file
sudo vim /etc/network/interfaces
2. Change the setting
auto lo eth0 eth0:0
iface lo inet loopback

iface eth0 inet dhcp

iface eth0:0 inet static
    address 192.168.2.6
    netmask 255.255.255.0

iface eth0:1 inet static
    address 192.168.3.6
    netmask 255.255.255.0

Reference

http://www.szakmeister.net/blog/2017/jun/1/static-ip-nmcli/
https://askubuntu.com/questions/452317/both-dhcp-and-static-ip-addresses-simultaneously-on-one-interface

Access computer behind a firewall without SSH port forwarding

Posted on

Introduction

If you want to directly access a computer behind the firewall(gateway), but the system admin is blocked the ssh port forwarding feature. What should you do?

Here is a method that use fifo to access the computer behind the firewall.

Method

You need to open 2 terminal sessions. One for the pipeline tunnel and the other one for accessing the computer.

#Session 1, create the tunnel
mkfifo fifo
cat fifo | ssh USER@FIREWALL_IP "ssh USER@COMPUTER_IP \"nc 127.0.0.1 22\"" | nc -l 127.0.0.1 8000 > fifo
rm fifo
#Session 2, ssh access
ssh USER@127.0.0.1 -p 8000

Toggle the visibility of GPU

Posted on Updated on

Introduction

This bash script can provide a simple bash function to toggle the visibility of certain GPU.

Motivation

Tensorflow has a behavior that discovers all of the GPU on your computer and starts using them. It is a problem when you share a computer with other users. If you forget to change the “CUDA_VISIBLE_DEVICES” variable, Tensorflow will occupy all of the GPU.

Proposed Method

This bash script can toggle the visibility of a certain GPU. By default, all of the GPUs are hidden. You need to use ugpu() function to toggle the visibility of certain GPU. Also, if you want to toggle the visibility to all GPU, you can use ugpuall() function to make all of the GPUs visible.

Setting

Please insert the following lines to “/etc/profile” OR “~/.bashrc”. For zsh user, please add “precmd() { eval “$PROMPT_COMMAND” }” to your ~/.zshrc first.

export CUDA_VISIBLE_DEVICES="";

PROMPT_COMMAND+="export CUDA_VISIBLE_DEVICES=\"\""; #hide all of the gpu by default

function ugpu(){    # toggle the visibility of the GPU
    export CUDA_VISIBLE_DEVICES="$1"
}
function ugpuall(){  # use all of the GPUs
    unset CUDA_VISIBLE_DEVICES
}

Usage

ugpu 0; ./gpu_program     #Use gpu 0
ugpu 0,1; ./gpu_program   #Use gpu 0 & 1
./gpu_program             #Use NO gpu
ugpuall; ./gpu_program    #Use All gpu

Reference

https://superuser.com/questions/735660/whats-the-zsh-equivalent-of-bashs-prompt-command

Using apt-get with proxy

Posted on

#Create & Edit this file using editor
sudo vim /etc/apt/apt.conf.d/proxy 
#insert the following lines
Acquire::http::proxy "http://PROXY_URL:PORT";  
Acquire::https::proxy "https://PROXY_URL:PORT";  
Acquire::ftp::proxy "ftp://PROXY_URL:PORT";  
Acquire::socks::proxy "PROXY_URL:PORT";

/boot need more space !

Posted on Updated on

查看中文版本 , 請按此進入

Sometime the system will prompt a notification to notify the user that the /boot partition is out of space. They need to free the space. Otherwise, the system may fail to install new kernel or fail to boot.

Linux system will store multiple kernel image in /boot partition. It is very useful when you install a new kernel but it fails to boot. In this situation, you can rollback to other kernel that store inside the /boot partition. Linux system won’t remove the old kernel image even the /boot partition is running out of space. You need to do it by yourself.

To solve this problem, you need to run this command only. It just like a magic :)

sudo apt autoremove --purge

You can use the following command to list the files that storage in /boot partition.

df -h /boot
sudo du -sh /boot/*

Reference:

https://techmonger.github.io/28/boot-cleanup-ubuntu

Remote file editing via VSCode

Posted on Updated on

1. Install rmate

First, you need to install rmate in your REMOTE server, there have many implementation of the rmate. In this post, i use the shell script implementation one.

#install rmate in remote server
sudo wget -O /usr/local/bin/rmate https://raw.github.com/aurora/rmate/v1.0.1/rmate
sudo chmod a+x /usr/local/bin/rmate

2. Edit SSH config

After that, you need to edit the ssh config file in your LOCAL computer. This ssh config will open a reverse tunnel of port 52698 for rmate along with the ssh connection.

#In .ssh/config
#Insert these line

Host YOUR_HOST_IP_OR_HOST_NAME
     RemoteForward 52698 localhost:52698

3. Install Remote VSCode in your VSCode

You can download this package through VS Code

4. Start the remote server of your

Press “Ctrl + P”
Type “>Remote: Start Server”

5. SSH to your remote server

Using the VSCode terminal to ssh to your server. You can also use another Terminal to establish the connection between your local computer and your remote server.

6. Open the file

Use the rmate command to open you file on your REMOTE server.

#Example
rmate file.txt

The file’s content will display on a new VSCode Tab. Use “Ctrl + S” or save button to upload you edited file to your remote server.

Reference

https://www.cnblogs.com/learn21cn/p/6189023.html
https://unix.stackexchange.com/questions/162093/reverse-ssh-tunnel-in-config

git SSH host through proxy

Posted on Updated on

If your computer is running behind the proxy, you cannot connect to the external machine via ssh directly.

I encounter this problem when i want to connect to gitlab.com via ssh connection.

Here is the solution. Just add the proxy setting to ~/.ssh/config

 

Add proxy setting to ssh

#~/.ssh/config
Host=*
ProxyCommand=nc -X 5 -x PROXY:PORT %h %p

 

Git Repo Setting

Please use "git remote -v" command to check your repo's link. If there has "https://&quot; in front of the URL, you need to switch it from HTTPS remote URL to SSH remote URL via the following command.

#ssh access link of most git host
git remote set-url origin "git@HOST:USERNAME/REPO"

If needed, your also need to apply socks proxy to git config.

#git socks proxy
git config --global socks.proxy "PROXY:PORT"

Generate ssh key

#Generate ssh rsa key
ssh-keygen -t rsa -b 4096

 

Reference:

https://stackoverflow.com/questions/18797940/still-requiring-login-after-ssh-authentication

Speedup X11 forward

Posted on

To speedup the X11 forwarding speed

-X argument enable the X11 forwarding
-C argument enable the compression in ssh connection
-c argument can change the cipher, seems aes128-gcm@openssh.com is the fastest cipher in the default configuration

#Example
ssh -XC -c aes128-gcm@openssh.com <HOSTNAME_HERE>