2018년 2월 25일 일요일

Turn Off drag to maximize effect in Ubuntu 16.04

Install unity-tweak-tool.

sudo apt-get install unity-tweak-tool
 

click window-snapping in unity-tweak-too.


Ubuntu - 16.04 hangul setting

 

http://hochulshin.com/ubuntu-1604-hangul/

한글 설치

  1. sudo apt-get install fcitx-hangul로 한글을 설치한다.
  2. System Settings > Language Support를 실행해서 아직 완전히 설치되지 않다고 표시되는데 잠시 기다려서 모두 설치한다.
  3. Keyboard input method system:을 ibus가 아닌 fcitx로 변경한다.
  4. 재부팅한다.

한영 전환 설정

Shortcut 설정

  1. AllSettings > Keyboard > Shortcuts Tab > Typing을 선택한다.
  2. Switch to Next source, Switch to Previous sourc, Compose Key, Alternative Characters Key를 모두 Disabled로 선택한다. Disabled로 선택하기 위해서는 backspace를 누르면 된다.
  3. Compose Key의 Disabled를 길게 눌러 Right Alt를 선택한다.
  4. Switch to next source는 한영키를 눌러 Multikey를 선택한다. 반드시 Compose Key 설정이 먼저되어야 Multikey를 선택할 수 있다.
  5. AllSetting 윈도우를 닫고 상단 메뉴바 오른쪽의 입력기 선택하는 것을 본다. 키보드 표시가 된 것이 fcitx이다. fcitx아이콘을 눌러서 Configure Current Input Method를 선택한다.
  6. Keyboard-English(US)가 있다면 +를 눌러 Hangul을 추가한다. (Uncheck “Only Show Current Language”). Korean이 아닌 Hangul이여야 한다.
  7. Global Config tab에서 Trigger Input Method는 한/영키를 눌러 Multikey로 설정(왼쪽 오른쪽 모두)하고 Extrakey for trigger input method는 Disabled로 설정한다. (Mac에서는 command key이므로 대신 shift+space를 선택한다.)
  8. Global Config tab에서 Program > Share State Among Window > All을 선택한다.

테스트

  1. log out을 한후 다시 log in을 한다.
  2. 한글/영어가 한영키로 전환되는지 확인한다.

2017년 7월 14일 금요일

reset pulse audio sound

Current solution, Kill pulseaudio

pulseaudio -k && sudo alsa force-reload 

If still can't restart audio use -D option to restart

  1. pulseaudio --check
    pulseaudio -k && sudo alsa force-reload  
    pulseaudio -D 
 
second solution   
show list of audio devices
] aplay -l

remove any sound cache in pulse audio
] rm -r ~/.config/pulse/* followed by rm -r ~/.pulse*  

pulseaudio -k && sudo alsa force-reload 

2017년 3월 11일 토요일

How to use GPG


gpg --gen-key
 

If need some entropy to generate random number, install random number tool

sudo apt-get install rng-tools to set it up.
Then run sudo rngd -r /dev/urandom before generating the keys.


How to export GPG private key.
 gpg --export-secret-key -a USER_ID_HERE > GPG_KEY_FILE_NAME

2017년 1월 8일 일요일

fail2ban setting.

https://www.howtogeek.com/675010/how-to-secure-your-linux-computer-with-fail2ban/


https://www.digitalocean.com/community/tutorials/how-to-protect-ssh-with-fail2ban-on-centos-7

Let's begin by writing a very simple version of jail.local. Open a new file using nano (or your editor of choice):

  • sudo nano /etc/fail2ban/jail.local
Paste the following:
/etc/fail2ban/jail.local
[DEFAULT]
# Ban hosts for one hour:
bantime = 3600

# Override /etc/fail2ban/jail.d/00-firewalld.conf:
banaction = iptables-multiport

[sshd]
enabled = true
This overrides three settings: It sets a new default bantime for all services, makes sure we're using iptables for firewall configuration, and enables the sshd jail.
Exit and save the new file (in nano, press Ctrl-X to exit, y to save, and Enter to confirm the filename). Now we can restart the fail2ban service using systemctl:
  • sudo systemctl restart fail2ban
The systemctl command should finish without any output. In order to check that the service is running, we can use fail2ban-client:
  • sudo fail2ban-client status
Output
Status |- Number of jail: 1 `- Jail list: sshd
You can also get more detailed information about a specific jail:
  • sudo fail2ban-client status sshd

Explore Available Settings

The version of jail.local we defined above is a good start, but you may want to adjust a number of other settings. Open jail.conf, and we'll examine some of the defaults. If you decide to change any of these values, remember that they should be copied to the appropriate section of jail.local and adjusted there, rather than modified in-place.
  • sudo nano /etc/fail2ban/jail.conf

Default Settings for All Jails

First, scroll through the [DEFAULT] section.
ignoreip = 127.0.0.1/8
You can adjust the source addresses that Fail2ban ignores by adding a value to the ignoreip parameter. Currently, it is configured not to ban any traffic coming from the local machine. You can include additional addresses to ignore by appending them to the end of the parameter, separated by a space.
bantime = 600
The bantime parameter sets the length of time that a client will be banned when they have failed to authenticate correctly. This is measured in seconds. By default, this is set to 600 seconds, or 10 minutes.
findtime = 600
maxretry = 3
The next two parameters that you want to pay attention to are findtime and maxretry. These work together to establish the conditions under which a client should be banned.
The maxretry variable sets the number of tries a client has to authenticate within a window of time defined by findtime, before being banned. With the default settings, Fail2ban will ban a client that unsuccessfully attempts to log in 3 times within a 10 minute window.
destemail = root@localhost
sendername = Fail2Ban
mta = sendmail
If you wish to configure email alerts, you may need to override the destemail, sendername, and mta settings. The destemail parameter sets the email address that should receive ban messages. The sendername sets the value of the "From" field in the email. The mta parameter configures what mail service will be used to send mail.
action = $(action_)s
This parameter configures the action that Fail2ban takes when it wants to institute a ban. The value action_ is defined in the file shortly before this parameter. The default action is to simply configure the firewall to reject traffic from the offending host until the ban time elapses.
If you would like to configure email alerts, you can override this value from action_ to action_mw. If you want the email to include the relevant log lines, you can change it to action_mwl. You'll want to make sure you have the appropriate mail settings configured if you choose to use mail alerts.

Settings for Individual Jails

After [DEFAULT], we'll encounter sections configuring individual jails for different services. These will typically include a port to be banned and a logpath to monitor for malicious access attempts. For example, the SSH jail we already enabled in jail.local has the following settings:
/etc/fail2ban/jail.local
[sshd]

port    = ssh
logpath = %(sshd_log)s
In this case, ssh is a pre-defined variable for the standard SSH port, and %(sshd_log)s uses a value defined elsewhere in Fail2ban's standard configuration (this helps keep jail.conf portable between different operating systems).
Another setting you may encounter is the filter that will be used to decide whether a line in a log indicates a failed authentication.
The filter value is actually a reference to a file located in the /etc/fail2ban/filter.d directory, with its .conf extension removed. This file contains the regular expressions that determine whether a line in the log is bad. We won't be covering this file in-depth in this guide, because it is fairly complex and the predefined settings match appropriate lines well.
However, you can see what kind of filters are available by looking into that directory:
  • ls /etc/fail2ban/filter.d
If you see a file that looks to be related to a service you are using, you should open it with a text editor. Most of the files are fairly well commented and you should be able to tell what type of condition the script was designed to guard against. Most of these filters have appropriate (disabled) sections in jail.conf that we can enable in jail.local if desired.
For instance, pretend that we are serving a website using Nginx and realize that a password-protected portion of our site is getting slammed with login attempts. We can tell Fail2ban to use the nginx-http-auth.conf file to check for this condition within the /var/log/nginx/error.log file.
This is actually already set up in a section called [nginx-http-auth] in our /etc/fail2ban/jail.conf file. We would just need to add an enabled parameter for the nginx-http-auth jail to jail.local:
/etc/fail2ban/jail.local
[DEFAULT]
# Ban hosts for one hour:
bantime = 3600

# Override /etc/fail2ban/jail.d/00-firewalld.conf:
banaction = iptables-multiport

[sshd]
enabled = true

[nginx-http-auth]
enabled = true
And restart the fail2ban service:
  • sudo systemctl restart fail2ban

Monitor Fail2ban Logs and Firewall Configuration

It's important to know that a service like Fail2ban is working as-intended. Start by using systemctl to check the status of the service:
  • sudo systemctl status fail2ban
If something seems amiss here, you can troubleshoot by checking logs for the fail2ban unit since the last boot:
  • sudo journalctl -b -u fail2ban
Next, use fail2ban-client to query the overall status of fail2ban-server, or any individual jail:
  • sudo fail2ban-client status
  • sudo fail2ban-client status jail_name
Follow Fail2ban's log for a record of recent actions (press Ctrl-C to exit):
  • sudo tail -F /var/log/fail2ban.log
List the current rules configured for iptables:
  • sudo iptables -L
Show iptables rules in a format that reflects the commands necessary to enable each rule:
  • sudo iptables -S

2016년 12월 24일 토요일

unordered_set / unordered_map with custom object in C++

http://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key



To be able to use std::unordered_map (or one of the other unordered associative containers) with a user-defined key-type, you need to define two things:
  1. A hash function; this must be a class that overrides operator() and calculates the hash value given an object of the key-type. One particularly straight-forward way of doing this is to specialize the std::hash template for your key-type.
  2. A comparison function for equality; this is required because the hash cannot rely on the fact that the hash function will always provide a unique hash value for every distinct key (i.e., it needs to be able to deal with collisions), so it needs a way to compare two given keys for an exact match. You can implement this either as a class that overrides operator(), or as a specialization of std::equal, or – easiest of all – by overloading operator==() for your key type (as you did already).
The difficulty with the hash function is that if your key type consists of several members, you will usually have the hash function calculate hash values for the individual members, and then somehow combine them into one hash value for the entire object. For good performance (i.e., few collisions) you should think carefully about how to combine the individual hash values to ensure you avoid getting the same output for different objects too often.
A fairly good starting point for a hash function is one that uses bit shifting and bitwise XOR to combine the individual hash values. For example, assuming a key-type like this:
 
struct Key
{
  std::string first;
  std::string second;
  int         third;

  bool operator==(const Key &other) const
  { return (first == other.first
            && second == other.second
            && third == other.third);
  }
};
Here is a simple hash function (adapted from the one used in the cppreference example for user-defined hash functions):
namespace std {

  template <>
  struct hash<Key>
  {
    std::size_t operator()(const Key& k) const
    {
      using std::size_t;
      using std::hash;
      using std::string;

      // Compute individual hash values for first,
      // second and third and combine them using XOR
      // and bit shifting:

      return ((hash()(k.first)
               ^ (hash()(k.second) << 1)) >> 1)
               ^ (hash()(k.third) << 1);
    }
  };

}
With this in place, you can instantiate a std::unordered_map for the key-type:
int main()
{
  std::unordered_map<Key,std::string> m6 = {
    { {"John", "Doe", 12}, "example"},
    { {"Mary", "Sue", 21}, "another"}
  };
}
It will automatically use std::hash as defined above for the hash value calculations, and the operator== defined as member function of Key for equality checks.
If you don't want to specialize template inside the std namespace (although it's perfectly legal in this case), you can define the hash function as a separate class and add it to the template argument list for the map:
struct KeyHasher
{
  std::size_t operator()(const Key& k) const
  {
    using std::size_t;
    using std::hash;
    using std::string;

    return ((hash()(k.first)
             ^ (hash()(k.second) << 1)) >> 1)
             ^ (hash()(k.third) << 1);
  }
};

int main()
{
  std::unordered_map<Key,std::string,KeyHasher> m6 = {
    { {"John", "Doe", 12}, "example"},
    { {"Mary", "Sue", 21}, "another"}
  };
}
How to define a better hash function? As said above, defining a good hash function is important to avoid collisions and get good performance. For a real good one you need to take into account the distribution of possible values of all fields and define a hash function that projects that distribution to a space of possible results as wide and evenly distributed as possible.
This can be difficult; the XOR/bit-shifting method above is probably not a bad start. For a slightly better start, you may use the hash_value and hash_combine function template from the Boost library. The former acts in a similar way as std::hash for standard types (recently also including tuples and other useful standard types); the latter helps you combine individual hash values into one. Here is a rewrite of the hash function that uses the Boost helper functions:
#include 

struct KeyHasher
{
  std::size_t operator()(const Key& k) const
  {
      using boost::hash_value;
      using boost::hash_combine;

      // Start with a hash value of 0    .
      std::size_t seed = 0;

      // Modify 'seed' by XORing and bit-shifting in
      // one member of 'Key' after the other:
      hash_combine(seed,hash_value(k.first));
      hash_combine(seed,hash_value(k.second));
      hash_combine(seed,hash_value(k.third));

      // Return the result.
      return seed;
  }
};
And here’s a rewrite that doesn’t use boost, yet uses good method of combining the hashes:
namespace std
{
    template <>
    struct hash<Key>
    {
        size_t operator()( const Key& k ) const
        {
            // Compute individual hash values for first, second and third
            // http://stackoverflow.com/a/1646913/126995
            size_t res = 17;
            res = res * 31 + hash()( k.first );
            res = res * 31 + hash()( k.second );
            res = res * 31 + hash()( k.third );
            return res;
        }
    };
}

2016년 12월 18일 일요일

random number generation in C++. with better distribution.

random number generation in C++.  with better distribution.


int getRand() //generater random number between 1 to 5
{
    std::default_random_engine generator;
    generator.seed(std::random_device()());
    std::uniform_int_distribution distribution(1,5);
    int randomNumber = distribution(generator);
    return randomNumber;
}

sublime close without confirmation

  Close without confirm   Yes, you can just write a plugin to set the view as scratch and close it. Then create a keybinding for that c...