Nov 6, 2016

iptables 룰 영구적으로 저장



1. create /etc/network/if-pre-up.d/iptables file and put

#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0


2. create /etc/network/if-post-down.d/iptables file and put

#!/bin/sh
iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.rules ]; then
    iptables-restore < /etc/iptables.rules
fi
exit 0



jkpark@cactus:~$ sudo chmod +x /etc/network/if-post-down.d/iptables
jkpark@cactus:~$ sudo chmod +x /etc/network/if-pre-up.d/iptables

Nov 5, 2016

How to setup vsftpd on ubuntu


jkpark@cactus:~$ sudo apt-get install vsftpd
다음 새 패키지를 설치할 것입니다:
  vsftpd
0개 업그레이드, 1개 새로 설치, 0개 제거 및 6개 업그레이드 안 함.
115 k바이트 아카이브를 받아야 합니다.
이 작업 후 336 k바이트의 디스크 공간을 더 사용하게 됩니다.
받기:1 http://kr.archive.ubuntu.com/ubuntu xenial/main amd64 vsftpd amd64 3.0.3-3ubuntu2 [115 kB]
내려받기 115 k바이트, 소요시간 0초 (974 k바이트/초)
패키지를 미리 설정하는 중입니다...
Selecting previously unselected package vsftpd.
(데이터베이스 읽는중 ...현재 211923개의 파일과 디렉터리가 설치되어 있습니다.)
Preparing to unpack .../vsftpd_3.0.3-3ubuntu2_amd64.deb ...
Unpacking vsftpd (3.0.3-3ubuntu2) ...
Processing triggers for systemd (229-4ubuntu11) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for man-db (2.7.5-1) ...
vsftpd (3.0.3-3ubuntu2) 설정하는 중입니다 ...
Processing triggers for systemd (229-4ubuntu11) ...
Processing triggers for ureadahead (0.100.0-19) ...
jkpark@cactus:~$

jkpark@cactus:~$ sudo vi /etc/vsftpd.conf

uncomment the below lines (line no:31, 35, 122, 123 and 125)
write_enable=YES
local_umask=022
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

chroot_local_user에 대해..
FTP접속 시 홈 디렉토리의 상위 디렉토리 이동을 막기 위한 설정.
Default 상태(chroot_local_user=YES에 주석처리되어 있음)
- ftp접속 후 pwd 시 '/home/user1' 로 나옴. 상위 이동 가능
chroot_local_user=YES 설정
- FTP접속 후 pwd 시 '/'로 나옴. 상위 이동 불가능
chroot_list_enable=YES 설정
- FTP 접속 실패. 
chroot_list 파일 생성
- FTP 접속 시 '/'로 나옴. 상위 이동 불가능
chroot_list 파일 안에 user1 등록
- user1은 ftp 접속 시 '/home/user1'로 나옴. 상위 이동 가능
- user2는 ftp 접속 시 '/'로 나옴. 상위 이동 불가능

즉 chroot_local_user=YES 설정은 ftp 접속 시 홈 디렉토리를 root 디렉토리 처럼 인식하도록 하는 설정이다.
http://blog.naver.com/jbells/220416391250



Add the following lines to enable passive mode
pasv_enable=Yes
pasv_min_port=40000
pasv_max_port=40100

Add the following lines to enable utf8
utf8_filesystem=YES

Add the following lines to enable write permission on root directory
allow_writeable_chroot=YES

Restart vsftpd service
jkpark@cactus:~$ sudo service vsftpd restart


jkpark@cactus:~$ sudo iptables -I INPUT -p tcp --destination-port 40000:40100 -j ACCEPT

jkpark@cactus:~$ sudo iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpts:40000:40100

Jun 15, 2015

byteToHexString

public class Main {
public static void main(String[] args) {
System.out.print(byteToHexString("abcde".getBytes()));
}

public static String byteToHexString(byte[] b) {
char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
StringBuffer sb = new StringBuffer();
for (byte i : b) {
sb.append(digit[((i & 0xF0) >> 4)]);
sb.append(digit[i & 0x0F] + " ");
}
return sb.toString();
}
}



output:
61 62 63 64 65

Dec 11, 2014

날짜 표현 DateFormat, Custom Date Text View



Date today;
String output;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat(pattern, currentLocale);
today = new Date();
output = formatter.format(today);
System.out.println(pattern + " " + output);



Customized Date and Time Formats
PatternOutput
dd.MM.yy30.06.09
yyyy.MM.dd G 'at' hh:mm:ss z2009.06.30 AD at 08:29:36 PDT
EEE, MMM d, ''yyTue, Jun 30, '09
h:mm a8:29 PM
H:mm8:29
H:mm:ss:SSS8:28:36:249
K:mm a,z8:29 AM,PDT
yyyy.MMMMM.dd GGG hh:mm aaa2009.June.30 AD 08:29 AM



Custom Date Text View

public class MDateView extends View {
 private final static String FORMAT_DEFULAT = "yyyy.MM.dd";

 private boolean isTickable = false;
 private boolean isColon = true;
 private Calendar mCalendar;
 private Runnable mTickerRunnable;
 private Handler mHandler;
 private String mFormat = FORMAT_DEFULAT;
 private String mDateString = "", mTimeString = "";
 private Paint mDatePaint, mTimePaint;

 public MDateView(Context context) {
  super(context);
  initClock(context);
 }

 public MDateView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initClock(context);
 }

 private void initClock(Context context) {
  if (mCalendar == null) {
   mCalendar = Calendar.getInstance();
  }

  mDatePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  mDatePaint.setColor(Color.argb(255, 255, 255, 255));
  mDatePaint.setTextSize(18);
  mDatePaint.setTextAlign(Align.LEFT);

  mTimePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  mTimePaint.setColor(Color.argb(255, 255, 255, 255));
  mTimePaint.setTextSize(24);
  mTimePaint.setTextAlign(Align.LEFT);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  canvas.drawText(mDateString, 5, 35, mDatePaint);

  int offsetX = 92;
  float time_X_Pos = (120 - mTimePaint.measureText(mTimeString, 0, mTimeString.length())) / 2.0f;// center align
  canvas.drawText(mTimeString, offsetX + time_X_Pos, 35, mTimePaint);

 }

 @Override
 protected void onAttachedToWindow() {
  super.onAttachedToWindow();
  isTickable = true;
  mHandler = new Handler();

  mTickerRunnable = new Runnable() {
   public void run() {
    if (!isTickable)
     return;

    mCalendar.setTimeInMillis(System.currentTimeMillis());
    mDateString = DateFormat.format(mFormat, mCalendar).toString();
    mTimeString = DateFormat.format("k" + (isColon ? ":" : " ") + "mm", mCalendar).toString();
    invalidate();

    long now = SystemClock.uptimeMillis();
    long next = now + (1000 - now % 1000);
    isColon = !isColon;
    mHandler.postAtTime(mTickerRunnable, next);
   }
  };
  mTickerRunnable.run();
 }

 @Override
 protected void onDetachedFromWindow() {
  super.onDetachedFromWindow();
  isTickable = false;
 }

 public void setFormat(String format) {
  if (mFormat == format)
   return;
  mFormat = format;
 }

Nov 18, 2014

Change keymap

The tools to manipulate the keyboard layout on the virtual consoles are loadkeys, dumpkeys and showkeys. Read their manpages and inform yourself about their intricacies.

Note that these tools only work in a virtual console, not in a terminal emulator in X.

Save your current keyboard layout:
$ dumpkeys > backup.kmap

In case something goes wrong you might be able restore your keymap using the command:
$ sudo loadkeys backup.kmap
If the keyboard is so messed up that you can't even do this then your only option not involving ancient kernel magic is to reboot.

Check which keycodes are assigned to your keys:
$ showkey
Now press the ESC key and the CAPSLOCK key. The keycodes should show up on the screen. Note the keycodes. On my system the ESC has the keycode 1 and CAPSLOCK has the keycode 58. showkey will terminate after 10 seconds of inactivity.

Note the names of the ESC and CAPSLOCK keys from dumpkeys:

$ dumpkeys | grep 1
...
keycode   1 = Escape
...
$ dumpkeys | grep 58
...
keycode  58 = CtrlL_Lock
...
Note the keymap line from dumpkeys:

$ dumpkeys | head -1
keymaps 0-127

Create a keymap file which switches ESC and CAPSLOCK:
keymaps 0-127
keycode   1 = CtrlL_Lock
keycode  58 = Escape

Load the keymap:
$ sudo loadkeys swap_esc_capslock.kmap



original link