Blog categories

Comments

[Linux] IP 주소 이용하여 hostname 자동 변경 하기

[Linux] IP 주소 이용하여 hostname 자동 변경 하기

IP 주소의 맨 뒷자리 숫자를 얻어오는 코드는 다음과 같다.

ifconfig | grep Bcast | awk '{print $2}' | cut -f 4 -d '.'

만약 숫자에 추가로 덧샘이나 뺄샘이 필요하다면 다음과 같이 수정 가능하다.

ifconfig | grep Bcast | awk '{print $2}' | cut -f 4 -d '.' | awk '{print $1 - 10}'

숫자 앞에 별도의 알파벳이나 기호 등을 붙이기 위해서는 다음과 같이 작성 할 수 있다.

's'$(ifconfig | grep Bcast | awk '{print $2}' | cut -f 4 -d '.')

이를 이용하여 hostname을 변경하는 방법은 아래와 같다.

hostnamectl set-hostname 's'$(ifconfig | grep Bcast | awk '{print $2}' | cut -f 4 -d '.')

매번 수동으로 바꿀 수 없음으로, crontab에 작업을 예약한다.

# m h dom mon dow user  command
  @reboot         root  hostnamectl set-hostname 's'$(ifconfig | grep Bcast | awk '{print $2}' | cut -f 4 -d '.')

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

div#stuning-header .dfd-stuning-header-bg-container {background-image: url(https://tech.sangron.com/wp-content/uploads/sites/2/2018/04/ubuntu_wallpaper_background.jpg);background-color: #3f3f3f;background-size: cover;background-position: top center;background-attachment: initial;background-repeat: no-repeat;}#stuning-header div.page-title-inner {min-height: 350px;}#main-content .dfd-content-wrap {margin: 0px;} #main-content .dfd-content-wrap > article {padding: 0px;}@media only screen and (min-width: 1101px) {#layout.dfd-portfolio-loop > .row.full-width > .blog-section.no-sidebars,#layout.dfd-gallery-loop > .row.full-width > .blog-section.no-sidebars {padding: 0 0px;}#layout.dfd-portfolio-loop > .row.full-width > .blog-section.no-sidebars > #main-content > .dfd-content-wrap:first-child,#layout.dfd-gallery-loop > .row.full-width > .blog-section.no-sidebars > #main-content > .dfd-content-wrap:first-child {border-top: 0px solid transparent; border-bottom: 0px solid transparent;}#layout.dfd-portfolio-loop > .row.full-width #right-sidebar,#layout.dfd-gallery-loop > .row.full-width #right-sidebar {padding-top: 0px;padding-bottom: 0px;}#layout.dfd-portfolio-loop > .row.full-width > .blog-section.no-sidebars .sort-panel,#layout.dfd-gallery-loop > .row.full-width > .blog-section.no-sidebars .sort-panel {margin-left: -0px;margin-right: -0px;}}#layout .dfd-content-wrap.layout-side-image,#layout > .row.full-width .dfd-content-wrap.layout-side-image {margin-left: 0;margin-right: 0;}