vlan関連のメモ

ポートVLAN
1つの物理ポートは、1つのポートVLANにしか所属できない。
2台のLANスイッチにまたがって、2つのセグメント(VLAN1, VLAN2)をポートVLANで構成する場合、VLAN1用のケーブルとVLAN2用のケーブルを使い、それぞれ別の物理ポートで、LANスイッチ間を接続する。

VLAN ID
VLANIDは「VLAN識別子」とも呼ぶ。
VLAN IDは12ビット(4バイト)。12ビットで表現できる数は0~4095。しかしLANスイッチで設定可能なVLAN IDは1~4094(VLANは4094個まで作れる)。
0と4095は、IEEE802.1Qにおいて予約された値として規定しているため、VLAN IDとして使えない。
タグフレームの最大フレーム長は、1522バイト(1518+4)。


f:id:maruchan_shiro123:20141010063704p:plain

java 思いつくままに備忘録

PATH
javac, java などのコマンドをプルパス指定しない場合は、Windows等のOS側でPATH(環境変数)を設定しておく必要がある。
※私は、javaのインストール先を、別ドライブに指定していたため、PATHで指定しているドライブ名が書き換わっていて、eclipseが起動できない、という問題が生じた。ドライブ名はWindows側で変更が可能。(コントロールパネル→管理コンソールにドライブ管理、が確かあって、そこから変更)

PATHの変更方法はこちらを参照。

PATH and CLASSPATH (The Java™ Tutorials > Essential Classes > The Platform Environment)

しかし、windowsのPATHを変更してもcygwin側では、別のpathを使用してしまった。(コマンドプロンプトではjavaコマンドは使用可だが、cygwinではNG。通常はcygwinにも、windowsのPATHが引き継がれると思うのだけど。。)

とりあえず、~/.bashrcに以下を追加し、対処。

[~/.bashrc]
alias java='/cygdrive/e/java/jdk1.8.0_20/bin/java.exe'

Ubuntu でシリアルコンソールを使う

シリアル接続する際の、2通りの方法を書きます。
※ちなみに、最初はシリアル接続できましたが、何らかの理由で、接続できても、意味不明な文字が大量に画面に出力されるようになってしまい、現在シリアル接続できなくなってしまいました。数時間悩んだので、解決はあきらめてしまいましたが、、いずれ解決したいです(泣)

①gtkterm というアプリを使用する。

1.
ubuntuのソフトウェアセンターで「serial」と入力すると、
gtkterm というソフトがあるのでインストールする。

2.
gtktermを起動する。
GUIからgtktermを実行すると、アクセス権がないため、弾かれる。

teminalを起動して、「sudo gtkterm &」とすることで、
シリアル接続可能となった。



dmesgのログからシリアルポートが有効であることが分かるようだ.

dmesg

[    0.729071] Serial: 8250/16550 driver, 32 ports, IRQ sharing enabled
[    0.749461] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A


上記はこちらのサイトを参考にしました。

UbuntuでRS-232Cを利用したシリアルポート通信 |Linux 時々 XX



②screen コマンドを使う。

1.
screenパッケージをインストールする

sudo update
sudo apt-get install screen

2.
シリアル接続する

sudo screen /dev/ttyS0


こちらを参照しました。
SerialConsoleHowto - Community Help Wiki

Working with the serial console - ArchWiki

How to Install and Use Screen on an Ubuntu Cloud Server | DigitalOcean



■補足

/etc/default/grup のマニュアルは、以下で確認できる(設定ファイルに、そのようにコメントがある)

info -f grub -n 'Simple configuration'

ただ、上記マニュアルを閲覧する際の操作コマンドがlessやviと違って、ややこしかった。。(覚えようとするべきだけど。。)
なので、webのマニュアルを見た方が楽だった。

sedコマンドの使い方

sedコマンドで、複数の変換処理の指定をする

cat file |
 sed -e "s/OldText1/NewText1/g" \
     -e "s/OldText1/NewText1/g" |
 while read LINE
 do
  ...
 done

デリミタを / から変更する

 s%abc/ef%ABC-EF%g

文字列の変換

 sed -e "s/OldText/NewText/g" 

sedコマンドsはsubstitute、gはgloballyの略。

行頭の文字列を消す

 sed -e "s/^TextToRemove//"

行末の文字列を消す

 sed -e "s/TextToRemove\$//"

$はシェルの特殊文字なので、sedより先に解釈させてはいけない。
そこで \ を $ の前につけて、エスケープする必要がある。

文字列を追加する

 sed -e "s/abc/abcxyz/g"
 sed -e "s/^/TextToInsert/"
 sed -e "s/\$/TextToAppend/g"

文字列の追加は、任意の場所を指定することはできない。
するなら、行頭か行末を指定して、置換する。

ドットとアスタリスク

 sed -s "s/.*/abcd/"

括弧内の文字列を削除する

$ cat file 
0123456
abc(abc)jkl
mn(123)qrstp

$ sed -e "s/(.*)/()/" file
0123456
abc()jkl
mn()qrstp
$ 

idコマンドからユーザIDを取り出す

$ id | sed -e 's/uid=//' -e 's/(.*//'
1000
$ 

idコマンドからユーザ名を取り出す

$ id | sed 's/uid=.*(\(.*\)) gid=.*/\1/'
harue
$ 

括弧そのものを表す際は、そのままエスケープせずに()で良い。
グループ化する際の括弧は\(\)のようにエスケープする。

PATTERN以降を全て削除する

#!/bin/bash

STRING=abc/def/ghi
PATTERN=/
STRING=`echo "$STRING" | sed -e "s%$PATTERN.*%%"`
echo $STRING #=> abc

指定した位置から、ある文字数分を切り取る

$ STRING=abcdef
$ echo "$STRING" | cut -c1-5
abcde
$ 
  • c1-5(開始位置と長さ)を変数にすると、以下のようになる。
#!/bin/bash

STRING=abcdef
FIRST=1
LEN=5
echo $STRING | cut -c$FIRST-$LEN

awkを使って、ファイルの先頭から5文字だけを取り出す

$ awk '{printf "%-.5s\n", $0}' < file
abced
abc
abced
abce
abced
abced
$ 

%-.5s の部分は、先頭から最大5文字を取得する、という意味。
以下のmanを参照。

man 3 printf

The flag characters

- The converted value is to be left adjusted on the field boundary. (The default is right jus‐
tification.) Except for n conversions, the converted value is padded on the right with
blanks, rather than on the left with blanks or zeros. A - overrides a 0 if both are given.


The precision
An optional precision, in the form of a period ('.') followed by an optional decimal digit string.
Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify
that the precision is given in the next argument, or in the m-th argument, respectively, which must
be of type int. If the precision is given as just '.', the precision is taken to be zero. A nega‐
tive precision is taken as if the precision were omitted. This gives the minimum number of digits
to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the radix char‐
acter for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G
conversions, or the maximum number of characters to be printed from a string for s and S conver‐
sions.


また、上記は以下と同じ結果。

$ sed -e "s/^\(.....\).*/\1/" file 
abced
abc
abced
abce
abced
abced
$ 

間違えやすいが(私だけかもしれないが)、これは、5文字より少ない場合は。sedで置換されずに、そのまま出力される。
よって、3文字などの場合は、patternにマッチせずにそのまま出力されている。

正規表現については、以下に詳しい説明があった。
Regular Expressions - sed, a stream editor


sedコマンドの基本的な使い方

 sed -e "s/OldText/NewText/" samplefile
 sed -e "s/OldText/NewText/" samplefile > result
 sed -e "s/OldText/NewText/" < samplefile > result
 cat samplefile | sed -e "s/OldText/NewText/" > result
  • e オプションは、その後の文字列が編集用のコマンドだということを表す。

本来、複数の編集コマンドに対して-e を付けて指示する。
上記のように、1つの編集コマンドしか無い場合は、-eを省略して構わない。


変数を使った変換処理も可能。

 OLDTEXT=OldText
 NEWTEXT=NewText
 sed -d "s/$OLDTEXT/$NEWTEXT/" samplefile

ファイルの2行目だけを出力

 sed -n '2p' < samplefile

sedコマンドによって、置換された行だけを出力

 sed -n -e "s/OldText/NewText/gp" samplefile

※実際のファイルの内容は書き換わらない

Ubuntu Vimでクリップボードを使う

clipbord や xterm_clipboard などが + になっていない場合は、必要なものをインストールする

$ vim --version | grep clipboard
-clipboard       +iconv           +path_extra      +toolbar
+eval            +mouse_dec       +startuptime     -xterm_clipboard
$ 
sudo apt-get -y install vim-gtk vim-athena vim-gnome

.vimrcに以下を追加

set clipboard+=unnamedplus

<参考にしたサイト>

UbuntuにインストールしたVimでクリップボード共有する方法 | MBA-HACK


Accessing the system clipboard - Vim Tips Wiki

NTPサーバで時刻を合わせる

Tips

時刻補正には ntpdateコマンド が適さない場合がある
例えば、Dovecotというメールサーバーは6秒以上、時刻が巻き戻ると、サービスが落ちる可能性がある。
以下の公式Wikiサイトにntpdateを使うことは良いアイデアじゃないとの記載がある。
TimeMovedBackwards - Dovecot Wiki

ハードウェアクロック(リアルタイムクロック、BIOSクロック)
電源を切っていても動作し続ける。
精度は高くなく、適宜、修正する必要がある。
秒単位の粗い時刻情報しか保持しない。

システムクロック
起動時にハードウェアクロックを参照して取得した現在時刻を、Linuxカーネルが保持・管理する時刻。
ハードウェアクロックより、単位の細かい時刻情報(ミリ秒〜ナノ秒単位)を保持。

ハードウェアクロックを表示する

$ sudo hwclock  --debug
hwclock from util-linux 2.20.1
Using /dev interface to clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2014/09/20 13:05:44
Hw clock time : 2014/09/20 13:05:44 = 1411218344 seconds since 1969
2014年09月20日 22時05分44秒  -0.313228 seconds
$ 

このマイナス表記の時間は、「コマンド実行時の時刻と、画面に表示した際の時間差」

以下でも同じ結果となる

$ hwclock -r
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.
$ sudo hwclock -r --debuhwclock from util-linux 2.20.1
Using /dev interface to clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2014/09/20 23:46:37
Hw clock time : 2014/09/20 23:46:37 = 1411256797 seconds since 1969
2014年09月21日 08時46分37秒  -0.161288 seconds
$

$ sudo hwclock --show --debug 
hwclock from util-linux 2.20.1
Using /dev interface to clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2014/09/20 23:47:00
Hw clock time : 2014/09/20 23:47:00 = 1411256820 seconds since 1969
2014年09月21日 08時47分00秒  -0.500714 seconds
$ 


ハードウェアクロックの時刻をシステムクロックに合わせる
$ sudo hwclock -w

システムクロックの時刻をハードウェアクロックに合わせる
$ sudo hwclock -s

dateコマンドの使い方

オプション無しだと、現在時刻が表示される
オプションをつけると、システムクロックの日付や時刻を設定できる

コマンド書式

date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

月日・時分・西暦 の順で指定する点に注意。

時刻を公開NTPサーバと同期する方法①

ntpdateコマンドを使う

オプション無しで、NTPサーバを指定すると、瞬時に時刻同期する。

サーバー構築後にntpdateコマンドを使う場合は、「-B」オプションを使うか、ntpdというサービスを使い、徐々に時刻同期すると良い。

ntpdateコマンドで、公開NTPサーバと時刻同期する

$ sudo ntpdate jp.pool.ntp.org
21 Sep 11:09:08 ntpdate[3369]: adjust time server 157.7.154.134 offset 0.024619 sec
$ 

pool.ntp.org というドメインは、NTPサーバーの仮想クラスター。
このような公開NTPサーバには、「DNSラウンドロビン」という負荷分散の仕組みが備わっていて、1台のNTPサーバーに負荷が集中しないようになっている。
※上記と同様のコマンドを実行しても、2回目は違うIPアドレスが表示された。

一般ユーザーが利用できる公開NTPサーバーについては、以下のサイトに情報が乗っている。
NTP/推奨公開サーバ - wiki@nothing

NTPサーバーを構築する

構築の流れ
・ntpパッケージの導入
・上位サーバーやサービス提供先などの設定
・ntpサービスの起動
ファイアウォールudp/123ポートの開放

$ apt-get install ntp

上記サーバは /etc/ntp.conf に記述する。
通常はデフォルトのままで良い。

 # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
 # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for 
 # more information.
 server 0.ubuntu.pool.ntp.org
 server 1.ubuntu.pool.ntp.org
 server 2.ubuntu.pool.ntp.org
 server 3.ubuntu.pool.ntp.org


・NTPサーバを192.168.10.0/24のネットワーク内のサーバーやクライアントから利用可能にするには、ntp.confで以下のように設定する

 restrict 192.168.10.0 mask 255.255.255.0 notrap

ntpdサービスの起動

$ service ntp start
 * Starting NTP server ntpd                                                                     [ OK ] 
$ 

ntpサービスの自動起動

$ sudo update-rc.d ntp enable 2 3 4 5
update-rc.d: warning:  start runlevel arguments (none) do not match ntp Default-Start values (2 3 4 5)
update-rc.d: warning:  stop runlevel arguments (none) do not match ntp Default-Stop values (1)
 Enabling system startup links for /etc/init.d/ntp ...
 Removing any system startup links for /etc/init.d/ntp ...
   /etc/rc1.d/K77ntp
   /etc/rc2.d/K77ntp
   /etc/rc3.d/K77ntp
   /etc/rc4.d/K77ntp
   /etc/rc5.d/K77ntp
 Adding system startup for /etc/init.d/ntp ...
   /etc/rc1.d/K77ntp -> ../init.d/ntp
   /etc/rc2.d/S23ntp -> ../init.d/ntp
   /etc/rc3.d/S23ntp -> ../init.d/ntp
   /etc/rc4.d/S23ntp -> ../init.d/ntp
   /etc/rc5.d/S23ntp -> ../init.d/ntp
$ ls -l /etc/rc*.d/*ntp
lrwxrwxrwx 1 root root 13  9月 21 12:10 /etc/rc1.d/K77ntp -> ../init.d/ntp
lrwxrwxrwx 1 root root 13  9月 21 12:10 /etc/rc2.d/S23ntp -> ../init.d/ntp
lrwxrwxrwx 1 root root 13  9月 21 12:10 /etc/rc3.d/S23ntp -> ../init.d/ntp
lrwxrwxrwx 1 root root 13  9月 21 12:10 /etc/rc4.d/S23ntp -> ../init.d/ntp
lrwxrwxrwx 1 root root 13  9月 21 12:10 /etc/rc5.d/S23ntp -> ../init.d/ntp
$

ファイアウォール(firewalld)のインストール

ubuntuでは、デフォルトではファイアウォールは有効になっていない。
そこで、最近、iptablesサービスの代わりに、導入されているfirewalldをインストールする。(fedoraではfirewalldが標準)

$ sudo apt-get install firewalld

iptableからの違いとしては、firewalldでは、ポリシー設定が動的に反映されるようになったほか、ゾーンと呼ばれる概念が導入された。

firewall-cmdを使ったファイアウォールの設定

# デフォルトのゾーンを確認
$ firewall-cmd --get-default-zone
public

# publicゾーンで123/udpポートを許可
$ sudo firewall-cmd --add-port=123/udp --zone=public
success
$

# publicゾーンで常に123/udpポートを許可
$ sudo firewall-cmd --permanent --add-port=123/udp --zone=public
success
$