Bash Index : N - The 'N' Bash commands : description, flags and examples

namei

Usage :

Follow a pathname until a terminal point is found.
This is exactly what I re-invented .

Flags :

Flag Usage
-l --long Use the long listing format
-x --mountpoints Show mountpoint directories with a D rather than a d

newgrp

Usage :

change the current group ID during a login session (i.e. log in to a new group)

Flags :

Flag Usage
newgrp - when the - is...
  • present : the user's environment will be reinitialized as though the user had logged in
  • absent : the current environment, including current working directory, remains unchanged
newgrp groupName
  • changes the current real group ID to groupName and (try to) add groupName to the user groupset (password may be prompted, group credentials required)
  • if groupName is missing : changes the current real group ID to the default group listed in /etc/passwd

notify-send

Usage :

Send desktop notifications :

Flags :

Flag Usage
-t durationMs --expire-time=durationMs Display the notification during durationMs milliseconds (if unspecified, defaults to ~10s)
-i /path/to/file --icon=/path/to/file Display this icon with the message

Example :

nsupdate

nsupdate is a utility allowing DNS clients to send updates to a DNS server, i.e. creating a dynamic DNS setup.
The implementation described below may _not_ be the most academic way of doing things, especially considering the fact that rules (on AD/DNS side) have been softened so that no authentication key is necessary.
My setup :

Example :

Update the forward records :

  • the -d (debug) flag helps a lot when things are not going as expected
  • DNS updates should not be performed anonymously. TSIG should be used (details)
  • the actual way of updating a record is deleting + re-creating it
cat << EOC | nsupdate -d
server ip.of.dns.server
zone acme.com
update delete myServer.acme.com
update add myServer.acme.com 3600 A 10.27.25.105
send
EOC
Sending update to ip.of.dns.server#53
Outgoing update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id:	7985		NOERROR is its way of saying "it worked"
;; flags:; ZONE: 1, PREREQ: 0, UPDATE: 2, ADDITIONAL: 0
;; ZONE SECTION:
;acme.com.				IN	SOA

;; UPDATE SECTION:
myServer.acme.com. 0		ANY	ANY
myServer.acme.com. 3600	IN	A		10.27.25.105


Reply from update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id:	7985		NOERROR is its way of saying "it worked"
;; flags: qr; ZONE: 1, PREREQ: 0, UPDATE: 2, ADDITIONAL: 0
;; ZONE SECTION:
;acme.com.				IN	SOA

;; UPDATE SECTION:
myServer.acme.com. 0		ANY	ANY
myServer.acme.com. 3600	IN	A		10.27.25.105

Update the reverse records :

cat << EOC | nsupdate -d
server ip.of.dns.server
zone 25.27.10.in-addr.arpa
update delete 105.25.27.10.in-addr.arpa PTR
update add 105.25.27.10.in-addr.arpa 3600 PTR myServer.acme.com.
send
EOC
Sending update to ip.of.dns.server#53
Outgoing update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id:	1423
;; flags:; ZONE: 1, PREREQ: 0, UPDATE: 2, ADDITIONAL: 0
;; ZONE SECTION:
;25.27.10.in-addr.arpa.		IN	SOA

;; UPDATE SECTION:
105.25.27.10.in-addr.arpa. 0	ANY	PTR
105.25.27.10.in-addr.arpa. 3600 IN	PTR	myServer.acme.com.


Reply from update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOERROR, id:	1423
;; flags: qr; ZONE: 1, PREREQ: 0, UPDATE: 2, ADDITIONAL: 0
;; ZONE SECTION:
;25.27.10.in-addr.arpa.		IN	SOA

;; UPDATE SECTION:
105.25.27.10.in-addr.arpa. 0	ANY	PTR
105.25.27.10.in-addr.arpa. 3600 IN	PTR	myServer.acme.com.

Update both forward + reverse records at once :

It is possible to combine both commands, but this requires a little hack : a blank line needs to be introduced between the setting of the A record and the setting of the PTR record :
cat << EOC | nsupdate -d
server 10.27.25.129
zone acme.com
update delete myServer.acme.com
update add myServer.acme.com 3600 A 10.27.25.105
										here !
zone 25.27.10.in-addr.arpa
update delete 105.25.27.10.in-addr.arpa PTR
update add 105.25.27.10.in-addr.arpa 3600 PTR myServer.acme.com.
send
EOC

Without this, trying to alter the PTR record will fail saying :

...
Reply from update query:
;; ->>HEADER<<- opcode: UPDATE, status: NOTZONE, id:	8212
;; flags: qr; ZONE: 1, PREREQ: 0, UPDATE: 4, ADDITIONAL: 0
;; ZONE SECTION:
;25.27.10.in-addr.arpa.		IN	SOA
...

nslookup

Example :

Forward DNS lookup (name into IP) :

Interactive mode :
  • nslookup hostname
  • nslookup hostname dnsServer
  • nslookup
    server dnsServer
    hostname
  • nslookup 
    set type=a|any|cname|mx|ns
    example.com
Non-interactive mode :
nslookup -type=txt hostname dnsServer

Reverse DNS lookup (IP to name) :

  • nslookup IP@
  • nslookup IP@ dnsServer
  • nslookup 
    server dnsServer
    IP@

Random notes :

I've just noticed the following behavior. Given this DNS zone :
web.example.com		A	123.45.67.89
*.example.com		CNAME	web.example.com
test.example.com	TXT	Hello world !
  • nslookup web.example.com : works
  • nslookup $RANDOM.example.com : works
  • nslookup test.example.com : fails
If a domain name exists as a TXT record only, it can not anymore be resolved against a wildcard definition. (Tested on OVH servers)

nohup

Usage :

Start a process so that it will ignore the HUP signal. This signal is, by convention, the way a terminal warns dependent processes of logout.
Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected.
This is somewhat similar to screen or tmux.

Example :

nohup anyCommand &

How to nohup an existing process ?

The idea behind this question is to let a running process ignore the HUP signal it may receive. The typical use case is to make a program launched via CLI survive the closing of the terminal window used to start it.
  • sad news : there is no such thing as "nohup an existing process"
  • good news : but disown will do it for you

nice

Usage :

Run a command with modified scheduling priority (i.e. altered nice value), print or modify the scheduling priority of a job. To change the priority of a running process, consider renice.

Flags :

Flag Usage
(none) Print the current scheduling priority, which is inherited.
-n priority command Run the command command with the priority priority

Example :

nice -n niceValue command argument1 argument2

netstat

/bin/netstat comes from net-tools.

Option Function
(none) Displays the list of open sockets. When available, IP@ and standard sockets numbers are translated into names
-a --all Show both listening and non-listening sockets
-c --continuous Redisplays the required information every second
-e --extend Display additional information. Use this option twice for maximum detail
-i Display a table of all network interfaces (name, MTU, metric, received/sent packets, errors, ...)
-l --listening Show only listening sockets. (These are omitted by default.)
-n Displays all fields in numerical form (=non-resolved). This includes addresses, ports, user ids, ...
-o --timer Include information related to networking timers
-p --program Show the PID and name of the program to which each socket belongs
--protocol=family Specifies the address families (perhaps better described as low level protocols) for which connections are to be shown. family is a comma (,) separated list of address family keywords like:
  • inet This includes the raw, TCP and UDP protocols. Apache is seen using the TCP6 protocol (details).
  • unix
  • ipx
  • ax25
  • netrom
  • ddp
-r --route Displays the kernel routing tables
-s --statistics Display summary statistics for each protocol
-t --tcp Show only TCP connections
-u --udp Show only UDP connections

More "netstat" :

  • netstat -pan
  • netstat -lapute
  • netstat -laputen

This is easy to remember for french speakers as la pute is a french insult (and you all known how much we love swearing )

Apache and TCP6

In /etc/apache2/ports.conf, if the listening port is configured as :

  • Listen 80 : Apache listens by default on IPv6 addresses (hence the TCP6), but also replies on IPv4 addresses.
  • Listen 0.0.0.0:80 : Apache only listens on IPv4 addresses.

How to "do netstat" without /bin/netstat ?

Have a look at :
  • /proc/net/tcp
  • /proc/net/udp
  • ...
Read + convert IP addresses :
  1. cat /proc/net/tcp
      sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
       0: 6538A8C0:008B 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 132977 1 000000007606394f 100 0 0 10 0
       1: 00000000:006F 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 14694 1 000000003f7e8f86 100 0 0 10 0
    ...
    
  2. echo '6538A8C0' | awk 'BEGIN { FIELDWIDTHS="2 2 2 2" } { printf("%d.%d.%d.%d\n", strtonum("0x"$4), strtonum("0x"$3), strtonum("0x"$2), strtonum("0x"$1)) }'
    192.168.56.101
Alternate solution to split strings without delimiter with Awk :

https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html#index-patsplit_0028_0029-function-_0028gawk_0029
patsplit(string, array [, fieldpat [, seps ] ]) #

echo '12345678' | awk '{patsplit($0, myArray, ".."); printf("%d %d %d %d", myArray[4], myArray[3], myArray[2], myArray[1]); }'
Read + convert port numbers :
Based on the example above :
139