1. 目的
虛擬機(jī)安裝的Arch linux,沒(méi)裝圖形界面,配置的dhcp。為了方便通過(guò)ssh登錄,便想起這個(gè)方法,然而并不知道怎么弄,于是搜索了一番,找到下面參考,記錄一下免得忘了:
blog.sina.com.cn/s/blog_621bce900100wbzf.html
Running Services After the Network is up
2. 實(shí)現(xiàn)
登錄信息在/etc/issue 和 /etc/motd 中,此處只需要修改/etc/issue,步驟就是在網(wǎng)絡(luò)啟動(dòng)之后,用ifconfig或者ip addr取得并提取ip地址,輸出到/etc/issue。由于現(xiàn)在的版本已經(jīng)沒(méi)有/etc/rc.local,所以要添加一個(gè)systemd的服務(wù),這個(gè)服務(wù)調(diào)用腳本去做這個(gè)事。
1. 腳本文件/etc/rc.local:
#!/bin/bash
ipaddr=$(/usr/bin/ip addr | /usr/bin/sed -ne 's/.*inet \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\/\([0-9]\{1,3\}\) *b.*/\1/p')
logintime=$(date)
echo "Arch Linux \r (\l) ($ipaddr - $logintime)" >/etc/issue
2. service文件
取個(gè)名字rc-local.service,文件就放在/etc/systemd/system/下面好了,由于systemd啟動(dòng)的服務(wù)是并行的,所以可能ip地址還沒(méi)分配好,這個(gè)腳本就執(zhí)行完了。只能在起個(gè)服務(wù)了
systemctl enable netctl-wait-online.service
(systemd目錄下有netctl-wait-online.service 和 systemd-networkd-wait-online.service)
同時(shí)加上Wants= and After= dependency on network-online.target
[Unit]
Description=/etc/rc.local Compatibility
Wants=network-online.target
After=network-online.target
Before=getty@tty1.service
[Service]
Type=oneshot
ExecStart=/etc/rc.local
TimeoutSec=0
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
3. 效果

4.freebsd
/etc/rc.local
#!/bin/sh
ipaddr=$(ifconfig | sed -ne 's/.*inet \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\( .*b.*\)/\1/p')
echo " " > /etc/issue
echo "ip=($ipaddr)" >> /etc/issue