Thursday, May 15, 2014

Linux - How to change default sshd port

There are many reasons why default port 22 for ssh is need to be changed. If you need to change default port or add other listening port:

1. open /etc/ssh/sshd_config with your favorite text editor
2. find line
Port 22

and

edit this value to your needs.

or if you want to add other listening port, simply add line with port. Example:

Port 22
Port 1000

after editing save config file and restart sshd:

$ sudo /etc/init.d/ssh restart

or

$ service sshd restart

dependant on system you use.

File /etc/ssh/sshd_config opened in editor



Monday, May 12, 2014

Bash - Substring

How to get substring in Bash script? Simple :)

if we have string:

$ a=123456789

substring from position 1 to 5 is

$ echo ${a:1:5}
23456

First position in a string is 0.

in general: ${varname:start_pos:length}


Sunday, May 11, 2014

Linux - Make ISO image of CD

If we want to make exact copy of cd media type, because Linux handles everything as file, we need to read all data from one file and write it to imagefile on disk.

$ cat /dev/cdrom >./imagefile.iso

another way is using dd command:

dd if=/dev/cdrom of=imagefile.iso

Saturday, May 10, 2014

Linux - Printing first or last 10 lines

There are many examples when we need to print first or last lines of some file. Commands head and tail ca help to do this.

Command head prints 10 first lines of file.

$ head file

or

$ cat file|head

There is command line argument -n, which says how many lines to be printed.

Example:

$ head -n 5 file

prints first 5 linest from file.

if number is negative, command head prints last n lines from file.

Command tail does oposite from command head: prints last 10 lines from a file.

$ tail file

same as command head there is argument -n which tells to command head how many last lines from a file is to be printed:

$ tail -n 7 file

prints last 7 lines from file.

If you want to print all lines in file excluded first n lines, command is:

$ tail -n +5 file

prints all lines in file, excluded first 5 lines.

Command tail is very useful in log analyzing, when you are not interested in whole file, but only last n lines.

Friday, May 9, 2014

Linux - Find ssh attacks

All login attempts, successful or not, are in /var/log/auth.log file. To find from which IP addresses attempts are coming, and only for valid usernames, execute following line:

$ cat /var/log/auth.log|grep -v "invalid"|grep "Failed password"|awk '{ print $(NF-3) }'|sort|uniq

grep -v "invalid" - -v switch reverts filter then only lines without word "invalid" are included
grep "Failed password" - only lines with words "Failed password" are included
awk '{ print $(NF-3) }' - Take 3rd arg from last
sort
and print only unique IP addresses.


Also, if you want to find on which usernames attacks are targeted change awk part into: awk '{ print $(NF-5) }'





Thursday, May 8, 2014

Linux - File Checksum

The purpose of file checksums is to validate integrity of files. There are many ways to do checksums on Linux, but the simplest is by using md5sum command (for MD5 checksum). Format is:
md5sum [options] [file1] ... [file n]

Options are:
-b or --binary - binary mode
-t or --text - text mode (default)
-c or --check - read file with checksums and check them

For test purpose make one file named test

$ md5sum test
ee10c66475e841d934c0e39d07650d4a  test

output md5 checksum to file:
$ md5sum test >checksum

check:
$md5sum -c checksum
test: OK

MD5 gives 128 bit checksum. For security related purposes is better to use SHA-2 algorithm implemented in: sha224sum, sha256sum, sha384sum and sha512sum programs.








Wednesday, May 7, 2014

Linux - Add User to a Group

1. To add existing user to existing group:

$ usermod -G groupname username

or 

$ usermod -g GID username

2. To add existing user to existing supplementary groups

$ usermod -a -G groupname username

-a is switch for append

3. To add user to group when adding user

$ adduser --ingroup groupname username

4. To check which groups user is member of

$ groups username

Tuesday, May 6, 2014

Bash script: User Input

For user general input in variable we use command read:

#!/bin/bash
read -p "Enter first number:" num1
read -p "Enter second number:" num2
if [ num1 -eq num2 ]
then
   echo "Numbers are equal"
else
   if [ $num1 -lt $num2 ]
   then
      echo "Second number is larger"
   else
      echo "First number is larger"
   fi
fi

For entering exact expected word (example Yes or No) we use command select:

#!/bin/bash
echo "Do you want continue?"
select inp in "yes" "no"
do
   case $inp in
      yes ) echo "You want to continue"
              break
              ;;
      no ) echo "You want to exit"
             break
             ;;
   esac
done


Monday, May 5, 2014

Bash FOR loop

For loop is a structure that executes a number of times, generally known in advance. In Bash script syntax of FOR loop is:

for VARIABLE [in LIST]; do COMMANDS BLOCK; done

or

for VARIABLE [in LIST]
do
   COMMANDS_BLOCK
done

LIST is a list of words or numbers. If LIST is omitted $@ is implied.

Simple examples:
1. This example prints all files in directory

dirlist=$(ls)
for i in $dirlist
do
   echo $i
done


2. This example prints numbers from 1 to 10

for i in {1..10}
do
   echo $i
done

or

for i in 1 2 3 4 5 6 7 8 9 10
do
   echo $i
done 

3. This example prints number from 1 to 10 with step 2

for i in {1..10..2}
do 
   echo $i
done

4. This example prints 

Sunday, May 4, 2014

Bash - Exit code

1. Exit code from Bash script

argument of exit command is exit code
exit n
example (exit_example.sh)

#!/bin/bash
exit $1  #return argument 1 as return code

2. Bash last exit code

last exit code is in variable $?

#!/bin/bash
./exit_example.sh 2
echo $?

result is:
2

Saturday, May 3, 2014

Bash - Join Arrays

How to join arrays in Bash Script? Very simple :) Example:

#!/bin/bash
a1=(a b c d) #array 1
a2=(e f g h) #array 2
a=(${a1[@]} ${a2[@]}) #join array
echo ${a[@]}

Output is:
a b c d e f g h

Make array of arrays and result is joined array :)

Friday, May 2, 2014

Customize Bash prompt

Bash prompt text is defined in PS1 variable. PS1 variable is declared in .bashrc file which is in home directory. Open this file with your favorite text editor: ~/.bashrc
Find declaration of PS1 variable (maybe there are few of them) and, if you want to permanent change prompt, change this declaration to suit your needs. If you want to change prompt temporally (recommended!) until restart, type in Bash command prompt:

$ PS1="NEW PROMPT --> "
NEW PROMPT -->

If you look in default string there are some special character with some system parameters as:

  • \u username
  • \h hostname
  • \w working directory
  • ...


$ PS1="\h:\w"
linips:~


Thursday, May 1, 2014

Linux - Add User Account

To add user to Linux from command line (shell) type command adduser followed with options and username. Example to add user linips (type sudo if you are not root):

$ sudo adduser linips
Adding user `linips' ...
Adding new group `linips' (1001) ...
Adding new user `linips' (1001) with group `linips' ...
Creating home directory `/home/linips' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for linips
Enter the new value, or press ENTER for the default
        Full Name []: Linips Blog
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]

Defaults are in file /etc/adduser.conf

What if we are want to add user with username format as firstname.secondname?

$ sudo adduser linips.blog
adduser: Please enter a username matching the regular expression configured
via the NAME_REGEX[_SYSTEM] configuration variable.  Use the `--force-badname'
option to relax this check or reconfigure NAME_REGEX.

Then we must add --force-badname switch to command

$ sudo adduser --force-badname linips.blog

and continue with entering data for new user.