Linux things - youdar/How-to GitHub Wiki

Linux basic commands

Linux in 10 minuts

Basic linux commads

Some basic linux commands:

  • ctrl-z - suspends a job
  • jobs - list the current jobs
  • fg - resume the job that's next in the queue
  • fg [number] - resume job [number]
  • bg - Push the next job in the queue into the background
  • bg [number] - Push the job [number] into the background
  • kill %[number] - Kill the job numbered [number]
  • kill -[signal] %[number] - Send the signal [signal] to job number [number]
  • pkill -u username - Kill all processes of a user
  • ls -1 | wc -l List the number of files in a folder
  • du -h . | tail -1 List files and file sizes
  • du -k . | sort -n | tail another way to find folders that use lots of disc space
  • find . -name '*' | xargs wc -l number of rows in current folder .
  • `` lines of python and SQL code
  • sed '123!d' file_name Show row number 123 in file_name
  • nohup Run a command in the background

Search for a string in many tgz files

  • find . -name "*.tgz" | xargs zgrep -a ncs_from

Temporarily disable history logging:

  • disable: set +o history
  • enable: set -o history

Delete files with a particular extension

  • First check that you are deleting the correct files:

  • find . -name "*xyz" -type f

  • Then to delete

  • find . -name "*xyz" -type f -delete

  • Delete files older than days

  • find <directory path> -type f -mtime +<num of days>

  • `find -name "*." -type f -mtime + -delete

  • Other useful commands

  • tcpdump
    Example: to check connection to a machine, login as root and do
    tcpdump -XvSSn -i bond0.65 host 10.13.38.145

  • netstat
    Example: to check connection from a particular port netstat -an | grep 5433

  • traceroute

  • strace

  • To list the network open ports on your Linux server and the process that owns them use the following. lsof -P, sudo lsof -i, sudo netstat -lptu, 'sudo netstat -tulpn`

  • ps -ef report process status.

  • df -h "disk filesystem", gives full summary of available and used disk space usage. option -h will shows the file system disk

  • sudo su - : work as a root (to get out type exit) , su -

  • klist : list kerberos keytab users

  • split -l number_of_lines_in_each_segment file_to_spllit segments_prefix_ Splitting file

Without the xargs zgrep will only look at the names of the files passed, not in them

Enable automatic keytab renew

Create file: /usr/local/bin/kt_renewer.sh

kt_renewer.sh code
#!/bin/bash
# Get kerberos ticket for target principal using target keytab

[ $# -eq 2 ] || { echo "Usage: $0 <PRINCIPAL> <KEYTAB>"; }

# Send stdout/stderr script output into system logger
exec 1> >(logger -s -t $(basename $0)) 2>&1


PRINCIPAL="$1"
KEYTAB="$2"
DOMAIN=`echo $PRINCIPAL | awk -F@ '{print $2}'`

function check_kerberos_ticket {
  klist_entry=`klist | grep "krbtgt/$DOMAIN@$DOMAIN"`
  if [ $? -ne 0 ]; then
    retval=1
  else
    # check that the ticket was obtained less than 2 minutes ago
    ticket_start_time=`echo "$klist_entry" | awk '{print $1 " " $2}'`
    ticket_ts=`date -d "$ticket_start_time" +%s`
    current_ts=`date +%s`
    ts_diff=$((current_ts - ticket_ts))
    [ $ts_diff -le 180 ] && retval=0 || retval=1
  fi

  echo $retval
}

attempt=0
while true; do
  attempt=$((attempt+1))

  /usr/bin/kinit -kt "$2" "$1"

  result=`check_kerberos_ticket`
  [ "$result" -eq 0 ] && break

  [ "$attempt" -ge 6 ] && break
  sleep 300
done

exit $result

Add crontab entry:

crontab entry code ```bash @reboot (while true; do sleep 2; ping -c 1 -q `/usr/sbin/ip route show default 0.0.0.0/0 | awk '/default/ {print $3}'` &gt; /dev/null 2&gt;&amp;1 &amp;&amp; break; done); sleep $(($RANDOM \% 240)) && bash /usr/local/bin/kt_renewer.sh $USER@$DOMAIN.COM ~/$USER.keytab
</details>   

Setting the `keytab` file:
<details>  
   <summary>keytab setup stages</summary>  
```bash  
kinit \<user-name\>@$DOMAIN.COM    

<Enter password>     

rm  ~/\<user-name\>.keytab   

ktutil    

ktutil: addent -password -p \<user-name\>@$DOMAIN.COM -k 1 -e RC4-HMAC   

<Enter password>   

ktutil: wkt /home/\<user-name\>/\<user-name\>.keytab   

ktutil: q   

chmod 700 ~/\<user-name\>.keytab   

kinit -kt ~/\<user-name\>.keytab \<user-name\>@$DOMAIN.COM   

klist    

Find what processes are swapping, to identify issues with swap memory

for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r

Working from Windows

Adding linux like command

Create and add aliases in MicrosoftPowerShell_profile.ps1 For current user, this file is located in: %UserProfile%\Documents\Windows­PowerShell\profile.ps1

Create a shortcut for power shell, and in the properties set target to %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned

for example, MicrosoftPowerShell_profile.ps1 can contain

# Copied from http://msdn.microsoft.com/en-us/library/bb613488(VS.85).aspx
# To display the path to the Windows PowerShell profile, type:
# $profile
# To determine whether a Windows PowerShell profile has been created on the system, type:
# test-path $profile
# To create a Windows PowerShell profile file, type:
# new-item -path $profile -itemtype file -force
# To open the profile in Notepad, type:
# notepad $profile
#
# If you get a security error when opening powershel dCHANGE YOUR EXECUTION POLICY:
# (http://technet.microsoft.com/library/hh847748.aspx)
# Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# to cancel
# Set-ExecutionPolicy Undefined
# To be more secure you can create a PowerShell short-cut and in the properties change
# %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned
New-Alias np "C:\Program Files (x86)\Notepad++\notepad++.exe"
new-alias grep findstr
new-alias gitpath "C:\Users\Youval\AppData\Local\GitHub\GitHub.appref-ms"
function gitbash { gitpath --open-shell }
function pro { np $profile }
function which($cmd) { if ($cmd -ne $null){ get-command $cmd | select path} }

Installation of things

Python Anaconda

Instructions https://docs.continuum.io/anaconda/install#linux-install

Links to the files
https://docs.continuum.io/anaconda/hashes/lin-2-64.html

$ curl -o Anaconda2-4.1.1-Linux-x86_64.sh https://repo.continuum.io/archive/Anaconda2-4.1.1-Linux-x86_64.sh
$ md5sum Anaconda2-4.1.1-Linux-x86_64.sh
$ bash Anaconda2-4.1.1-Linux-x86_64.sh

Apache web server

Based on https://www.linode.com/docs/websites/apache/apache-web-server-on-centos-6

and https://support.rackspace.com/how-to/centos-6-apache-and-php-install/

installation

$ hostname
$ hostname -f
$ sudo yum update
$ sudo yum install httpd mod_ssl httpd mod_ssl

Python support

$ sudo yum install mod_wsgi

Start Apache

$ sudo service httpd start
$ sudo chkconfig httpd on

Reload Apache:

$ sudo /usr/sbin/apachectl restart

Apache config location: /etc/httpd/conf/httpd.conf

$ sudo nano /etc/httpd/conf/httpd.conf

Virtual hostslocation: /etc/httpd/conf.d/vhost.conf

nano adds a break-line at the end of the file, to avoid this use:

$ nano -L file_name

⚠️ **GitHub.com Fallback** ⚠️