Just to clarify:
- You would like to run a geth node starting up with the accounts all locked as per the default
- You later want to run a 'geth attach' command to unlock one or more accounts for a period of time
You can use use the following command to attach to your geth node to unlock an account using the personal.unlockAccount(...) JavaScript API (https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console#personalunlockaccount), where the third parameter is the duration in seconds:
> geth --exec "personal.unlockAccount(eth.accounts[0], 'password', 1000)" attach
true
Or you can create a file, e.g. unlock.js with the following content:
personal.unlockAccount(eth.accounts[0], 'password', 1000);
And then use the following command to unlock the account:
> geth --exec "loadScript('unlock.js')" attach
true
Responding to the question below regarding the default duration, if no duration is specified then a duration of 0 is implied. And a duration of 0 will unlock the account until the program exits, e.g. there is no expiry.
From https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L239-L254 :
// UnlockAccount will unlock the account associated with the given address with
// the given password for duration seconds. If duration is nil it will use a
// default of 300 seconds. It returns an indication if the account was unlocked.
func (s *PrivateAccountAPI) UnlockAccount(addr common.Address, password string, duration *uint64) (bool, error) {
const max = uint64(time.Duration(math.MaxInt64) / time.Second)
var d time.Duration
if duration == nil {
d = 300 * time.Second
} else if *duration > max {
return false, errors.New("unlock duration too large")
} else {
d = time.Duration(*duration) * time.Second
}
err := s.am.TimedUnlock(accounts.Account{Address: addr}, password, d)
return err == nil, err
}
And from https://github.com/ethereum/go-ethereum/blob/master/accounts/account_manager.go#L181-L216 where you can see the comment A timeout of 0 unlocks the account until the program exits
:
// TimedUnlock unlocks the given account with the passphrase. The account
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
// until the program exits. The account must match a unique key file.
//
// If the account address is already unlocked for a duration, TimedUnlock extends or
// shortens the active unlock timeout. If the address was previously unlocked
// indefinitely the timeout is not altered.
func (am *Manager) TimedUnlock(a Account, passphrase string, timeout time.Duration) error {
a, key, err := am.getDecryptedKey(a, passphrase)
if err != nil {
return err
}
am.mu.Lock()
defer am.mu.Unlock()
u, found := am.unlocked[a.Address]
if found {
if u.abort == nil {
// The address was unlocked indefinitely, so unlocking
// it with a timeout would be confusing.
zeroKey(key.PrivateKey)
return nil
} else {
// Terminate the expire goroutine and replace it below.
close(u.abort)
}
}
if timeout > 0 {
u = &unlocked{Key: key, abort: make(chan struct{})}
go am.expire(a.Address, u, timeout)
} else {
u = &unlocked{Key: key}
}
am.unlocked[a.Address] = u
return nil
}