4 Ekim 2020 Pazar

systemctl komutu - Ubuntu Kullanır

Giriş
systemctl "Service Manager" olarak düşünülmeli. systemd kullanan işletim sistemlerindeki komutlardan bir tanesi systemctl. Açıklaması şöyle.
systemd gives us the systemctl command which is mostly used to enable services to start at boot time. We can also start, stop, reload, restart and check status of services with the help of systemctl.

We can do sudo systemctl enable service_name, and the service will automatically start at boot time. We can also disable services not to start at boot time.
Ubuntu 15.04'ten itibaren bu yapıya geçmeye başladı.

servisismi.service dosyası
Systemd İçin Service Dosyası yazısına taşıdım.

Bash Ornekleri
Örnek
Şöyle yaparız
#!/bin/sh

service=$1
if 
  systemctl | grep -q "$service"
then
  systemctl status "$service"
else
  echo "The service doesn't exist"
fi
Komut Satırı Seçenekleri
Giriş
Kullanım : systemctl + seçenek + servis_ismi şeklindedir.

daemon-reload seçeneği
/etc/systemd/system/ dizini altındaki dosyalarda bir ayar değiştiyse kullanılır. Şöyle yaparız.
systemctl daemon-reload
Daha sonra yeni servisi veya değişen servisi tekrar durdurup başlatmak gerekir. Şöyle yaparız
sudo systemctl start zookeeper
sudo systemctl status zookeeper
disable seçeneği
Açıklaması şöyle.
Running systemctl disable removes the symlink to the service in /etc/systemd/system/*.
From now on, that service won't start on boot anymore.
Örnek
Şöyle yaparız.
systemctl disable snmpd
Örnek
Şöyle yaparız.
sudo systemctl disable apache2
enable seçeneği
Örnek
Şöyle yaparız.
sudo systemctl enable /etc/systemd/system/macoverride.service
Örnek
Eğer tek bir komut ile enable + start yapmak istersek şöyle yaparız
systemctl enable --now
is-active seçeneği
Örnek
Şöyle yaparız.
systemctl is-active --quiet myservice
Örnek
Şöyle yaparız.
systemctl is-active --quiet service && echo Service is running
isolate seçeneği
Kendi dosyamı çalıştırmak için şöyle yaparız.
# systemctl isolate maintenance.target
Belli bir seviyedeki servisleri çalıştırmak için şöyle yaparız.
# systemctl isolate multi-user.target
list-unit seçeneği
Açıklaması şöyle
systemd units can be listed with systemctl list-units for currently active and systemctl -a list-units for all known units.
Örnek
Şöyle yaparız.
systemctl list-units | grep -E 'service.*running'
mask seçeneği
Örnek - poweroff yeteneğini iptal etme
Şöyle yaparız
sudo systemctl mask poweroff.target
...
sudo systemctl unmask poweroff.target
reboot seçeneği
UEFI (Unified Extensible Firmware Interface) ekranına girebilmeyi sağlar.
Örnek
Şöyle yaparız
systemctl reboot --firmware-setup
restart seçeneği
Örnek
Şöyle yaparız.
systemctl restart NetworkManager
Örnek
Şöyle yaparız.
systemctl restart sshd
show seçeneği
Açıklaması şöyle.
Show properties of one or more units, jobs, or the manager itself. If no argument is specified, properties of the manager will be shown. If a unit name is specified, properties of the unit are shown, and if a job ID is specified, properties of the job are shown. By default, empty properties are suppressed. Use --all to show those too. To select specific properties to show, use --property=. This
command is intended to be used whenever computer-parsable output is required. Use status if you are looking for formatted human-readable output.
Şöyle yaparız.
$ systemctl show -p SubState --value NetworkManager
running
Şöyle yaparız.
$ systemctl show -p ActiveState --value x11-common
inactive
$ systemctl show -p SubState --value x11-common
dead
start seçeneği
Şöyle yaparız.
systemctl enable ssmatt.service
systemctl start ssmatt.service
status seçeneği
Örnek
Tüm servisleri görmek için şöyle yaparız.
systemctl status
Çıktı olarak ağaç yapısı alırız.
├─systemd-journald.service
         │ └─318 /lib/systemd/systemd-journald
         ├─fwupd.service
         │ └─1703 /usr/lib/fwupd/fwupd
         ├─systemd-networkd.service
         │ └─395 /lib/systemd/systemd-networkd
         └─cups-browsed.service
           └─2918 /usr/sbin/cups-browsed
lines 172-194/194 (END)
Örnek - servis ismi
Servis ismi olarak x.service ye da sadece x kullanılabilir.

Şöyle yaparız.
systemctl status bluetooth
 bluetooth.service - Bluetooth service
  Loaded: loaded ...
  Active: inactive (dead)
     Docs: man:bluetoothd(8)
Şöyle yaparız
systemctl status sshd
Örnek - pid
Elimizde şöyle bir bilgi olsun.
systemd(1)───foo(37775)─┬─{foo1}(37782)
                        ├─{foo2}(37783)
                        └─{foo3}(37784)
Şöyle yaparız.
$ systemctl status 37775
stop seçeneği
Örnek
Şöyle yaparız
systemctl stop snmpd
Örnek
Normalde şöyle yapmak gerekir.
systemctl stop media-backup.automount
servis ismi için dosya yolu kullanmak istersek şöyle yaparız.
systemctl stop "$(systemd-escape -p --suffix=automount /media/backup)"
-t seçeneği
type anlamına gelir. Açıklaması şöyle.
-t, --type=
       The argument should be a comma-separated list of unit types such as
       service and socket.

       If one of the arguments is a unit type, when listing units, limit
       display to certain unit types. Otherwise, units of all types will
       be shown.

       As a special case, if one of the arguments is help, a list of
       allowed values will be printed and the program will exit.

   --state=
       The argument should be a comma-separated list of unit LOAD, SUB, or
       ACTIVE states. When listing units, show only those in the specified
       states. Use --state=failed to show only failed units.

       As a special case, if one of the arguments is help, a list of
       allowed values will be printed and the program will exit.
Çalışmakta olan servisleri görmek için şöyle yaparız.
systemctl --type=service --state=running list-units
--user seçeneği
Sistem service manager" yerine kullanıcının "service manager" uygulamasını kullanmak için şöyle yaparız.
systemctl --user enable my_example.service
systemctl --user start my_example.service
...
systemctl --user stop my_example.service
systemctl --user status my_example.service




















30 Eylül 2020 Çarşamba

taskset komutu - Uygulamanın Hngi İşlemciye Bağlanacağını Belirtir

Giriş
Komut satırından uygulama başlatılırken, hangi işlemciye bağlanmak istendiği belirtilir. Açıklaması şöyle
taskset komutu bir uygulamayı istenilen çekirdek yada çekirdekler üzerinde koşulmasını sağlayan komuttur.
Kullanım şekli şöyle
taskset [options] mask command [arg]...
taskset [options] -p [mask] pid
Örnek
Açıklaması şöyle
The below command will launch Google Chrome browser in CPU 1 & 2 (or 0 and 1). The mask is 0×00000003 and command is “google-chrome”.
Şöyle yaparız.
taskset 0×00000003 google-chrome
-c seçeneği
Bitmask kullanmak yerine CPU listesini verir.
Örnek
Şöyle yaparız
taskset -c 0 ffmpeg ...
Örnek
Şöyle yaparız
taskset -c 1 java -cp demo-all.jar main.TestLoop

/etc/services Dosyası - Port Numaralarına İsim Verir

Giriş
Açıklaması şöyle.
The canonical list of services is maintained by the IANA, but you can add local definitions; you might even see a “# Local services” comment at the end of your /etc/services file already.

Örnek
Şöyle yaparız
my-service-name    1234/tcp
Açıklaması şöyle.
Once that’s done, you’ll be able to write “0.0.0.0:my-service-name” instead of “0.0.0.0:1234”.
ssh
Örnek
Şöyle yaparız.
$ grep ssh /etc/services  
ssh             22/tcp         # SSH Remote Login Protocol 
ssh             22/udp

$ grep http /etc/services  
http            80/tcp          www             # WorldWideWeb HTTP            
http            80/udp                          # HyperText Transfer Protocol

28 Eylül 2020 Pazartesi

getfacl komutu

Giriş
ls komutu ile bakınca "+" karakteri görüyorsak özel izinler atanmış anlamına gelir. Açıklaması şöyle
The + indicates that the file has an Access Control List (ACL) with additional permissions.

getfacl {dir/file} to get more info on what is set

setfacl -b {dir/file} to remove ACL.
Örnek
Şöyle görürüz
(directory)rwxr-x---+

Ansible

Kurulum
Şöyle yaparız
sudo apt install ansible
Master/Worker Kurulumu
Master'da ssh-keygen ile üretilen public key, worker bilgisayara kopyalanır

/etc/ansible/hosts Dosyası
Bilgisayar grupları bu dosyada tanımlanır

Örnek
workers isimli bir grup tanımlamak için şöyle yaparız
[workers]
10.24.0.1
10.24.0.2
Kullanmak için şöyle yaparız
-hosts: workers
  remote_user: root
Bağlantı Ayarları
Ansible normalde uzaktaki bilgisayara bağlanmak için ssh kullanır. Eğer kendi Linux sistemimizde bir şey çalıştıracaksak şöyle yaparız
If you are connecting to localhost you shouldn't even need to use ssh, just make sure ansible_connection=local is set in your inventory for your localhost entry
Bu durumda şöyle yaparız
[mylocalhost]
localhost ansible_connection=local
Eğer localhost yerine farklı bir isim kullanmak istersek bir örnek burada.

Windows İçin Bağlantı
Windows uzak bağlantı için winrm kullanır.

Örnek
Şöyle yaparız
[windows]
13.233.160.153
[windows:vars]
ansible_user=LocalUsername
ansible_password=Password
ansible_connection=winrm
ansible_winrm_transport=basic
ansible_winrm_server_cert_validation=ignore
Bir başka örnek şöyle. Burada port numarası da belirtiliyor.
[windows]
13.233.160.153

[windows:vars]
ansible_port=5985
ansible_connection=winrm
ansible_winrm_transport=basic
ansible_user=<<ansible_user>>
ansible_password=<<ansible_password>>

Playbook Nedir?
Yaml formatında yazılırlar. Açıklaması şöyle. Playbokk asenkron da çalışabilir.
So rather than chaining ad-hoc commands together, you can set up a Playbook to run multiple commands at once.
...
Ansible provides the Asynchronous feature to allow an operation to run asynchronously such that the status may be checked. This can prevent interruption from SSH timeouts for long running operations.
Playbook Çıktısı
Çıktı olarak şuna benzer bir şey alırız
PLAY RECAP ******************************************************************************
 : ok=2    changed=1    unreachable=0    failed=0
Açıklaması şöyle
Here changed=1 means, playbook made a change in worker nodes. If we run the playbook again, you would see this changed=0 . Which means there is no state change.
Modules
Module one small specific task içindir. Playbook module'leri çalıştırır

Değişkenler
Örnek
Şöyle yaparız
-hosts: databases
  remote_user: root
  vars:
    tablename: foo
    tableowner: someuser

  tasks:
    -name Rename table {{ tablename }} to bar
      postgresql_table
        table: {{ tablename }}
        rename: bar

    -name Set owner to someuser
      postgresql_table
        table: {{ tablename }}
        owner: someuser
Kubernetes
Bazı örnekler burada

Roles
Büyük bir playbook'un daha küçük parçalara bölünmüş hali gibi düşünülebilir. Yani roles içinde birden fazla playbook bulunur. Şöyle yaparız
ansible-galaxy init /etc/ansible/roles/apache --offline
Şeklen şöyle
defaults
  -- main.yml
files
handlers
  -- main.yml
meta
  -- main.yml -- Role ile ilgili açıklamalar
tasks
  -- main.yml
templates
tests
  -- inventory
  -- test.yml
vars
  -- main.yml
tasks/main.yml
Diğer dosyaları çağırmak için şöyle yaparız
- include_tasks: install-docker-registry.yml
  run_once: true
  when: internal_registry == "docker_registry"
ansible-playbook komutu
Örnek
Şöyle yaparız
ansible-playbook komutu runsetup.yml
Örnek
Şöyle yaparız
ansible-playbook komutu runsetup.yml --syntax-check
Module Kullanımları
"become:true" hedef bilgisayarda "root" olmak istediğimizi belirtir.

apt module
Şöyle yaparız.
tasks:
  - name: Install VLC Media Player
    apt:                            # the package manager
      name: vlc-bin
      state: latest
Açıklaması şöyle
To remove a package, we just have to use the same syntax as the installation playbook. But
state: absent
command module
Şöyle yaparız
---
  - name: "Check if User exists sample"
    hosts: localhost
    connection: local
    vars:
      users:
        - nginx
        - root
        - user4
        - user3

    tasks:
      - name: "check for user in /etc/passwd"
        command: getent passwd {{ item }}
        register: check_user
        ignore_errors: yes 
        loop: "{{ users }}"
        register: all_checks
      
      - name: "iterate over checks"
        debug:
          msg:
          - "{{ item.rc }}"
          - "{{ item.item }}"
        when:
          - item.rc == 0
        loop: "{{ all_checks.results }}"
copy module
Açıklaması şöyle
The copy module is often used in writing playbooks when we want to copy a file from a remote server to destination nodes.
Örnek
Şöyle yaparız
- name : Copy configuration file
  copy: src=/data/httpd.original dest=/etc/httpd/conf/httpd.conf
Örnek
Şöyle yaparız
ansible test-servers -m copy -a 'src=/home/knoldus/Personal/blogs/blog3.txt dest=/tmp' 
  -u ec2-user
ping module
workers grubundaki tüm bilgisayarlara ping atmak için şöyle yaparız
ansible workers -m ping
Çıktı olarak şunu alırız
 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
Örnek
Açıklaması şöyle
---
  - name: installvlc      # name of the playbook
    hosts: workers        # where we need to install
    become: true          # run as sudo user
shell module
Açıklaması şöyle
When we want to run UNIX commands then we use shell module
Örnek
Şöyle yaparız
ansible test-servers -m setup -u ec2-user
service module
Açıklaması şöyle
When we want to ensure the state of a service that is service is running we use the service module.
Örnek
Şöyle yaparız
ansible test-servers -m service -a 'name=httpd state=started' -become -u ec2-user
setup module
Açıklaması şöyle
The setup module is used when we want to see the information of all the hosts, their configuration, and detailed information.
Örnek
Şöyle yaparız
ansible test-servers -m shell -a 'ls -la' -u ec2-user
yum module
Örnek
Şöyle yaparız. Burada -become seçeneği -u seçeneğinden önce kullanılmalı
ansible test-servers -m yum -a 'name=httpd state=present' -become -u ec2-user
Örnek
Şöyle yaparız
- name : Install httpd
  yum: name=httpd state=latest