To open URLs like: ssh://user@hostname.com
Check to see if ssh protocol handler is already set:
xdg-mime query default x-scheme-handler/ssh
Create desktop entry (.desktop file) for ssh handler:
cat << EOF > ~/.local/share/applications/ssh-handler.desktop
[Desktop Entry]
Name=SSH Handler
GenericName=SSH Handler
Comment=Open ssh URLs - ssh://user@hostname
Version=1.0
Exec=bash -c '(URL="%U" HOST="\${URL:6}"; ssh \$HOST); bash'
Terminal=true
Type=Application
Icon=utilities-terminal
MimeType=x-scheme-handler/ssh;
Keywords=ssh;protocol
EOF
Set default ssh handler:
xdg-mime default ssh-handler.desktop x-scheme-handler/ssh
Now open URL from browser. Done!
Grep – Useful expressions
Grep file size from du -sh
Format (10K , 10.1M , 1000.21G)
grep -oE "^[[:digit:]]+(\.[[:digit:]]+)?[KMG]"
Or
grep -oE "^[[:digit:]]+(\.[[:digit:]]+)?[KMG]|^[[:digit:]]+[[:blank:]]"
Grep IP address
grep -E '(^|[[:blank:]])[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}([[:blank:]]|$)' | sed -e 's/[[:blank:]]//g' # grep sed awk grep -E '(^|[[:blank:]])[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}([[:blank:]]|$)' | sed -e 's/[[:blank:]]//g' | awk -F"." '{ if ($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255) print $1"."$2"."$3"."$4}'
Grep upper or lower character
# grep any upper character and lower 'a' grep -E '[[:upper:]a]' # grep upper of lower single alphabet (defined by variable) (alpha=a;Alpha=`echo ${alpha} | tr [a-z] [A-Z]` ; echo aABab | grep -E '[${alpha}${Alpha}]')
Tutorial: Disk Drive: Loop back device
Tools: losetup, gdisk, mkfs.ext4
File as a filesystem / Filesystem in a file
create a file
dd if=/dev/zero of=virt_fs.img bs=1024 count=10240
bytes=1024 (1KB)
count=10240
total = 1024*10240 = 10485760 Bytes (10MB)
or
dd if=/dev/zero of=virt_fs.img bs=1M count=10
or
truncate -s 10M virt_fs.img
Setup loop-back device
find first available loop-back device
sudo losetup -f
create loop back device (using first available loop-back device and file)
sudo losetup /dev/loop0 virt_fs.img
Check status of loop-back devices
sudo losetup -a
Format the device
(Partitioning is optional)
mkfs.ext3 -L virtFileSystem /dev/loop0
Mount device
make mount point
mkdir virtFS
mount loop-back device
sudo mount -t ext3 /dev/loop0 virtFS
CLI o/p
testUser@testBench:~$ dd if=/dev/zero of=virt.fs.img bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0197492 s, 531 MB/s
testUser@testBench:~$ sudo losetup -f
[sudo] password for testUser:
/dev/loop0
testUser@testBench:~$ sudo losetup /dev/loop0 virt.fs.img
testUser@testBench:~$ sudo losetup -a
/dev/loop0: [0806]:1608731 (/home/testUser/virt.fs.img)
testUser@testBench:~$ mkdir Virt.FS
testUser@testBench:~$ sudo mkfs.ext3 -L VirtFS /dev/loop0
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done
Filesystem label=VirtFS
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
2560 inodes, 10240 blocks
512 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=10485760
2 block groups
8192 blocks per group, 8192 fragments per group
1280 inodes per group
Superblock backups stored on blocks:
8193
Allocating group tables: 0/2 done
Writing inode tables: 0/2 done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: 0/2 done
testUser@testBench:~$ sudo mount -t ext3 /dev/loop0 Virt.FS
testUser@testBench:~$ df -h /home/testUser/Virt.FS
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 8.7M 97K 8.1M 2% /home/testUser/Virt.FS
testUser@testBench:~$ exit
Tutorial: ssh: Password less (key based) authentication
Password-less authentication using public/private key pair
Create public/private key pair
Default is RSA key pair for use with SSH v2
It is created in .ssh/ in home directory
Files created are id_rsa (private key file) and id_rsa.pub (public key file)
ssh-keygen -t rsa
Copy public key to remote server .ssh/authorized_keys file
.ssh/ is in the home directory of remote server user (in this case xyz)
cat .ssh/id_rsa.pub | ssh xyz@192.168.1.100 ' \ [ ! -d .ssh ] && mkdir -p -m 700 .ssh ; \ [ ! -f .ssh/authorized_keys ] && touch .ssh/authorized_keys ; \ [ "$(stat -c %a .ssh/authorized_keys)" != 600 ] && chmod 600 .ssh/authorized_keys \ cat - >> .ssh/authorized_keys ; '
Or
cat .ssh/id_rsa.pub | ssh xyz@192.168.1.100 ' \ (umask 0077; [ ! -d .ssh ] && mkdir -p .ssh; \ [ ! -f .ssh/authorized_keys ] && touch .ssh/authorized_keys ; \ cat - >> .ssh/authorized_keys ; )'
Or
ssh-copy-id xyz@192.168.1.100
Tutorial: Encrypted drive
Coming soon…
Tools 101: cryptsetup
Create a LUKS (Linux Unified Key Setup) formated, encrypted device:
cryptsetup --verbose --verify-passphrase luksFormat /dev/sdb1
Display encrypted device info:
cryptsetup luksDump /dev/sdb1
Open (unlock) encrypted device
cryptsetup open --type luks /dev/sdb1 crypt
Close (lock) encrypted device
cryptsetup close --type luks crypt
Add pass-phrase from key_file, total of eight are allowed
cryptsetup luksAddKey /dev/sdb1 /home/xyz/key_file
Remove pass-phrase previously added from key_file, undo the above AddKey step
cryptsetup luksRemoveKey /dev/sdb1 /home/xyz/key_file
Open encrypted drive using key file
cryptsetup open --type luks --key-file /home/xyz/key_file /dev/sdb1 crypt
Tools 101: dd
Fill a device with zeros
dd if=/dev/zero of=/dev/sdb
Fill a device with 1024*10240 bytes of zeros
dd if=/dev/zero of=/dev/sdb count=10240 bs=1024
Create a random key file:
dd if=/dev/urandom of=key_file bs=1024 count=4 iflag=fullblock
Hello world!
Welcome to nixCast!
At nixCast we offer helpful tips and tricks on GNU/Linux and Unix Operating Systems.