Linux: De Snelle Weg naar Productiviteit
Linux biedt een schat aan commando's die je workflow kunnen versnellen. Hier zijn enkele van mijn favorieten:
Alias
Om aliassen te gebruiken, kan je volgende regels toevoegen in /etc/bash.bashrc. Vervolgens even uit-/inloggen, en je zou de commando's moeten kunnen gebruiken.
viewdisk="sudo df -h"
Firewall
Toevoegen van een service:
ufw enable service
(bv ufw enable http)
Controleren van de status
ufw status
Authenticatie
OpenLDAP
sudo su -
apt-get update
apt-get install slapd ldap-utils
Login met RSA-Keys
Instead of entering each time your login-credentials (username, password…), you can also user sa-keys. This allows you to securely log in to linux machines.
1. Create keys
To start, you need to create a private and public key.
Ideal is to do this on a linux machine.
* Open a session on a linux machine
* Create the keys with the command ‘ssh-keygen’
* Choose the proper file (default is OK), and if wanted a passphrase.
> ssh-keygen
2. Update key-server
* Copy the public part
* Open the file id_rsa.pub and copy the data in there.
* Change key-server
*Login to the keyserver portal and edit your profile
(key-server > identity self-service > my profile > Edit your information)
Past the public key in ‘ssh public keys’ and ‘Save changes’.
*Test connection, connect through SSH
Open a ssh session from the linux machine where you created the key.
If everything went fine, you shouldn’t enter your DAS password.
3. Linux Client
* Copy the private key
* Copy the content from the id_rsa file (not the public one!)
* Paste the private key
* Paste Copy the content in \home\a123456\.ssh\id_rsa file
* Change 123456 with your username
* Change the file rights
* Change the rights to user only (chmod 600 id_rsa)
4. SecureCRT
* Copy the private key
* Copy the content from the id_rsa file (not the public one!)
* Past the private key
* Create a local file (pref id_rsa) on your windows machine. Remember the location!
* Import the key in SecureCRT
* Tools > Manage Agent Keys > Add
* Change the order
* Change the order of the authentication in the global options (if needed).
* You can also add your username in this part so you don’t need to typ it each time.
* Options > Global Options > General > Default Session > Edit Default Settings > Connection > SSH2
Verify the properties of the PublicKey. Does it points to the correct rsa file?
If you want to change existing configs, you can do this right now.
Or you can select ‘folder by folder’ and change all nodes underneath with the options.
The basic commands to work with Ansible
Run first following command (not as root !!!)
> cd /home/username/to-be-linux-ansible/trunk
. ./env
svn up (not necessary, but nice to be up to date)
Use of wildcards
Show on which nodes it will run:
>ansible '~VERNAME.*' -i hosts.nagios -s -m shell -c ssh -a "/etc/init.d/samhain status" --list-hosts
(the option --list-hosts will show you on which nodes it will run)
Use of file (fe hosts3) with servernames
>ansible -i hosts3 'all' -s -m shell -a 'uptime'
Start/Stop van services
>systemctl start XXX > starten
systemctl stop XXX > stoppen
systemctl status XXX > huidige status
systemctl enable XXX > starten bij booten
systemctl disable XXX > Niet meer starten bij booten
/lib/systemd/system/XXX.service > script met nodige inhoud
Oudere systemen:
>service XXX start > Starten
service XXX stop > Stoppen
service XXX status > Huidige status
chkconfig XXX on > Starten bij booten
chkconfig XXX off > niet meer starten bij booten
Netwerk routes
update files:
>/etc/sysconfig/network-scripts/route-eth0
of
etc/rc.local
>ip route add 10.82.65.132/32 via 172.16.130.129 dev
eth1
How to see the routes your are using on network level?
For Windows
>route print
For Linux
>route -n
For Cisco
>show ip route
Test slow network
WLC (SSH)
>show run-config
show msglog
show traplog
debug client< MAC addr>
Laptop
Test WIFI 'employee' from laptop (IP 10.1.2.x)
>http://www.speedtest.net
Test WIFI 'employee' from phone
>Test cable 'VLAN 12' (IP 10.1.2.x)
Configure a port to access vlan 12 on switch-01
>http://www.speedtest.net
>Switch-01
Disable port 1/0/39 (to verifiy!!!)
Test WIFI 'employee' from laptop (IP 10.1.2.x)
http://www.speedtest.net
WLC (SSH)
>show client detail <MAC addr>
Archey
Archey geeft je in het kort enkele statussen weer van je linux machine in CLI. Dit is bv handig bij het opstarten/aanloggen.
sudo apt-get install lsb-release scrot
wget http://github.com/downloads/djmelik/archey/archey-0.2.8.deb
sudo dpkg -i archey-0.2.8.deb
Om nu telkens dat iemand aanlogt de informatie te tonen, voeg je dit toe aan bash.bashrc. Hieronder zie je een voorbeeldje met extra alias definties.
vi /etc/bash.bashrc
alias viewdisk="sudo df -h"
alias cdlogs="cd /var/log/"
if [[ -n $SSH_CONNECTION ]] ; then
clear
archey
echo " Aliasses";
echo " --------";
echo " viewdisk = sudo df -h";
echo " cdlogs = cd /var/log/";
echo "";
echo "";
fi
Proxy testen
To test a https session through a proxy server, you can use something like:
Without proxy
> curl -k -L https://www.something.com
> result: curl: (7) couldn't connect to host
With proxy:
> curl -k -x https://proxyserverurl:8080 -L https://https://www.something.com
> result: curl: (56) Received HTTP code 407 from proxy after CONNECT
CSV naar tekst?
**Import verschillende lijnen/kolomen**
Met volgend scriptje lees je de verschillende tabellen uit in een csv bestandje, en kan je deze gebruiken in een scriptje. Bv onderstaand was om een hele lijst hosts toe te voegen in cacti.
> #!/bin/bash
> listfile="file.txt"
> while read f2 f1
> do
> php cli/add_device.php --description=$f1 --ip=$f2 --community=public --template=4 --disable=1;
> done < $listfile
Log iets met datum
>now="$(date '+%Y:%m:%d:%H:%M:%S')"
echo "$now Restart em0" >> /var/log/homemade.log
Vind bestanden
Find biggest files:
>find /app/ -type f -exec du -Sh {} + | sort -rh | head -n 5
Find biggest folders
>du -hs /app/* | sort -rh | head -5
Logs bekijken
Logs zijn altijd heel handig wanneer je precies wenst te weten wat er gaande is in je systeem. Je kan dmv logs een beter inzicht krijgen hoe alles in elkaar draait. Maar nog meer, logs bevatten een schat aan informatie eens je op problemen stuit. En wanneer je problemen hebt, dan gebeurt het wel es dat je wat hulp kan gebruiken. De honderden lijnen logs zijn soms nu ook weer niet zo ideaal als je met je handen in het haar zit.
**Openhab2 logs**
Logs
De 2 interessante logs zijn terug te vinden onder /var/logs/openhab/...
Dit zijn:
>events.log: welke zaken loggen ivm triggers (bv sensoren)
openhab.log: zaken ivm openhab zelf (bv opstarten ed)
Debug logs
Een debug log bevat veel meer informatie, maar dit wordt meestal enkel gestart in geval van troubleshooting.
>/usr/share/openhab2/runtime/karaf/bin/client
log:set DEBUG org.openhab.binding.zwave
Check /var/log/openhab/* via another console
**Zwave logs**
Voor openHAB kan je online een handig tooltje vinden die de logbestanden van zwave net iets mooier rangschikt.
www.cd-jackson.com/index.php/openhab/zwave-log-viewer
Zoeken in (log) bestanden
Met grep kan je een woordje opzoeken in een bepaald bestand:
> grep IkZoekDit /var/log/zoekbestandje.log
Met egrpe kan je meerdere woorden ozken in een bepaald bestand:
> egrep "IkZoekDit|EnDit" /var/log/zoekbestandje.log
Met head of tail bekijk je het begin of het eind van een bestand. Je kan hier ook opties aan toevoegen om bv het aantal regels beter te weer te geven.
> tail -100 /var/log/zoekbestandje.log
Je kan nu beide commando's combineren, om zo de woordjes enkele te zoeken in die bepaalde regels.
> tail -100 /var/log/zoekbestandje.log | egrep "IkZoekDit|EnDit"
Met de optie -f onder tail kan je live het logbestandje bekijken. Dan zie je enkel die bepaalde zoeklijnen verschijnen op het moment dat het gebeurd. Heel handig als je iets aan het testen bent.
> tail -f /var/log/zoekbestandje.log | egrep "IkZoekDit|EnDit"
Verwijder oude log files
In order to clear (=delete) log-files, older then 30 days, without logrotate, I've put following script in place
vim /etc/cron.daily/azer_cleanup
find /tmp/azer_message_txt* -mtime +30 -type f -delete
find /home/azer/azer* -mtime +30 -type f -delete
The reason why I done it without logrotate, is because the log-files are created by external applications. So other names, rotation mechanism....
Schijfruimte vol?
I can happen that the diskspace is used forcely, but you can't find the files/folder that are using the space.
Sometimes, the files are deleted from the disk, but are still open by the application. So they aren't there, but they are still there. To find/see these files, you can use following command:
lsof | grep -i deleted
Kernels & schijf vol?
Soms gebeurt het wel eens dat de disk '/boot' vol loopt door de vele kernel updates.
Een makkelijk manier van dit te 'cleanen' is volgend commando:
Ubuntu/mint/...
purge-old-kernels
Centos
yum install yum-utils
package-cleanup --oldkernels --count=2
Update won't start cause of disk space
When you try to update, and you've got the error below (disk space critical):
>DISK CRITICAL - free space: /boot 50 MB (37% inode=99%);| /boot=83MB;;91;0;141
You can cleanup old kernels with following command.
This will leave the current and the last current in place (count = 2)
> package-cleanup --oldkernels --count=2
Zip / gzip / tar and beyond
Compressing files can gain you a lot of diskspace. That's one thing. But now a days, a disk isn't so expensive. So why still bother to compress your files? I use the zip function mostly to put all my files into one file. This is easier to transfer a bunch of files over the internet, mail, usb sticks...
To compress a complete folder into one file, you can use following command:
>tar -zcvf 20130403_something.tar.gz /folder/subfolder/subsub
Of course, after compressing something, you also want to decompress it:
>tar -zxvf 20130403_something.tar.gz
Look into zip files:
> zcat syslog-123123132-201608*gz | egrep -i 'sb029|sb060' | grep '^Aug'