linux/unix utilities

1. How to kill a known process

Step #1: First, you need to find out process PID (process id)

Use ps command or pidof command to find out process ID (PID). Syntax:
# ps aux | grep processname
pidof processname

For example if process name is lighttpd, you can use any one of the following command to obtain process ID:
# ps aux | grep lighttpd

OR use pidof command which is use to find the process ID of a running program:
# pidof lighttpd

Step #2: kill process using PID (process id)

Above command tell you PID of lighttpd process. Now kill process using this PID:
# kill PID
OR
# kill -9 PID
Where, -9 is special Kill signal, which will kill the process.


2. How to kill the process running on specific port in linux

Step #1: Find Out Which Process Is Listening Upon a Port

Type the following command:
# netstat -tulpn | grep :PID
OR
# fuser PID
OR
lsof -i :PID

Step #2: kill process using PID (process id)

same as previous Step #2.


3. Find out process name associated with PID and current working directory of a process

To find out process name associated with PID, type the following command:
# ls -l /proc/PID/exe

To find out current working directory of a process, type the following command:
# ls -l /proc/PID/cwd
OR
# pwdx PID

linux vps hakkında

  1. Yeni kullanıcı oluştur
  2. Ubuntu VPS kurulumu sonucunda size bir root kullanıcısı verilecek. Ubuntu için root aslında siz istemediğiniz sürece verilmeyen bir kullanıcı. Ancak VPS için illa ki bir root kullanıcısı üretilir ve bu kullanıcının şifresi size verilir. Root kullanıcısını SSH bağlantısı için kullanmak bir güvenlik açığı oluşturacağı için root ile değil, sudo yetkilerine sahip başka bir kullanıcı ile bağlantı sağlamak uygun olacaktır. adduser komutu ile yeni kullanıcı oluşturulur.
    adduser yenikullanici

  3. Yeni kullanıcıyı sudo grubuna ekle
  4. Kullanıcıyı oluşturduktan sonra bu kullanıcıya sudo yetkileri vermek için sudo grubuna eklemek gerekiyor. Yine adduser komutu ile yeni kullanıcıyı sudo grubuna ekliyoruz.
    adduser yenikullanici sudo

  5. Yeni kullanıcı için sudo yetkileri kontrolü yap
  6. Yeni eklediğimiz kullanıcının sudo yetkilerine gerçekten sahip olduğunu kontrol etmek üzere root üzerinden yaptığımız SSH bağlantsını kapatıp yeni kullanıcı ile bağlanalım ve birkaç sudo gerektiren komut girelim. Böylece yetkileri kullanabildiğimizden emin olalım. örneğin, sudo adduser deneme komutunu çalıştırıp şifremizi girdiğimizde yenikullanici is not in the sudoers file. This incident will be reported. hatası veriyorsa sudo yetkilerine sahip değilsiniz :) (bu arada yetkilere sahipseniz ve deneme kulanıcısını haybeye eklediyseniz sudo deluser -f deneme komutu ile bu kullanıcıyı silebilirsiniz :) kullanıcının silinip silinmediğinden emin olmak için cat /etc/passwd | grep deneme :)

  7. root için SSH erişimini kapat
  8. Yeni kullanıcı sudo yetkilerine sahip olduğuna göre artık root kullanıcısının SSH bağlantısını kapatmak ve SSH sunucusunu yeniden başlatmak gerekiyor.
    Dikkat: Eğer yeni kullanıcı ile sudo yapılamıyorsa, root’u SSH’a kapattığınızda VPS’e SSH bağlantısını sudo yetkileri ile yapabileceğiniz başka bir kullanıcı olmayacağı için tüm sunucuyu kaybetme riskiniz var. (Tecrübe konuştu. Uyarıldınız:)
    SSH yapılandırma dosyası /etc/ssh/sshd_config. Bu dosyadaki şu satırı buluyoruz:
    PermitRootLogin yes
    Bu satırın anlamı root kullanıcısının SSH bağlantısı yapabilmesidir. Buradaki yes yerine no yazarak root’un SSH bağlantsı yapmasını engelliyoruz:
    PermitRootLogin no
    SSH sunucusunu yeniden başlatmak için sudo service ssh restart
    Artık root ile değil, diğer sudo yetkili kullanıcı ile SSH bağlantısı yapacağız.
    (thanks to hosaf)


ssh yapmak için:
ssh username@ssh_server

iki makina arası secure kopyalama için:
scp source_file username@ip_address:/target_file

ssh tunnel:
ssh -D 8080 -fN username@ssh_server

ssh Local Port Forwarding:
ssh -L 8080:www.selcukc.com:80 host
host burada username@localhost olabilir. direk localhost yazarsak da hangi kullanıcıysak onu alır.. (host’tan kasıt ssh serverdaki kullanıcı) şimdi browsera http://localhost:8080/ yazarsak selcukc.com a ssh tunnel kullanarak gitmiş oluruz..
mesela (saçma bi örnek ama olayı anlamak için..) ssh -L 8080:whatismyipaddress.com:80 username@ssh_server yazıp browserın proxy ayarlarından http proxy ye localhost verip porta 8080 dersek hangi adres yazarsak yazalım (80. portu kullanan) bizi whatismyipaddress.com a yönlendirir. orada görünecek olan ip adresi ssh_server ın ip adresi olacak..
(bu tuneli kapatmak için sudo killall ssh)

useful things about linux

How to change system time on CentOS

http://abdussamad.com/archives/343-CentOS-Linux:-Setting-timezone-and-synchronizing-time-with-NTP-.html

    1. Setting the timezone

      • Login as root
      • To see what the current timezone is: “date
      • To change the timezone first look at what timezones are available by running the following command on the command line interface: “ls /usr/share/zoneinfo
      • Then simply delete the current timezone: “rm /etc/localtime
      • And replace it with a symbolic link to the new timezone from /usr/share/zoneinfo. For example if your chosen zone is Pacific time: “ln –s /usr/share/zoneinfo/PST8PDT /etc/localtime”
    2. Synchronizing time with NTP server

      • Network Time Protocol (NTP) is a standard way of synchronizing computer clocks across a network. Using NTP you can keep your server’s clock synchronized with super accurate atomic clocks located around the world. Computer clocks tend to “drift” so regularly synchronizing them with NTP servers helps keep them accurate.
        The first step is to make sure you have the ntp program installed. Do a: “which ntpdate
      • If its not available type: “yum install ntp
      • Once ntp is installed synchronize your computer clock with: “ntpdate 0.us.pool.ntp.org
      • You are not limited to the above server. There are numerous NTP servers around the world. You can find a complete list at ntp.org.
        Also keep in mind that ntp only affects the system time. The hardware clock on your server will not reflect that. So you want to set it as well so that the correct time is maintained after reboot: “hwclock --systohc
    3. ntpd

To keep your server clock automatically synchronized you can run the ntpd daemon which is installed as part of the ntp package.
Edit the /etc/ntp.conf file to comment out the following lines:
#server 127.127.1.0 # local clock
#fudge 127.127.1.0 stratum 10

The above two lines can sometimes prevent ntpd from properly synchronizing your clock. They are already commented out by default on CentOS/RHEL 6. But on 5.x you have to comment them out manually.
Finally type the following two commands to start the daemon and make it run automatically at boot up:
"service ntpd start"
"chkconfig ntpd on"


How to Create Desktop Shortcuts in Ubuntu

Type the following command at the prompt and press Enter

sudo apt-get install --no-install-recommends gnome-panel

The --no-install-recommends option makes sure that only the required packages and no recommended packages get installed. This saves space on your hard drive.

When the installation is done, you can create a new shortcut using the command line. Type the following command at the prompt and press Enter.

gnome-desktop-item-edit --create-new ~/Desktop

You can also create shortcuts by pressing Alt + F2, entering the command listed above in the edit box, and pressing Enter. The Create Launcher dialog box displays as shown above and you can create another shortcut.


How to easily resize images in linux

Open a terminal and run this command:

convert  -resize 50% source.png dest.jpg

It will reduce the size by 50%

You can also specify the size:

convert  -resize 1024X768  source.png dest.jpg