Listing posts

Displaying posts 11 - 15 of 189 in total
Useful Android apps
Last update
2019-03-04
2019
03-04
«a collection of must have android apps for many common needs»

General

Media

Games

System


~~~ * ~~~

SSH tunnel howto and hints attachment
mouse 241 · person cloud · link
Last update
2019-02-27
2019
02-27
«relevant things about ssh tunnels»

A general tunnel command:

1
2
3
4
5
6
7
8
9
10
11
12
13
ssh myuser@mysrv
  -L   8080:localhost:80  # tunnel local_port:target:target_port
  -L *:8080:localhost:80  # tunnel open to everyone who can connect to this machine
  -R   3380:localhost:80  # reverse tunnel port_on_mysrv:target:target_port
  -R *:3322:localhost:22  # reverse tunnel open to everyone who can connect to mysrv
  -q # quiet mode
  -f # go to background
  -n # redirects stdin from /dev/null
  -N # do not execute a remote command
  -T # disable pseudo-terminal allocation
  -M -S /path/to/socket # enable master mode via a shared socket
  -o UserKnownHostsFile=/dev/null # do not update known_hosts file
  -o StrictHostKeyChecking=no     # do no check the empty known_hosts file

Note: if you do a reverse tunnel open to everyone you have to set GatewayPorts clientspecified on /etc/ssh/sshd_config.

Scriptable tunnels

You can look for process IDs via pgrep/pkill:

1
2
ssh -fnNT ... mysrv          # start
pkill -f -QUIT 'ssh.*mysrv'  # stop

or better use master mode to avoid both grepping and any timing issues:

1
2
3
ssh -fNM -S /path/to/socket ... mysrv  # start
ssh -S /path/to/socket -O check        # check
ssh -S /path/to/socket -O exit mysrv   # stop

Auto-closing tunnels

SSH runs the specified command and then exits only if no one is using the tunnel:

1
2
ssh -f myusr@mysrv sleep 10  # start auto-closing tunnel
vncviewer 127.0.0.1::25901   # use the tunnel

Keep alive your connection

Keep alive the connection for 60 seconds, 1440 times (= 24 hours):

1
2
3
4
# client configuration: /etc/ssh/ssh_config | ~/.ssh/config
Host *
  ServerAliveInterval 60
  ServerAliveCountMax 1440
1
2
3
# server configuration: /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 1440

Test connectivity

1
ssh -o BatchMode=yes -o ConnectTimeout=3 myusr@mysrv exit && echo ok || echo ko

List active tunnels and ports

1
sudo lsof -i -n | egrep sshd

Source: Stackexchange - master mode, Patrickmn - keep alive, Stackoverflow - test, Superuser - list active tunnels


~~~ * ~~~

gitconfig global file and tools
mouse 385 · person cloud · link
Last update
2019-02-27
2019
02-27
« — »
  • Setup a main config file in /etc/gitconfig or ~/.gitconfig:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  df = diff --staged
  cp = cherry-pick
  bd  = branch --edit-description
  bds = !GIT_EDITOR=cat git branch --edit-description | grep -v "^#"
  pom = push origin master
  lom = pull origin master
  poa = push origin alb
  loa = pull origin alb
  lst = ls-tree --full-tree -r HEAD
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --date=short
  histg = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
  undoci = reset --soft HEAD^
  redoci = commit -c ORIG_HEAD
  stash-unapply = !git stash show -p | git apply -R
  stash-diff = stash show -p
  ; drop all local commits and align with origin/master
  reset-om = reset --hard origin/master
  ; side-by-side diff commands (install ydiff command)
  dfs   = !git diff  --staged | ydiff -s -w 0 -t 2 --wrap
  diffs = !git diff           | ydiff -s -w 0 -t 2 --wrap

[user]
  name  = Alberto Cavalin
  email = acavalin@users.noreply.github.com

[credential "https://github.com"]
  username = acavalin

[core]
  autocrlf = input
  safecrlf = true
  excludesfile = ~/.gitignore_global

[push]
  default = current

[color]
  ui = true
  • Enable adding CR/LF files
1
2
git config --global core.autocrlf false
git config --global core.safecrlf false

resulting in this ~/.gitconfig:

1
2
3
[core]
  autocrlf = false
  safecrlf = false
  • Setup the ignore list in ~/.gitignore_global:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# jEdit temporary files
*.marks
\#Untitled-*#

# Rails files (db, log, temp, cfg)
/db/*.sqlite3
/db/*.sqlite3-journal
/log/*.log
/log/*.gz
/tmp
Gemfile.lock
/config/database.yml

.DS_Store
nohup.out
  • Use a side-by-side colored diff tool

  • Enhance the terminal experience:

    1
    2
    3
    [ -f "$HOME/.git-completion.bash" ] && source "$HOME/.git-completion.bash"
    [ -f "$HOME/.git-prompt.sh" ] && source "$HOME/.git-prompt.sh"
    export PROMPT_COMMAND='__git_ps1'
    
  • Install some good interfaces for git:

1
2
apt-get install tig  # ncurses-based text-mode interface
apt-get install gitg # gtk visual repository viewer
  • Look for a file in a project:
1
grep --color=always --exclude-dir=".git" -rin term . | less -R

Source: cache username/pass via StackOverflow and GitHub; dev.to git terminal, side-by-side diff, add CR/LF files

See also: When to Use Git Reset, Git Revert & Git Checkout


~~~ * ~~~

GNU Screen | terminal multiplexer attachment
mouse 559 · person cloud · link
Last update
2019-02-27
2019
02-27
«terminal multiplexer»

Screen commands shortcuts:

Shortcut Descr
^a c create new terminal
^a # switch to # terminal (0-9)
^a " terminals list
^a A set the current terminal name/title
^a d detach session
^a S or ^a | split region horizontally or vertically
^a X close current region
^a Q close all regions but the current one
^a tab switch inputo to next region
^a ^a toggle between current and previous region
^a \ terminate entire session
^a :number x changer current terminal number to x

Scrolling + copy&paste:

Press ^a esc then use PgUp/Dn or Up/Dn arrows to scroll, when finished hit return two times.

If you move the cursor after hitting return once, you will be selecting text to copy, and hitting return the second time will copy it. Then you can paste it with ^a ].

Command line how-to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
screen       # start a new session (default screen is 0)
screen -d -m # start a new detached session (useful in scrips)
screen -r    # reattach to a running session

screen -d|-D [pid.tty.host] # detaches the elsewhere running screen session

# reattaching options (in case of a "stale attached state"):
screen -d -r   # reattach a session and if necessary detach it first
screen -d -R   # reattach a session and if necessary detach or create it
screen -d -RR  # reattach to the first session and if necessary detach or create it

# or reattach trying even harder:
screen -D -r   # reattach a session, if necessary detach and logout remotely first
screen -D -R   # same as "-D -r" but also notify the user
screen -D -RR  # attach here and now. whatever that means, just do it

Share a session with another user

Same account

1
2
3
4
5
6
7
# user1 starts the session then attach to it
screen -d -m -S shared # start a new detached session called `shared`
screen -x shared # attach in multi display mode

# user2 only attach to it
screen -ls # show current sessions
screen -x shared # attach in multi display mode

Different accounts

1
2
3
4
5
6
7
8
# user1 starts the session then attach to it
screen -d -m -S shared # start a new detached session called `shared`
screen -r shared # attach to the named session
# press "Ctrl-a" and type ":multiuser on"
# press "Ctrl-a" and type ":acladd user2"

# user2 only attach to it
screen -x user1/shared

Note: the multiuser mode needs screen as suid root but this is a security issue... if you want to enable it then type as root:

1
2
3
chmod u+s `which screen`
chmod 755 /var/run/screen
rm -fr /var/run/screen/*

but you really should make a third shared account which both users can log into.

Customize session (see attachment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# ~/.screenrc - screen user config file

# the following two lines give a two-line status, with the current window highlighted
hardstatus alwayslastline '%{= kG}@%{G}%H%? %1`%?%{g}|%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}|%{y}%l%{g}|%{B}%Y-%m-%d %{W}%c%{g}'

# huge scrollback buffer
defscrollback 5000

# no welcome message
startup_message off

# 256 colors
term screen-256color
attrcolor b ".I"
#termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
#defbce on

# mouse tracking allows to switch region focus by clicking
#mousetrack on

shelltitle --
#shell /usr/bin/fish

# default windows
screen 1 bash
screen 2 bash
select 1
bind c screen 1 # window numbering starts at 1 not 0
bind 0 select 10

# get rid of silly xoff stuff
bind s split

# Ctrl+F2 puts Screen into resize mode. Resize regions using hjkl keys.
bindkey "^[O1;5Q" eval "command -c rsz" # enter resize mode
# use hjkl keys to resize regions
bind -c rsz h eval "resize -h -5" "command -c rsz"
bind -c rsz j eval "resize -h +5" "command -c rsz"
bind -c rsz k eval "resize -v -5" "command -c rsz"
bind -c rsz l eval "resize -v +5" "command -c rsz"

# switch windows with F1 (prev) and F2 (next)
bindkey "^[OP" prev
bindkey "^[OQ" next

Tips & tricks

  • Use this handy script to start your terminals the way you want:
1
2
3
4
5
6
7
8
#!/bin/bash
if [ -z "$1" ]; then
  echo "USAGE: bashto pwd [cmd arg1 arg2 ...]"
  exit
fi
cd "$1"                    # go to the specified folder
shift; [ -n "$1" ] && "$@" # execute an optional command
exec /bin/bash             # run shell

.screenrc example: screen -t www bashto /mnt/ramd elinks.

1
2
[ -n "$DISPLAY" ] && echo -n "$DISPLAY" > $HOME/.last_display
[ -z "$DISPLAY" ] && export DISPLAY=$(cat $HOME/.last_display)

Alternatives:


Source: GNU.org, ArchLinux tips, A killer GNU Screen Config gist, MC mouse support, Dev.to, Shared session


~~~ * ~~~

Redis setup
mouse 397 · person cloud · link
Last update
2019-02-21
2019
02-21
«caching with http://redis.io/»

NOTE: Redis has changed its license... see its GoodFORM fork on github.


Configure Linux:

1
2
3
4
5
6
7
8
9
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1

echo "net.core.somaxconn = 512" >> /etc/sysctl.conf
sysctl net.core.somaxconn=512

# run and add this to /etc/rc.local (system startup) or
# reboot with the kernel bootparam transparent_hugepage=never
echo never > /sys/kernel/mm/transparent_hugepage/enabled

Compile Redis:

1
2
3
4
5
6
7
8
9
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
sudo apt-get install libc6-dev-i386 # per la versione a 32bit
make 32bit # usa less ram but max memory will be 3GB

# optional
sudo apt-get install tcl8.5
make test

Configure Redis (redis.conf):

1
2
3
4
5
6
7
8
9
10
11
bind 127.0.0.1
daemonize yes
pidfile /path/to/redis.pid
logfile "/path/to/redis.log"
unixsocket /path/to/redis.sock
unixsocketperm 700
save "" # disable auto snapshots
dir /path/to/db_dump_dir
# NOTE: snapshots can take up to 2x maxmemory
maxmemory xxxMB
maxmemory-policy allkeys-lru # eviction policy

Configure Rails:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# in Gemfile:
gem 'hiredis'  # speed up redis client
gem 'redis', require: %w{ redis redis/connection/hiredis } # redis client
gem 'readthis' # use redis as a faster cache store

# in environment config:
config.cache_store = :readthis_store, {
  expires_in: 1.hour.to_i,
  namespace: 'foobar',
  redis: { # redis-rb params
    # host: 'localhost', port: 6379, db: 0,
    url:  'redis://localhost:6379/0',
    # path: 'tmp/sockets/redis.sock', # this is not fault tolerant
  },
  driver: :hiredis
}

# in config/initializers/redis.rb:
Readthis.fault_tolerant = true

# everywhere in the code always use .to_i for expiration time:
expires_in: 1.week.to_i

Source: Redis.io quickstart, full docs, LRU, Admin; DigitaOcean/redis; SitePoint/rails; SitePoint/models; Stackoverflow/models