Sys admin tips

Too small to be put in separate post I will use this one "feature" post for putting all the little tricks I am using in my daily work. So when I remember something usefull or stumble on something new I't will go here...

  1. find your public IP from CLI
  2. list and kill screen session
  3. run playbook on a single host
  4. ldapsearch auto decode base64
  5. xenserver syslog level
  6. disable ipv6
  7. xen orchestra set NFS v4
  8. search string inside files
  9. search files recursively and copy
  10. redirect standard error (stderr)
  11. check on what ports server is listening
  12. set default web browser

find your public IP from CLI

There are few ways you can get your IP from command line. Easiest are using curl or wget:


curl ifconfig.me
wget -qO- ifconfig.me

Or using google servers:


dig TXT +short o-o.myaddr.l.google.com @ns1.google.co


list and kill screen session

Seems I always forget this one. So here it is. First command lists all sessions and second one kills the session by specifiying its ID:


screen -list
screen -XS [sessionID] quit


run playbook on a single host

If you have a playbook ready and need to run it ad-hoc on single host that you haven't specified in hosts file you can use this (note comma at the end of IP address, it is important):


ansible-playbook -i 1.1.1.1,  playbook.yml


ldapsearch auto decode base64

When using ldapsearch and getting attribute values back coded in base64 It's nice and handy to have it auto decoded while your ldapsearch command is throwing output.

So just put into your .bashrc:

myldapsearch()
{
ldapsearch $* | perl -MMIME::Base64 -n -00 -e 's/\n +//g;s/(?<=:: )(\S+)/decode_base64($1)/eg;print'
}

And after that reload your .bashrc:


source .bashrc

Now whenever you use ldapsearch it will autodecode base64 values.


xenserver syslog level

There is still no way (at least not in XenCenter 7.3, later versions I haven't checked) to send custom level logs from xenserver into your central log server using GUI. Since default level is info you will be flooded with xapi info messages. Work around this is by manually editing rsyslog file on xenserver host /etc/rsyslog.d/xenserver.conf and at the end add:

.=warn;*.=crit;*.=err  @your.log.server

After that just restart rsyslog service with service rsyslog restart.


disable ipv6

To disable ipv6 put at the end in /etc/sysctl.conf:

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1 

Then restart sysctl:


sysctl -p


xen orchestra set NFS v4

If needed you can set NFS to use v4 for your remotes (default is v3)


sudo nano /opt/xen-orchestra/@xen-orchestra/fs/dist/nfs.js

And change line where mount command is to use v4:

return (0, _execa.default)('mount', ['-t', 'nfs', '-o', 'vers=4'....

After just restart xo-server:


sudo service xo-server restart

search string inside files

If you need to search specific string inside files:


grep -rnw '/path/to/start/search' -e 'string'

search files recursively and copy

Sometimes you need to move specific files from inside multiple directories. Most this happen when TV show episode is in its own folder. Not sys admin problem but hey :)


find 'path/to/' -name '*.mkv*' -type f -exec mv {}  /path/to/season1/ \;

redirect standard error (stderr)

If you need to output command to a file writing:


ls /blabla > output.txt

This will still output of error messages in terminal. So to redirect those messages also use:


ls /blabla > output.txt 2> error.txt

The number 2 specifies the stream ID that the shell modifies. Stream ID
1 is standard output (the default), and 2 is standard error.
So if you like you can redirect both to the same file:


ls /blabla > log.txt 2>&1

check on what ports server is listening

To check what services are runing that is on what ports they are listening you can use netstat command from net-utils package:


sudo netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:10023         0.0.0.0:*               LISTEN      15662/postgrey --pi 
tcp        0      0 0.0.0.0:587             0.0.0.0:*               LISTEN      15882/master        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      462/sshd            
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      15882/master        
tcp        0      0 0.0.0.0:4190            0.0.0.0:*               LISTEN      6703/dovecot        
tcp        0      0 0.0.0.0:993             0.0.0.0:*               LISTEN      6703/dovecot        
tcp6       0      0 :::587                  :::*                    LISTEN      15882/master        
tcp6       0      0 :::9100                 :::*                    LISTEN      415/node_exporter   
tcp6       0      0 :::22                   :::*                    LISTEN      462/sshd            
tcp6       0      0 :::25                   :::*                    LISTEN      15882/master        
tcp6       0      0 :::4190                 :::*                    LISTEN      6703/dovecot        
tcp6       0      0 :::993                  :::*                    LISTEN      6703/dovecot

set default web browser

There are few ways depending what you are using. But few examples would be


xdg-settings set default-web-browser google-chrome.desktop
update-alternatives --config x-www-browser
update-alternatives --config gnome-www-browser