Thursday, December 27, 2012

How to Disable SElinux

Important note!  I do not recommend on disabling SElinux!
This article is meant for study purpose only!

as you know SElinux provides sophisticated security mechanism for your system.
sometimes you might want to temporarily disable SElinux  in order to see if its the reason for the problems you experience. Even if its the reason, you shouldn't disable its permanently! you should find anther solution!

First of all you need to know the possible selinux modes. taken from wiki.centos.org:

  • Enforcing: The default mode which will enable and enforce the SELinux security policy on the system, denying access and logging actions
  • Permissive: In Permissive mode, SELinux is enabled but will not enforce the security policy, only warn and log actions. Permissive mode is useful for troubleshooting SELinux issues
  • Disabled: SELinux is turned off

1. Temorarily switch to permissive mode:


Type in the following command:

echo 0 > /selinux/enforce
1 - enforcfing 
0 - permissive

2. permanently switch to permissive mode:

edit /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=permissive 
# SELINUXTYPE= can take one of these two values:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
As you can see SELINUX is set to permissive. 
Dont forget to reboot after the change.

1. DISABLE SElinux

edit /etc/selinux/config and this time set it like that: SELINUX=disabled

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
dont forget to reboot!

for none Redhat/Centos os append "enforcing=0" to the end of the kernel line:

title Mint (2.6.22-194.26.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.22-194.26.1.el5 ro root=LABEL=/ console=tty0,19200n8 enforcing=0
        initrd /initrd-2.6.22-194.26.1.el5.img


Thursday, October 11, 2012

Nice & Renice: manage the priority of processes

With nice and renice you can control the priority each new process given.

Each process is given an nice number which represents his priority among other processes in the system.
The default nice number given to process is 0

The range of nice numbers is -20 to 19.
-20 is the "highest" priority number. If you want to give your process the highest priority, give it the nice number minus 20
19 is the lowest. it might take time for your process to start running (if at all).

now lets see some examples:

1. Set Nice Number

nice  -n 19  ./scriptos
In this example I started my script "scriptos" with the nice number 19.
I can check the actual priority of the process in two ways:

ps  axl  |  grep  scriptos
top
The priority will be under PRI column.

2. Change Nice Number

You can change the priority of your process by changing the nice number with renice.
Lets look on the following example:

renice  -n  12  474
12 - the new nice number of the scriptos (instead of 19).
474 - the PID of scriptos.




Tuesday, October 2, 2012

8 Tar Examples You Should know

Hey readers,

Today i'm gonna show you some tar command examples.

When created, tar was used for data archiving on tapes.
today we use tar to collect different kinds of files together. the tar files themselves also know as tar balls

Lets start with simple example:

here the content of the directory I use for this example



total 0
-rw-r--r--. 1 root root 0 Sep 30 16:13 file1
-rw-r--r--. 1 root root 0 Sep 30 16:13 file2
-rw-r--r--. 1 root root 0 Sep 30 16:13 file3


1. Create Tar File


tar  -cvf  all.tar  file1  file2  file3

In this example we creating tar file with the following flags:

c - create
v - verbose. gives us more outpout about the process of creating the tar file
f - file (instead of tape for example)

all.tar will be our new tar file (tarball) which include the rest of files - file1, file2 and file3

we could use long options like create verbose and files and we also could separate them: -c -v -f


2. List the contents of a tar file

tar  -tf  all.tar

The -t option (which means table of contents) will show us which files are included in a specific tar file.
in this example the output will be:

file1
file2
file3

for long list, you will might want to use the -v option (verbose)

3. Create compressed tar archive- gzip,bz2 

for gzip:

tar   zcvf  all_files.tgz  file*
This will create an tar archive and compress is with gzip. Its the same as tar.gz file.
the z here means compress it with gzip

for bzip2:

tar   jcvf  all_files.tbz file*
Same example, only this time we using bzip2 compression. Its the same sa tar.bz2 file.
the j means use bzip2

4. Untar a tar file

I find myself more often extract files from tar files than create them. Here how you should extract files from the tar file "all.tar"

tar  xvf  all.tar
You could use the -C option for diffrent extract location.


5. Append files to tar file

You can always update your tar file by adding new files to it.
Suppose we created new file called "luigi". lets add "luigi" to tar file called "mario"


tar  rvf  mario.tar  luigi
We used the option -r to append the file (you can use the long option, --append).


6. Move entire directory tree with tar

This is very popular usage of tar. Lets see how it looks:


tar  cf  -  * |  (cd  /some_dir  && tar xf - )
1. Creates tar of your current directory (You can choose any path you want)
2. Opening new sub shell and changing to the destination directory
3. Extracts the tar from first stage in destination directory

*note: you can just use the -C option. but in older versions of tar this option does not exists


7. Exclude files when creating tar

Lets assume we got anther file in our directory that named file101
We want that every file in our directory will archived except file101:


tar  cvf  all_files.tar  --exclude='file101'  *
You can also use list of files with -X option

8. Extract specific file from tarball

Sometimes you'll want to extract one particular file from a tar file.
you can just specify the file name to get that done:


tar  xvf  all_files.tar  file1 
This command will extract only file1 from the tar.

That's it for this post.
Hope you'll find this useful.

Sunday, September 30, 2012

3 frequently asked questions about SSH

Open SSH
Hello Everyone,

Welcome to my first post in English (well, you can say its the second one). 

Today I'm gonna talk about three frequently asked questions i get on my job about SSH.

For those of you who dont familiar with the open ssh suite, I recommend you read about it here: http://www.openssh.org/


1. I get "connection refused" via ssh. How to solve this error?

first of all, check if the sshd daemon is running on the remote computer. You can do with following command:

service sshd status

you should get something like: "openssh-daemon is running"
If you getting something different you might found the problem. you can start the service like that:

service sshd start
I recommend you use ssh with the -v option which means "verbose". you will get more information about the command you trying.

try checking the status of ssh ports with:

netstat -tlp | grep ssh
if the iptables is on or any other kind of firewall, that could be anther reason for "connection refused".
you could also try to search the logs for something useful: /var/logs/messages 

2. How to use SSH for remotely executing commands?

Well, this answer is quite simply. for example the following will reboot the remote computer mario

ssh root@mario reboot
I use this kind of commands for checking logs on remote computer. It can be very useful.
Be aware that you might asked for password, which leads us for the next question:


2. How to use SSH without password?

when using ssh for remote actions (connect, command execution) the system will prompt you for password.
you can cancel the prompt and connect without password!.
Its very useful for sysadmins who works quite often on remote servers.

there are few simple steps for it to be done:

1. on the local computer:

ssh-keygen 
Press Enter key for each default option.
This is will create public and private key pair.

2. make sure on the remote computer there is a direcotory named .ssh in the user directory. if not create it

3. next copy the public key to the remote computer:

scp  ~/.ssh/id_rsa.pub  remote_linux:~/.ssh/
4. connect without password to the remote computer! =)

Thats it for this post.
Hope you will find this info helpful.

Happy Sukkot Holiday =)



Linux IT announcement

Hello readers,

I decided that from now on, Linux IT is going to be written in the English language only.

This decision derives from two main reasons:

1. Share Knowledge with more people - I would like to make my blog accessible for as many people as i can. and what better way to do it rather than using global language like English? 

2. My English is suck - I can read articles and books in English (At least that) but my writing and speaking is kinda suck as you see... so that's a great opportunity to do something with that...dont you think?

So, Hope you will could read my future posts =)

Until the next time..

Linux IT

Thursday, September 27, 2012

10 פקודות Find שפשוט חייבים להכיר

לינוקס זה קבצים. כך תמיד היה. רוב הדברים במערכת שלנו הם קבצים - devices, links, sockets ועוד.
לכן תמצאו את הכלי find כשימושי במיוחד בעבודה שלכם עם מערכת ההפעלה.

בפוסט הזה אני אנסה לתת לכם טעימה קטנה מfind עם 10 דוגמאות שחייבים להכיר =)

lets go

1. שימוש בסיסי

לפני שאננו קופצים למים העמוקים בואו נראה דוגמה בסיסית לשימוש בfind:

find  /  -name "*.txt" 
אז מה בעצם אנחנו רואים כאן?

/ הוא המיקום שממנו אנו רוצים להתחיל. שימוש נפוץ נוסף הוא פשוט לשים נקודה. הכוונה בנקודה היא המקום בו אנו נמצאים עכשיו.

name- אומר שהביטוי הבא הוא חלק מהשם של הקובץ (או השם כולו) שאנו רוצים לחפש.
בדוגמה הזאת השתמשתי בWildCards. הכוונה בכוכבית היא 0 תווים או יותר ולא משנה באיזה תו מדובר.
מה שאנחנו מחפשים כאן זה למעשה את כל הקבצים שמסתיימים בtxt - דרך שימושית למצוא את כל הקבצים מסוג מסויים.


2. זמן רבותיי, זמן

לעיתים קרובות נרצה לדעת אילו קבצים שונו היום, אילו לפני חודש ואולי רק יומיים ומעלה.  את כל אלה ויותר נוכל לבדוק באמצעות find

find /tmp -mtime 2
בדוגמה אנו מחפשים את כל הקבצים ששונו לפני יומיים בדיוק. ניתן להשתמש בסימנים + - כדי לחפש קבצים ותיקיות ששונו לפני יותר מיומיים או פחות מיומיים.

אופציות חשובות נוספות הן:
atime - access time למעשה הזמן שבו נעשה שינוי בקובץ שלא בתוכן שלו.
ctime - change time הזמן שבו השתנו מאפייני הקובץ כמו הרשאות, metadata וכו'


3. הרשאות

find מאפשרת לנו לחפש גם קבצים עם הרשאות מסויימות

find  . -perm 646
בדוגמה הנ"ל אנו מחפשים את כל הקבצים עם הרשאות קריאה וכתיבה לowner ולothers והרשאות קריאה לgroup.

באמצעות not- ניתן למצוא את כל הקבצים שאין להם את ההרשאות הנ"ל.

בנוסף ניתן לחפש בצורה סימבולית. למשל  



find  . -perm -ug=rwx

4. סוג קובץ

לפעמים המטרה שלנו בחיפוש היא לאו דווקא למצוא קובץ רגיל. לעיתים נרצה לחפש תיקיות ובפעמים אחרות block device. לכן חשוב להכיר איך לחפש גם סוגי קבצים אחרים.


find /var -type d 

בדוגמה הנ"ל אנחנו מחפשים את כל התיקיות תחת var/
כדי ללמוד על סוגים נוספים ניתן להסתכל בדפי הman של find


5. חפש ובצע

אחת מהאופציות החזקות (ומהמסוכנות) שיש לfind להציע זאת אופציית הexec
exec מאפשרת לנו לבצע כל פעולה שנרצה על הקבצים שמצאנו בעת החיפוש


find ~ -name *.txt -exec rm -f {} \;
בדוגמה הנ"ל אנו מחפשים את כל הקבצים שמסתיימים בtxt ולאחר מכן מוחקים את אותם קבצים.
זאת דוגמה לשימוש מסוכן בפקודה. אין לדעת מה נעלה בעת החיפוש. לכן יש לשים לב ולנסות לעשות את זה בזהירות. בדוגמה הזאת למשל, הייתי שם i- ולא f-


6. קצת עומק

בברירת מחדל find מחפש בכל תתי התיקיות בנתיב שאנו מציינים.
כדי להגביל את עומק החיפוש ניתן לציין את האפשרות maxdepth


find / -maxdepth 2 -name "mario"
maxdepth 2 פירושו חפש בנתיב שצויין ושלב אחד למטה. כך למשל הוא יחפש גם בתיקייה etc/ אך לא מעבר לכך.
ניתן להתחיל חיפוש גם בעומק מסויים (כלומר לא מהנתיב שצויין) עם האפשרות mindepth


7. חדש יותר

מקודם ציינתי שניתן לחפש לפי זמן מסויים שקובץ נערך או בוצעה גישה אליו. ניתן לבצע חיפוש יותר חכם בהקשר הזה של מודיפקציה של קבצים.


find . -newer file1


הפקודה תחפש בנתיב הנוכחי את כל הקבצים ששונו לאחר זמן השינוי של file1
שימושי במקרה ואתם רוצים לראות את השינויים האחרונים בתיקייה מסויימת.

8. גודל

לפעמים נרצה לאתר את הקבצים הכי גדולים בעץ קבצים שלנו על מנת לפנות כמה שיותר מקום. האופציה size מאוד שימושית במקרה הזה:


find /tmp -size +38k 

כאן אתם יכולים לראות דוגמה לחיפוש של קבצים מעל 38 קילובייט. ניתן לחפש לפי גדלים שונים. M גדולה למשל ייחפש לנו בגדלים של Megabytes

9.   Iname

לינוקס היא case sensitive. כלומר הקובץ file והקובץ File הם קבצים שונים בתכלית.
לא תמיד ברור לנו מה שם הקובץ שאנחנו מחפשים. גם כשאנחנו כבר יודעים איזה קובץ אנחנו מחפשים, לא תמיד נהיה בטוחים איך הוא מאויית (אות גדולה בהתחלה? הכל אותיות קטנות? גדולות?) בשביל זה בדיוק יש את iname


find / -iname data
בדוגמה הנ"ל אנחנו מחפשים את כל הקבצים שנקראים data. זה לא משנה אם הם רשומים כDATA או Data. 
find ימצא אותם בכל מקרה.

10. משתמשים וקבוצות

נניח שמשתמש מסויים עזב את הארגון שלנו ואנחנו רוצים לוודא שכל הקבצים ששייכים לו נמחקו מהאחסון.
find מאפשר לנו לחפש גם לפי שיוך קבצים למשתמש וקבוצה מסויימת.


find /users/doron -uid 500 -group doron

נניח שהuid של doron הוא 500 וגם של הקבוצה ששמה כמו שם המשתמש (doron). בדוגמה אנו מחפשים את כל הקבצים ששייכים למשתמש doron ולקבוצה שלו. find מאפשרת לנו לחפש עם האפשרויות uid וgid, ומצד שני גם מילולית עם user וgroup.

מספר טיפים

לעיתים תתקלו בדברים לא רצויים עם הפקודה find.

אחת מה"תקלות" הנפוצות בעבודה עם find היא חוסר הרשאות למיקום מסויים במערכת שמוציא לנו שגיאות יחד עם הפלט של החיפוש. כדי לסנן את השגיאות הללו מומלץ להשתמש בredirection כך:

find / -name "*.mkv" 2> /dev/null

דבר נוסף שנרצה לעשות לעיתים קרובות הוא לחפש במספר מקומות בו זמנית. כדי לעשות את זה פשוט מפרידים בין המקומות באמצעות רווח.


find /tmp  /var/log  /home/arie -name log

זהו. זה הכל להפעם.
ממליץ לכם לקרוא קצת יותר על find בtdlp או בדפי הman כדי לקבל מושג יותר טוב על מלוא היכולות של הכלי הזה.

שתהיה חופשה נעימה וחג שמח =)




Monday, September 10, 2012

איך להציג מידע על המערכת הפעלה והקרנל

שלום לכולם,

פקודה שתציג לנו מידע על המערכת שבה אנו משתמשים יכולה להיות שימושית מאוד, במיוחד בסביבה בה יש לנו עשרות ולפעמים אף מאות שרתים הנבדלים ביניהם במערכות ההפעלה.

היום אני אראה לכם מספר פקודות שיעזרו לכם לזהות את המערכת הפעלה בה אתם משתמשים.

שימו לב - הפקודות שמתוארות בפוסט נוסו על מערכת ההפעלה red hat. הכלי uname הוא כלי נפוץ שנמצא כמעט בכל מערכת הפעלה לינוקס אחרת שקיימת כיום.

ניגש למלאכה:

גרסת ההפצה של Red Hat

אצלי בעבודה קיימות גרסאות רבות של Red Hat וכיוון שיש הבדל משמעותי ביניהן אני מוצא את עצמי הרבה פעמים משתמש בפקודה הבאה:

cat /etc/redhat-release
פלט לדוגמה:

Red Hat Enterprise Linux Server release 6.2 (Santiago)

שם המחשב שלכם

רוצים לדעת לאיזה מחשב אתם מחוברים עכשיו? הפקודה הבאה (מקבילה לפקודה hostname) תציג לכם את שם המחשב והdomain בו אתם נמצאים:

uname -n
פלט לדוגמה:


localhost.local

הצגת כל המידע

לרוב, לא נרצה לזכור את השימוש של כל אופציה ומה בדיוק היא מציגה.
הדרך הקצרה והיעילה ביותר היא פשוט להציג הכל עם האפשרות a-

uname -a 
פלט לדוגמא:

Linux mario.domain 2.4.12-122.2.1.el6.x86_64 #1 SMP Tue AUG 22 12:26:34 EST 2012 x86_64 x86_64 x86_64 GNU/Linux

מידע על ה-Kernel

כדי למצוא מידע ספציפי על גרסת השחרור של הKernel יש להשתמש באפשרות r-

uname -r
על מנת לראות את כל האפשרויות של הפקודה ממליץ לקרוא את הman שלה:

man uname

 מספר אפשרויות נוספות שאולי יועילו לכם:


uname -p = processor type
uname -o = operating system
uname -i = hardware platform
uname -m = machine hardware name


עד הפעם הבאה =)